【发布时间】:2018-12-04 23:25:07
【问题描述】:
Java 8/Camel 2.19.x 在这里。我有以下路由 XML:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:spring="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring-2.0.0.xsd"
>
<routeContext id="myRoute" xmlns="http://camel.apache.org/schema/spring">
<route id="doStuff">
<from uri="activemq:input"/>
<onException useOriginalMessage="true">
<exception>java.lang.Exception</exception>
<redeliveryPolicy logStackTrace="true"/>
<handled>
<constant>true</constant>
</handled>
<log message="${exception.stacktrace}" loggingLevel="ERROR"/>
<!-- we get the original XML message - convert it to an object -->
<unmarshal ref="xstream"/>
<wireTap uri="bean:errorProcessor" copy="true"/>
<rollback markRollbackOnly="true"/>
</onException>
<transacted ref="shared"/>
<doTry>
<unmarshal ref="xstream"/>
<to uri="bean:thingProcessor"/>
<marshal ref="xstream"/>
<to uri="activemq:output"/>
</doTry>
</route>
</routeContext>
</beans>
所以,很简单:
- 在愉快的路径上,从 AMQ 上的
input队列消费,将其反序列化(通过 XStream)为 Java 对象,将其发送到thingProcessor,并将该处理器的结果放在output队列中。 - 如果发生异常,比如
thingProcessor抛出RuntimeException,我们将异常堆栈跟踪记录到应用程序日志中,然后我们转换原始 XML(我们从input队列中消耗),将其反序列化为一个 POJO,发给errorProcessor处理。最后我们回滚 JMS 事务。
有时CamelFilePath header 会在失败时出现在消息上,我希望errorProcessor 接受这一点并在标头存在时执行特殊逻辑。
目前我的errorProcessor 看起来像:
@Component("errorProcessor")
public class ErrorProcessor {
private static final Logger log = LoggerFactory.getLogger(ErrorProcessor.class);
private final ErrorHelper errorHelper;
public ErrorProcessor(final ErrorHelper errorHelper) {
this.errorHelper = errorHelper;
}
public void handleErrors(
final Fizzbuzz fizzbuzz,
@Header("CamelFilePath") final String camelFilePath,
@ExchangeProperty(Exchange.EXCEPTION_CAUGHT) final Exception exception) {
// If camelFilePath is non-null and non-empty, do stuff with it here.
}
}
在上面,fizzbuzz 是从input 队列中消耗的原始(反序列化)XML/POJO。
我的问题
有时CamelFilePath 标头会出现在消息/交换中,有时则不会。如何调整我的路线,以便 if 它存在于“快乐路径”路线上,它将被复制并出现在“错误”路线上(即,从 <onException> 定义内部) 也一样?
提前致谢!
【问题讨论】:
标签: java-8 apache-camel