@KafkaListener与批处理Kakfa侦听器的正常关机不起作用

时间:2020-05-14 14:21:43

标签: java apache-kafka spring-kafka

2020-05-14 19:32:11.238信息19880 --- [on(4)-127.0.0.1] oscsupport.DefaultLifecycleProcessor:无法在30000的超时时间内关闭具有相位值2147483547的1个bean:[org] .springframework.kafka.config.internalKafkaListenerEndpointRegistry]

1 个答案:

答案 0 :(得分:0)

默认情况下,Spring要求每个SmartLifecycle阶段中的bean在30秒内停止;您可以通过添加以下bean来更改该行为:

@Bean
public DefaultLifecycleProcessor lifecycleProcessor() {
    DefaultLifecycleProcessor lp = new DefaultLifecycleProcessor();
    lp.setTimeoutPerShutdownPhase(120_000);
    return lp;
}

编辑

在其侦听器正在处理批处理时停止容器不会影响该批处理;侦听器线程没有被容器杀死;但是,即使监听器尚未真正完成,容器也会默认在10秒后发布容器停止事件(容器属性shutDownTimeout)。

如果您担心生命周期处理器会在批处理中终止线程,并且不想增加其超时时间,则可以通过暂停使用者和监听事件的组合来执行正常关机。

这里是一个例子:

@SpringBootApplication
public class So61799727Application {

    public static void main(String[] args) {
        SpringApplication.run(So61799727Application.class, args);
    }


    @KafkaListener(id = "so61799727", topics = "so61799727", concurrency = "3")
    public void listen(List<String> in) {
        System.out.println(in);
    }

    @Bean
    public NewTopic topic() {
        return TopicBuilder.name("so61799727").partitions(3).replicas(1).build();
    }

    @Bean
    public ApplicationRunner runner(KafkaTemplate<String, String> template,
            KafkaListenerEndpointRegistry registry) {

        return args -> {
            sendTen(template);
            System.out.println("Hit enter to pause container");
            System.in.read();
            registry.getListenerContainer("so61799727").pause();
        };
    }

    public static void sendTen(KafkaTemplate<String, String> template) {
        IntStream.range(0, 10).forEach(i -> template.send("so61799727", "foo" + i));
    }

}
@Component
class Eventer {

    privaate final KafkaTemplate<String, String> template;

    private final AtomicInteger paused = new AtomicInteger();

    Eventer(KafkaTemplate<String, String> template) {
        this.template = template;
    }

    @EventListener
    public void paused(ConsumerPausedEvent event) {
        System.out.println(event);
        if (this.paused.incrementAndGet() ==
                event.getContainer(ConcurrentMessageListenerContainer.class).getConcurrency()) {
            System.out.println("All containers paused");
            So61799727Application.sendTen(this.template);
        }
    }

    @EventListener
    public void idle(ListenerContainerIdleEvent event) {
        System.out.println(event);
    }

}
spring.kafka.listener.idle-event-interval=5000
spring.kafka.listener.type=batch
spring.kafka.producer.properties.linger.ms=50