1

I am using spring integration XML and want to know what is the best way to deal with error handling.

I am connecting to s3 using s3-inbound-streaming-channel-adapter and then transforming the csv file from s3 bucket.

Potential errors that can happen are:

  1. Transformation Exception could happen if a line in the file is not valid and so can throw a custom error:

LineTransformationException

  1. What if on s3 bucket an image file is placed by mistake and again could get Transformation Exception

  2. s3 could be down and could get

Caused by: com.amazonaws.SdkClientException: Unable to execute HTTP request: Connect to localhost:4572 [localhost/127.0.0.1] failed: Connection refused

And the list of many known and unknown errors can go on...

  1. So what is the best way to handle all these errors? Through custom ErrorHandler or exception-type-router

  2. If doing it through ErrorHandler then how to cater for so many Exceptions.

  3. And is there a catch-all exception handler?

    public class ErrorHandler {

    public void handleFailure(Message errorMmessage) {

    MessagingException payload = (MessagingException) errorMmessage.getPayload();
    
    LOG.info(">>--- Exception --- " + payload.getCause());
    

    }}

or

 <int:exception-type-router input-channel="errorChannel"
                           default-output-channel="nullChannel">

    <int:mapping exception-type="com.api.exception.TransformationException"
                 channel="transformErrorChannel"/>

    <int:mapping exception-type="com.amazonaws.SdkClientException"
                 channel="clientErrorChannel"/>

</int:exception-type-router>

<int:channel id="transformErrorChannel"/>

<int:service-activator ref="errorHandler"
                       method="handleFailure"
                       input-channel="transformErrorChannel"
                       output-channel="nullChannel"/>

<int:service-activator ref="clientErrorHandler"
                       method="handleFailure"
                       input-channel="clientErrorChannel"
                       output-channel="nullChannel"/>
user2279337
  • 691
  • 5
  • 13
  • 26

1 Answers1

2

The s3-inbound-streaming-channel-adapter together with its <poller> can be configured with the error-channel. By default the polling error (and therefore all the downstream) are routed to the global errorChannel: https://docs.spring.io/spring-integration/docs/5.0.5.RELEASE/reference/html/configuration.html#namespace-errorhandler

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • 1
    Thanks. I've gone with custom error handler with two methods one to handle transformation exception e.g. messageHandlingFailure(Message message) and the other to handle any other type errors e.g. handleFailure(Message message). Do you think this is the right thing to do? – user2279337 Jun 08 '18 at 16:01
  • 1
    Also how to disable the LoggingHandler? as its continously output the stage trace. – user2279337 Jun 08 '18 at 16:03
  • 1
    You can declare you own `errorChannel` bean. This way that `LoggingHandler` will be disabled. – Artem Bilan Jun 08 '18 at 16:04