Spring-integration error handling
Spring-integration error management
Java uses Errors and Exceptions to signal application non-expected events and runtime errors. Here is the 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 tojava.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);
}
}
}