【问题标题】:How to stop Spring Kafka Listner and invoke some function to execute after the Consumer is stopped?如何在消费者停止后停止 Spring Kafka Listener 并调用相同的函数来执行?
【发布时间】:2022-01-22 00:53:44
【问题描述】:

我有一个场景,我需要停止 kafka 并在 Consumer 停止后调用一些函数来执行。同样,流程是这样的:

  1. 使用来自 kafka 主题的消息
  2. 将每个消费的消息添加到文件中
  3. 如果过去 10 秒内没有收到任何消息,则停止 kafka 侦听器
  4. 调用一些函数例如:UploadFileToS3()

我在我的消费者方法上使用 spring kafka 的 @KafkaListener 注释。

我知道停止使用 @KafkaListener 注释的 Kafka 消费者我可以使用 KafkaListenerEndpointRegistry

我想要这样的东西:

if(not consumed messages for more than 10s) {
    kafkaListenerEndpointRegistry.getListenerContainer("my-listener-id").stop();
    UploadFileToS3()
}

如何使用 Spring Kafka 做到这一点?

【问题讨论】:

    标签: spring-boot apache-kafka kafka-consumer-api spring-kafka


    【解决方案1】:

    ListenerContainerIdleEvent:

    /**
     * An event that is emitted when a container is idle if the container
     * is configured to do so.
     *
     * @author Gary Russell
     *
     */
    public class ListenerContainerIdleEvent extends KafkaEvent {
    

    文档在这里:https://docs.spring.io/spring-kafka/docs/current/reference/html/#idle-containers

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2017-09-23
    • 2022-10-23
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    相关资源
    最近更新 更多