Kafka producer callback Exception

后端 未结 3 869
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 00:15

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

kafkaProducer.send(produce         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 00:30

    Trying to add more info to @Mike's answer, I think only a few Exceptions are enum in Callback Interface.

    Here you can see the whole list: kafka.common.errors

    And here, you can see which ones are retriables and which ones are not: kafka protocol guide

    And the code could be sht like this:

    producer.send(record, callback)
    
    def callback: Callback = new Callback {
        override def onCompletion(recordMetadata: RecordMetadata, e: Exception): Unit = {
          if(null != e) {
             if (e == RecordTooLargeException || e == UnknownServerException || ..) {
                log.error("Winter is comming") //it's non-retriable
                writeDiscardRecordsToSomewhereElse
             } else {
                log.warn("It's no that cold") //it's retriable
             }
          } else {
            log.debug("It's summer. Everything is fine")
          }
        }
    }
    

提交回复
热议问题