如何通过application.properties(factory.setBatchListener(true))激活KafkaBatchListener

时间:2018-11-27 14:08:03

标签: java spring-kafka

文档明确说明了如何设置出厂设置以启用批处理侦听器:https://docs.spring.io/spring-kafka/reference/htmlsingle/#batch-listeners

我通过应用程序属性进行所有其他配置:

spring.kafka.bootstrap-servers=url
spring.kafka.listener.ack-mode=record
spring.kafka.consumer.enable-auto-commit=false
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.group-id=id-we
spring.kafka.consumer.max-poll-records=1000
spring.kafka.consumer.value-deserializer=org.meteogroup.kafka.envelope.StringEnvelopeDeserializer

我想避免通过@Value参数将所有这些手动移动到代码中。有没有一种方法可以通过应用程序属性激活批量监听?

1 个答案:

答案 0 :(得分:2)

不是通过属性,而是您可以简单地覆盖出厂时启动的默认自动配置...

@Bean
public ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
        ConcurrentKafkaListenerContainerFactoryConfigurer configurer,
        ConsumerFactory<Object, Object> kafkaConsumerFactory) {

    ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
    configurer.configure(factory, kafkaConsumerFactory);

    factory.setBatchListener(true);

    return factory;
}