Spring KafkaTemplate annotation type implementation factory mode

Implementation Notes-style injection kafkaTemplate producers and consumers, simplify configuration file

 

table of Contents

 

 

Consumers factory
/**
 * 消费者工厂
 */
@EnableKafka
@Configuration
public class KafkaConsumerFactory {

    @Autowired
    private ApplicationContext context;

    /**
     * 获取消费者工厂
     */
    public ConsumerFactory<String, String> consumerFactory(String kafkaBroker) {

        // 消费者配置信息
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBroker);
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, Boolean.TRUE);
        props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 10);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);

        return new DefaultKafkaConsumerFactory<>(props);
    }

    /**
     * 容器配置
     *
     * @param groupId   组名
     * @param clazz     消费者监听器
     * @param topicName topicName
     * @return 容器配置
     */
    public ContainerProperties containerProperties(String groupId, Class clazz, String topicName) {

        ContainerProperties containerProperties = new ContainerProperties(topicName);
        containerProperties.setAckMode(AbstractMessageListenerContainer.AckMode.RECORD);
        containerProperties.setGroupId(groupId);
        containerProperties.setMessageListener(context.getBean(clazz));
        return containerProperties;
    }

    /**
     * 获取消费容器实例
     *
     * @param kafkaBroker kafka server
     * @param groupId     组名
     * @param clazz       消费者监听器
     * @param topicName   topicName
     * @param threadCount 消费线程数
     * @return 消息监听容器
     */
    public ThreadMessageListenerContainer<String, String> kafkaListenerContainer(
            String kafkaBroker, String groupId, Class clazz, String topicName, int threadCount) {

        ThreadMessageListenerContainer<String, String> container
                = new ThreadMessageListenerContainer<>(
                consumerFactory(kafkaBroker), containerProperties(groupId, clazz, topicName));
        container.setConcurrency(threadCount);
        container.getContainerProperties().setPollTimeout(3000);
        return container;
    }

}
 
Producer Factory
/**
 * 生产者工厂
 */
@EnableKafka
@Configuration
public class KafkaProducerFactory {

    @Autowired
    private ApplicationContext context;


    /**
     * 获取生产者工厂
     */
    public ProducerFactory<String, String> producerFactory(String kafkaBroker) {

        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBroker);
        props.put(ProducerConfig.RETRIES_CONFIG, 1);
        props.put(ProducerConfig.ACKS_CONFIG, "all");
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

        return new DefaultKafkaProducerFactory<>(props);
    }

    /**
     * 注册生产者实例
     */
    public KafkaTemplate<String, String> kafkaTemplate(String kafkaBroker, String topicName, Class clazz) {

        KafkaTemplate<String, String> template = new KafkaTemplate<>(producerFactory(kafkaBroker), Boolean.FALSE);
        template.setDefaultTopic(topicName);
        template.setProducerListener((ProducerListener<String, String>) context.getBean(clazz));
        return template;
    }
}

 

Initialization listener, examples

/**
 * kafka 初始化
 */
@Component
public class KafkaInit {

    /**
     * kafka server
     */
    @Value("${kafka.servers}")
    private String kafkaBroker;

    /**
     * 组名
     */
    @Value("${kafka.group}")
    private String groupId;

    /**
     * topicName
     */
    @Value("${kafka.topic}")
    private String topicName;

    /**
     * 消费者工厂
     */
    @Autowired
    private KafkaConsumerFactory kafkaConsumerFactory;

    / ** 
     * Producer Factory 
     * / 
    @Autowired 
    Private KafkaProducerFactory kafkaProducerFactory; 


    / ** 
     * Servlet running on the server when loaded, and will only be called once the server 
     * / 
    @PostConstruct 
    public  void Consumer () { 

        kafkaConsumerFactory.kafkaListenerContainer (kafkaBroker, . the groupId, TestConsumerListener class , topicName,. 6 ) 
                .startContainer (); 
        // load consumers listener 

    } 

    / ** 
     * Get the producer instance 
     * / 
    @Bean ( "testSender" )
     public KafkaTemplate <String, String> testSender () { 

        return kafkaProducerFactory.kafkaTemplate(kafkaBroker, topicName, DefaultProducerListener.class);
    }

}

 

Loading the container for manual control of

/**
 * 继承消息监听容器
 */
public class ThreadMessageListenerContainer<K, V> extends ConcurrentMessageListenerContainer<K, V> {


    public ThreadMessageListenerContainer(ConsumerFactory<K, V> consumerFactory, ContainerProperties containerProperties) {
        super(consumerFactory, containerProperties);
    }

    public void startContainer() {
        super.doStart();
    }
}

 

Producers listeners
/ ** 
 * Default producers listeners 
 * / 
@Component 
public  class DefaultProducerListener the extends ProducerListenerAdapter { 

    / ** 
     * After a successful call to send the message 
     * / 
    @Override 
    public  void onSuccess (Topic String, Integer Partition, Object Key, 
                          Object value, RecordMetadata recordMetadata ) { 
        Super .onSuccess (Topic, Partition, Key, value, recordMetadata); 
        System.out.println ( "message sent successfully!" ); 
    } 

    / ** 
     * error message after sending call 
     * / 
    @Override 
    public  voidthe onError (Topic String, Integer Partition, Object Key, 
                        Object value, Exception Exception) { 
        Super .onError (Topic, Partition, Key, value, Exception); 
        System.out.println ( "message failed!" ); 
    } 

    / * * 
     * is turned transmitted listener 
     * 
     * @return to true open, false off
      * / 
    @Override 
    public  Boolean isInterestedInSuccess () {
         return  to true ; 
    } 

}

 

Consumers listeners
/ ** 
 * Consumer listeners 
 * / 
@Component 
public  class TestConsumerListener the implements the MessageListener <String, String> { 


    / ** 
     * Consumer news 
     * 
     * @param the Record Message
      * / 
    @Override 
    public  void onMessage (ConsumerRecord <String, String> the Record ) { 

        System.out.println (Record); 
    } 
}

 

Send a test message
/ ** 
 * message 
 * / 
@Component 
public  class TestProducerSender { 

    / ** 
     * transfer transmission queue 
     * / 
    @Autowired 
    @Qualifier ( "testSender" )
     Private KafkaTemplate kafkaTemplate; 

    / ** 
     * Test message 
     * / 
    public  void the sendMessage () { 

        kafkaTemplate.sendDefault ( "Test Message" ); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/lidada/p/11346504.html