Difference between revisions of "Spring-integration error handling"

(Add spring-integration error / exception handling)
(No difference)

Revision as of 11:38, 16 November 2015


Spring-integration error management

Java uses Errors and Exceptions to signal application non-expected events and runtime errors. Here is the hierarchy:

Java exception hierarchy


  • Java Exception is a standard error. Such item might come from your application or a dependency.
    • Spring-integration can survive and handle Exceptions. Moreover, exceptions will be send to the error-channel automatically. They are part of the flow.


  • Java Error should be reserved to java.lang and JVM. Errors should only occur in case of emergency and fatal issue

However some libraries do throw Error instead of Exception. :(

    • Spring integration cannot survive Errors


How to avoid Spring-integration failure?

As some library are throwing Error we must handle Throwable events!


In the code you must do the following every-time your processing a Spring-integration message:

@Service
public class MyBean implements IMyBean {
   private static final Logger LOGGER = Logger.getLogger(MyBean.class);
 
   [...]
 
   @Transactional(propagation = Propagation.REQUIRES_NEW)
   @Override
   public Bean springImportMessageProcess(Bean params) {
       try {
       } catch(Exception e) { 
            LOGGER.error("Something wrong happened, failed to process message!", e);
            // You can throw the exception if you'd like to since Spring-integration will redirect it to its error channel.
            throw e;
       } catch(Throwable e) {
           // You must NOT push the error to the caller !! It will break Spring-integration
           LOGGER.fatal("Something wrong happened, failed to process message! The JVM or a key library has encountered a FATAL error!", e);
       }
   }
}