简体   繁体   中英

When using @KafkaListener in springboot, how to set idleBetweenPolls

"idleBetweenPolls" is a handy config in spring-kafka, to adjust the rate of consumption.

However when checking source code and doc of springboot, there seems to be no mention of this props or how to propagate some particular value into org.springframework.kafka.listener.ContainerProperties.

Anyone know of anyway to achieve the above?

Add a container factory customizer bean:

@Component
class Customizer {

    public Customizer(ConcurrentKafkaListenerContainerFactory<?, ?> factory) {
        factory.getContainerProperties().setIdleBetweenPolls(5_000L);
    }

}

This will set it on all containers.

If you only want to set it on a specific container:

@Component
class Customizer2 {

    public Customizer2(ConcurrentKafkaListenerContainerFactory<?, ?> factory) {
        factory.setContainerCustomizer(container -> {
            if (container.getContainerProperties().getGroupId().equals("theOneIWant") {
                container.getContainerProperties().setIdleBetweenPolls(5_000L);
            }
        });
    }

}

Or, configure your own factory rather than using Boot's auto-configured factory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM