【问题标题】:How to detect topic does not exist within Spring when using @KafkaListener使用@KafkaListener时如何检测Spring中不存在主题
【发布时间】:2021-05-31 23:30:55
【问题描述】:

当尝试使用@KafkaListener 订阅不存在的主题时,它会记录一个警告:

2021-04-22 13:03:56.710  WARN 20188 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-gg-2, groupId=gg] Error while fetching metadata with correlation id 174 : {not_exist=UNKNOWN_TOPIC_OR_PARTITION}

如何检测和处理这种情况?我尝试了errorHandler,它没有被调用:

    @KafkaListener(topics = "not_exist", groupId = "gg", errorHandler = "onError")
    public void receive(String m) {
        log.info("Rcd: " + m);
    }

...

    @Bean
    public KafkaListenerErrorHandler onError() {
        return new KafkaListenerErrorHandler() {
            @Override
            public Object handleError(Message<?> message, ListenerExecutionFailedException e) {
                log.error("handleError Error: {} message: {}", e.toString(), message);
                return message;
            }
        };
    }

【问题讨论】:

    标签: spring apache-kafka


    【解决方案1】:

    我想你可以在org/springframework/kafka/listener/KafkaListenerErrorHandler.java中找到答案

    * @return the return value is ignored unless the annotated method has a {@code @SendTo} annotation.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-28
      • 1970-01-01
      • 2019-01-20
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      相关资源
      最近更新 更多