Spring Integration中如何将流程异常消息发送至自定义errorChannel?
问题描述
我有一个Spring Integration流程,消息会经过反序列化、转换和序列化等多个处理环节。目前无法捕获流程中抛出的异常,已定义了自定义errorChannel及相关处理逻辑,却无法将异常消息发送至该通道并中断流程。
我希望异常能被errorChannel捕获,中断原集成流程,将异常消息发送至errorChannel,执行someChannel的处理逻辑并终止流程。当前若异常在try块内会被catch处理,若在try块外则直接在控制台打印堆栈信息,请问还需进行哪些配置才能让异常进入errorChannel?
错误通道配置
@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); }
异常触发通道配置
@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; } }
解决方案
要让异常正确进入自定义errorChannel并中断流程,需做以下调整:
移除try-catch块,允许异常向上抛出
当前代码在try块内捕获所有异常并返回普通消息,Spring Integration会判定流程正常完成,不会触发错误通道。若要异常进入errorChannel,需移除catch块,让异常直接抛出:@Override public Message<?> handleRequestMessage(Message<?> inputMessage) { String inputRecord = inputMessage.getPayload().toString(); Map<String, String> newheader = new HashMap<>(); InputModel result = null; 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; }为消息处理器指定错误通道
在@ServiceActivator注解中添加errorChannel属性,明确将该处理器抛出的异常导向自定义错误通道:@Bean @ServiceActivator(inputChannel = "JSON_to_IM_in_Process", errorChannel = "errorChannel") public AbstractReplyProducingMessageHandler json_to_im_svc() throws Exception { // 处理器逻辑... }全局默认错误通道配置(可选)
若希望整个Spring Integration流程默认使用自定义错误通道,可在application.properties中添加配置:spring.integration.default-error-channel=errorChannel确保流程中断
你的someChannel处理器为无返回值方法,会自动终止流程;若后续处理器有返回值,需确保不向原流程返回消息,避免流程继续执行。
内容的提问来源于stack exchange,提问作者Santrupta Dash
相关产品推荐
相关产品推荐

