Here's another exercise where there were just no examples. I spent a half day misunderstanding that the phrase "in that channel" in
THe Spring Integration Guide was referring to the ErrorChannel and not the channel that was processing the message.
So, here is an example of configuring an exception handler for exceptions being thrown by a Transformer. Since the handler is listening on the errorChannel, the exception still ends up being logged. I assume that you would need to create your own errorChannel bean to get a different behavior with this strategy.
Of course, you can also just use an errorChannel header which is probably the right thing to do anyway.
In the configuration file:
@Bean
public IntegrationFlow exceptionFlow() {
return IntegrationFlows
.from(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME)
.route(transformerExceptionRouter())
.get();
}
@Bean
public ErrorMessageExceptionTypeRouter transformerExceptionRouter() {
ErrorMessageExceptionTypeRouter router = new ErrorMessageExceptionTypeRouter();
router.setChannelMapping(CustomException.class.getCanonicalName(), "TransformerExceptionFlow");
return router;
}
@Bean
public IntegrationFlow transformerException() {
return IntegrationFlows
.from("TransformerExceptionFlow")
.handle(MessageTransformationException.class, (p, h) -> {
EquipmentUpdateTransformerException e = (CustomException)(p.getCause().getCause());
System.out.println("Could process xml: " + e.getSource()); return e;
})
.get();
}
I created a CustomException to store the original xml document that was causing the exception:
@SuppressWarnings("serial")
public class CustomException extends RuntimeException {
private final String source;
public CustomException(Throwable cause, String source) {
super(cause);
this.source = source;
}
public String getSource() { return source; }
}