Difference between revisions of "Spring-integration error handling"

 
Line 11: Line 11:
 
* 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. :(
 
* 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 behaviour:
 
* Spring-integration can survive and handle Exceptions. Exceptions will be send to the error-channel automatically = they are part of the flow.
 
* 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!
 
* Spring integration cannot survive Errors. if such an even occurs the framework will stop working and block your application!
Line 38: Line 38:
 
       } catch(Exception e) {  
 
       } catch(Exception e) {  
 
             LOGGER.error("Something wrong happened, failed to process message!", 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.
+
             // You can throw the exception if you like. Spring-integration will redirect it to its error channel.
 
             throw e;
 
             throw e;
 
       } catch(Throwable e) {
 
       } catch(Throwable e) {

Latest revision as of 12:43, 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 behaviour:

  • 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 like. 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);
       }
   }
}