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:
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