【问题标题】:How to run consumer @StreamListener only after ApplicationReadyEvent method completed?如何仅在 ApplicationReadyEvent 方法完成后运行消费者 @StreamListener?
【发布时间】:2022-03-10 16:39:58
【问题描述】:

我有一个消费者方法

@StreamListener(target = Sink.INPUT)
method()

我有一个带有事件监听器的方法

@EventListener(ApplicationReadyEvent.class)
method()...

是否可以将@StreamListener 配置为只有在@EventListener 方法完成后才开始监听?

【问题讨论】:

    标签: java spring-boot spring-cloud kafka-consumer-api event-listener


    【解决方案1】:

    根据 Spring Cloud Stream docs 您可以使用属性 spring.cloud.stream.bindings.<bindingName>.consumer.autoStartup=false 这将阻止 BindingService 自动启动消费者绑定。然后你可以在需要时自己做,例如在ApplicationReadyEvent监听器中。

    public class DelayedBindingStarter {
    
    private final BindingsEndpoint bindingsEndpoint;
    
    @EventListener(ApplicationReadyEvent.class)
    public void applicationReady() {
        bindingsEndpoint.changeState("binding_name", BindingsLifecycleController.State.STARTED);
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2019-09-07
      • 1970-01-01
      • 2022-10-26
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 2020-01-06
      相关资源
      最近更新 更多