首页 > 解决方案 > springboot中使用@KafkaListener时,如何设置idleBetweenPolls

问题描述

"idleBetweenPolls" 是 spring-kafka 中一个方便的配置,用于调整消耗率。

但是,在检查 springboot 的源代码和文档时,似乎没有提到这个 props 或如何将某些特定值传播到 org.springframework.kafka.listener.ContainerProperties。

任何人都知道无论如何要实现上述目标?

标签: spring-bootspring-kafka

解决方案


添加一个容器工厂定制器 bean:

@Component
class Customizer {

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

}

这将在所有容器上设置它。

如果您只想在特定容器上设置它:

@Component
class Customizer2 {

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

}

或者,配置您自己的工厂,而不是使用 Boot 的自动配置工厂。


推荐阅读