Spring-integration error handling

Revision as of 11:40, 16 November 2015 by WikiFreak (talk | contribs)


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.
  • 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 behavior:

  • Spring-integration can survive and handle Exceptions. Exceptions will be send to the error-channel automatically = they are part of the flow.
  • Spring integration cannot survive Errors. if such an even occurs the framework will stop working and block your application!



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);
       }
   }
}