Difference between revisions of "Spring-integration error handling"

(Add spring-integration error / exception handling)
 
Line 5: Line 5:
 
Java uses Errors and Exceptions to signal application non-expected events and runtime errors. Here is the hierarchy:  
 
Java uses Errors and Exceptions to signal application non-expected events and runtime errors. Here is the hierarchy:  
  
[[File:Exception-hierarchy.png|900px|Java exception hierarchy]]
+
[[File:Exception-hierarchy.png|700px|Java exception hierarchy]]
  
 
   
 
   
 
* Java <code>Exception</code> is a standard error. Such item might come from your application or a dependency.
 
* Java <code>Exception</code> 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 <code>Error</code> should be reserved to <code>java.lang</code> 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!
  
* Java <code>Error</code> should be reserved to <code>java.lang</code> 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''
 
  
 
   
 
   

Revision as of 12:40, 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.
  • 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);
       }
   }
}