Camel 2.1升级至2.17后onException异常处理失效问题咨询
我来帮你排查这个Camel升级后的异常处理问题,结合你的描述和路由配置,我梳理了几个可能的原因和对应的解决方案:
1. OnException的位置不符合新版本规范
在Camel 2.x的新版本(比如2.17)中,路由的onException块必须定义在<from>节点之前,这是官方明确要求的路由结构规范。看你的配置,onException是放在<to>之后的,旧版本可能对顺序要求宽松,但新版本会忽略位置不正确的异常处理块,导致触发异常时不会执行对应的逻辑。
解决方案:调整路由结构,把onException移到<from>之前,修改后的路由如下:
<?xml version="1.0" encoding="ASCII"?> <routes xmlns="http://camel.apache.org/schema/spring"> <route> <!-- 先定义异常处理逻辑 --> <onException> <exception>java.lang.Exception</exception> <exception>webservicequeues.utilities.WebServiceQueueException</exception> <log message="ProcessTaskEx - exception" loggingLevel="DEBUG" logName="WebServiceQueues" /> <redeliveryPolicy maximumRedeliveries="2" redeliveryDelay="60000" maximumRedeliveryDelay="900000" retriesExhaustedLogLevel="INFO" retryAttemptedLogLevel="INFO"/> <handled> <constant>true</constant> </handled> <log message="Failed after Retry.Sending ProcessTask Request to Error Queue" loggingLevel="ERROR" logName="WebServiceQueues" /> <to uri="switchyard://ErrorProcessTaskExQueue"/> </onException> <!-- 再定义路由的输入和业务逻辑 --> <from uri="switchyard://ProcessTaskEx"/> <log message="ProcessTaskEx - message received: ${body}" loggingLevel="DEBUG" logName="WebServiceQueues" /> <to uri="switchyard://RequestCapacity"/> </route> </routes>
2. SOAP Fault未被转换为Java异常
你看到的错误提示camel exchange failed without an exception: <SOAP-ENV:Fault>说明:Camel确实检测到了SOAP Fault,但没有将它包装成对应的Java异常对象。从Camel 2.1到2.17,SwitchYard组件对SOAP Fault的处理逻辑有变化,默认情况下可能不会自动把SOAP Fault转换成Exception或你自定义的WebServiceQueueException,导致onException的异常类型匹配失败。
解决方案:
- 扩展
onException的异常匹配范围,添加SwitchYard或Camel处理SOAP时抛出的特定异常:<onException> <exception>java.lang.Exception</exception> <exception>webservicequeues.utilities.WebServiceQueueException</exception> <!-- 添加SOAP相关异常 --> <exception>org.switchyard.exception.SwitchYardException</exception> <exception>org.apache.camel.component.soap.SoapFault</exception> <!-- 其他处理逻辑保持不变 --> </onException> - 或者自定义处理器,手动检测SOAP Fault并抛出异常:
先在路由中添加处理器节点:
然后实现处理器类:<to uri="switchyard://RequestCapacity"/> <process ref="soapFaultDetectionProcessor"/>import org.apache.camel.Exchange; import org.apache.camel.Processor; import webservicequeues.utilities.WebServiceQueueException; public class SoapFaultDetectionProcessor implements Processor { @Override public void process(Exchange exchange) throws Exception { // 检查Exchange中是否存在SOAP Fault标识 if (exchange.getIn().getHeader("SOAPFault") != null || exchange.getProperty(Exchange.EXCEPTION_CAUGHT) != null) { throw new WebServiceQueueException("Received SOAP Fault from RequestCapacity service"); } } }
3. 辅助排查:开启更详细的日志
你可以在路由中添加日志节点,查看Exchange的异常状态,帮助确认问题:
<log message="Exchange exception: ${exception}" loggingLevel="DEBUG" logName="WebServiceQueues" /> <log message="Exchange fault body: ${fault.body}" loggingLevel="DEBUG" logName="WebServiceQueues" />
这能让你清楚看到Exchange中是否存在异常,以及异常的具体类型,方便进一步调整异常处理规则。
内容的提问来源于stack exchange,提问作者user9868874
相关产品推荐
相关产品推荐

