卡夫卡生产者回调异常

卡夫卡生产者回调异常

本文介绍了卡夫卡生产者回调异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们生成消息时,我们可以定义一个回调,该回调可能会发生异常:

When we produce messages we can define a callback, this callback can expect an exception:

kafkaProducer.send(producerRecord, new Callback() {
  public void onCompletion(RecordMetadata recordMetadata, Exception e) {
    if (e == null) {
      // OK
    } else {
      // NOT OK
    }
  }
});

考虑到生产者中的内置重试逻辑,我想知道开发者应该明确处理哪种异常?

Considered the buitl-in retry logic in the producer, I wonder which kind of exception should developers deal explicitly with?

推荐答案

根据回调Java文档,回调期间可能发生以下异常:

According to the Callback Java Docs there are the following Exception possible happening during callback:

  • InvalidTopicException
  • OffsetMetadataTooLargeException
  • RecordBatchTooLargeException
  • RecordTooLargeException
  • UnknownServerException
    • InvalidTopicException
    • OffsetMetadataTooLargeException
    • RecordBatchTooLargeException
    • RecordTooLargeException
    • UnknownServerException
      • CorruptRecordException
      • InchvalidMetadataException
      • NotEnoughReplicasAfterAppendException
      • NotEnoughReplicasException
      • OffsetOutOfRangeException
      • TimeoutException
      • UnknownTopicOrPartitionException
        • CorruptRecordException
        • InchvalidMetadataException
        • NotEnoughReplicasAfterAppendException
        • NotEnoughReplicasException
        • OffsetOutOfRangeException
        • TimeoutException
        • UnknownTopicOrPartitionException
        • 也许这不是一个令人满意的答案,但是最终 异常和如何处理异常完全取决于您的用例和业务需求.

          Maybe this is a unsatisfactory answer, but in the end which Exceptions and how to handle them completely relies on your use case and business requirements.

          但是,作为开发人员,您还需要处理Kafka Producer的重试机制本身.重试主要由以下因素驱动:

          However, as a developer you also need to deal with the retry mechanism itself of the Kafka Producer. The retries are mainly driven by:

          建议将上述三种配置的默认值保留在上面,而将重点放在由...定义的硬上限上

          The recommendation is to keep the default values of those three configurations above and rather focus on the hard upper time limit defined by

          这篇关于卡夫卡生产者回调异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 17:09