Is there a way to configure polling interval of @KafkaListener?

Viewed 692

I'm new in the area of SpringBoot with Kafka. I saw that the common way to define a consumer is by @KafkaListener.
In the psat I configure a scheduler which polling the Kafka broker every X time, but here I didn't found how to specified this interval or a documentation about pushing data from the broker to the consumer. Is there a way to configure it ( or if the way of consuming data is by pushing I'll be happy to understand it)

1 Answers

It was recently introduced into spring kafka, so if you are using modern version it should look like this:

build.gradle

implementation("org.springframework.kafka:spring-kafka:2.6.1") // or higher, minimum required is 2.3.0

Config.java

@Bean("singleKafkaListenerContainerFactoryManualCommit")
public ConcurrentKafkaListenerContainerFactory<?, ?> singleKafkaListenerContainerFactoryManualCommit(ConcurrentKafkaListenerContainerFactoryConfigurer configurer, ConsumerFactory<Object, Object> kafkaConsumerFactory) {
    ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
    configurer.configure(factory, kafkaConsumerFactory);
    factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
    factory.setBatchListener(false);
    factory.getContainerProperties().setIdleBetweenPolls(1000L); // <- this is basically it
    return factory;
}

How it's look like:

2020-11-27 17:04:41.099  INFO 67146 --- [ntainer#0-0-C-1] c.r.t.m.d.l.service.KafkaService    : Consumed message 
2020-11-27 17:04:42.143  INFO 67146 --- [ntainer#0-0-C-1] c.r.t.m.d.l.service.KafkaService    : Consumed message
2020-11-27 17:04:43.185  INFO 67146 --- [ntainer#0-0-C-1] c.r.t.m.d.l.service.KafkaService    : Consumed message
2020-11-27 17:04:44.223  INFO 67146 --- [ntainer#0-0-C-1] c.r.t.m.d.l.service.KafkaService    : Consumed message

Keep in mind that you will need kafka broker version >= 1.0 to make it work.

Related