简体   繁体   中英

Error Handling in Kafka Producer while using Spring Cloud Stream Kafka Binder

I am having an rest post endpoint which consumes data and writes to Kafka using Spring Cloud Stream Kafka Binder. Right now we are not having any error handling in place. But we want to make this endpoint fault tolerant by adding an extra check whenever data is not written to Kafka. We intend to send an exception info object when data is not written to Kafka. I am trying to achieve this using global errors in this way

@ServiceActivator(inputChannel = "errorChannel")
public void handle(final ErrorMessage em) {
logger.error("encountered exception" + em.getOriginalMessage().toString());
throw new RuntimeException(em.getOriginalMessage().toString);
}

My doubt is two fold:

  1. Is this the correct way to handle exceptions when the data we write to Kafka fails.
  2. Is this handle method called whenever data write is failed and is this change propagated to system level error handling.

If there is another process please suggest. We are currently exploring application level error handling and global level error handling. System level error handling is off the table for now. Thanks in advance.

You have to opt in for async error handling, using errorChannelEnabled https://docs.spring.io/spring-cloud-stream/docs/3.1.3/reference/html/spring-cloud-stream.html#_producer_properties

You can get an exception on the sending thread, by setting the kafka producer property sync to true https://docs.spring.io/spring-cloud-stream-binder-kafka/docs/3.1.3/reference/html/spring-cloud-stream-binder-kafka.html#kafka-producer-properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM