Hi I have an spring integration flow where my message traverse through multiple Deserialization, transformation and serialization process.
I am unable to handle any exception that are thrown during the process. I have defined my error channel as below. How to interrupt the integration flow and send the message to error channel? ================ error channel ===============
@Bean("errorChannel")
public MessageChannel errorChannel() {
return new DirectChannel();
}
@Bean
@Router(inputChannel = "errorChannel")
public ErrorMessageExceptionTypeRouter handleError(){
ErrorMessageExceptionTypeRouter router = new ErrorMessageExceptionTypeRouter();
Map<String, String> mappings = new HashMap<>();
mappings.put(RuntimeException.class.getName(), "someChannel");
mappings.put(Exception.class.getName(), "someChannel");
router.setChannelMappings(mappings);
return router;
}
@ServiceActivator(inputChannel = "someChannel")
public void handleErrors(Message<?> message) {
LOGGER.info(" error caught in some channel {}", message);
}
============== error emitting channel ====================
@Bean
@ServiceActivator(inputChannel = "JSON_to_IM_in_Process")
public AbstractReplyProducingMessageHandler json_to_im_svc() throws Exception {
AbstractReplyProducingMessageHandler mh = new AbstractReplyProducingMessageHandler () {
@Override
public Message<?> handleRequestMessage(Message<?> inputMessage) {
String inputRecord = inputMessage.getPayload().toString();
Map<String, Object> newheader = new HashMap<String, Object>();
InputModel result = null;
******if(true) throw new RuntimeException("forcefully Exception ");******
try {
ObjectMapper mapper = new ObjectMapper();
result = mapper.readValue(inputRecord, InputModel.class);
newheader.put("Exception_Status", "Passed");
Message<?> outMsg = MessageBuilder.withPayload(result).copyHeaders(inputMessage.getHeaders())
.copyHeaders(newheader).build();
LOGGER.info("Deserialized to Input Model -------------->" + outMsg);
return outMsg;
} catch (Exception e) {
newheader.put("Exception_Status", "Failed");
String excep = "Exception in translating inputFormat to Object";
Message<?> outMsg = MessageBuilder.withPayload(excep).copyHeaders(inputMessage.getHeaders())
.copyHeaders(newheader).build();
LOGGER.info("Exception in translating inputFormat to Object", e.getMessage());
return outMsg;
}
}
I want to catch the exception in the errorChannel but unable to do so. I want to break the integration flow from the exception I want to execute "someChannel" and stop and send to the outboundRequest channel. If the exception is inside the try{} block it is handled by the catch block. If the exception is outside the try block the stack trace is coming directly on the console.
What I need to do more to catch the exceptiopn in the errorChannel ?