Announcement Announcement Module
No announcement yet.
Custom Error Handling Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom Error Handling

    I'm using Spring Integration in the following setup :

    JMS-InboundGateway --> ComponentA --> Component B --> ComponentC and then back to the inboundGateway using the default reply destination.

    This all works great, except of course when the data read from to queue triggers an error in one of the three Components.

    The original idea was to log the errors in the components and set a header in the message indicating that an error occurred and in which component it occurred in. Then use a router to determine if the fawlty message is to be send right back or the correct message to be passed to the next component.
    However this would mean writing the same code for logging the error and setting the error headers over and over again.

    So I created and aspect "after-throwing" and a pointcut on the classes.
    This way I can log the error and set the message in the aspect based upon the source of the exception.

    However Spring Integration return an Message containing the ErrorMessage as payload when an exception occurs in a service activator.

    Is there a way to override this behavior and return the Message I created in the aspect instead ?

  • #2
    I have two suggestions.

    First is that you could make use of jms transactions. Depending on what transaction manager you are using this may do what you want it to regarding sending back to error queue if a problem occurs. Of course it will also rollback the message for you and possibly even retry consumption a number of times. However it depends on what you are using underneath.

    Second is that Spring Integration has built in error handling. Have you looked at the error handling sample application by iwein? That helped me a lot when I was trying to understand the error handling. Although I haven't tried this I think you can declare a header enricher after your jms inbound gateway and ask it to send errors to a particular channel. Then you could declare an endpoint that listens on that channel. That will apply to errors that occur at any point after the header enricher declaration so the same handler will get invoked for any errors that occur in any of the three endpoints after the jms inbound gateway.

    Although SI will send an ErrorMessage to your error handler that listens on the error channel you have two options regarding how you handle this. You could either directly extract the exception or the failed message from the ErrorMessage in your error handler. Or you could declare a transformer before your error handler that does this extraction for you as illustrated in the error handling example. This way your error handler is only concerned about putting the failed message back on the error queue.

    Hope that helps.