KafkaListener 的默认 ack 策略是什么?

huangapple go评论70阅读模式
英文:

What is the default kafka ack policy for @KafkaListener?

问题

  1. "log.info("new message from kafka topic received: {}", message);" 被调用之前,将会发送 ack 吗?

  2. 如果第一个问题的答案是 "No",即使发生异常,ack 会被发送吗?

英文:

Let's say I have such a listener and no custom kafka configuration:

@KafkaListener(topics = "${my_topic_listen}", autoStartup = "true")
public void listenForMessage(String message) {
    log.info("new message from kafka topic received: {}", message);
    if(message.length() >1000) {
       throw new RuntimeException("TooLongMessage");
    }
    log.info(message is checked);
}
  1. Will ack be sent before

    log.info("new message from kafka topic received: {}", message);

is called ?

  1. if answer for the first question is "No". Will be ack sent even in case of exception ?

答案1

得分: 1

会在发生异常的情况下发送ack吗?

这取决于错误处理程序。

默认情况下,引发异常的记录将在记录和确认之前重试最多9次。您可以配置重试行为,并添加自定义恢复操作,以在重试用尽时采取一些措施(或使用提供的DeadLetterPublishingRecoverer)。您还可以配置哪些异常可以重试,哪些不能。

请参阅https://docs.spring.io/spring-kafka/docs/current/reference/html/#annotation-error-handling

英文:

>Will be ack sent even in case of exception ?

It depends on the error handler.

By default, the record causing the exception will be retried up to 9 times before being logged and ack'd. You can configure the retry behavior, and add a custom recover to take some action when retries are exhausted (or use the provided DeadLetterPublishingRecoverer. You can also configure which exceptions are retryable, and which are not.

See https://docs.spring.io/spring-kafka/docs/current/reference/html/#annotation-error-handling

答案2

得分: 0

根据这里提到的信息,仅在消息被消费者消耗后,确认将发送给代理。<br>

  1. 仅在listenForMessage方法完全执行后,将发送确认。
    <br>
  2. 由于异常与工作环境的故障无关,Kafka消费者将消息标记为已消耗并将确认发送给代理。
英文:

As mentioned here, the ack will be sent to the broker only after the message is consumed by the consumer. <br>

  1. Ack will be sent after the listenForMessage method is completely executed.
    <br>
  2. Since the exception is not related to the failure of working environment, the kafka consumer marks the message as consumed and sends the ACK to the broker.

huangapple
  • 本文由 发表于 2023年5月22日 18:10:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定