使Apache Camel有条件地在onException路由上注入标头

时间:2018-11-29 20:23:28

标签: java-8 apache-camel

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>

所以,很简单:

  1. 在快乐的道路上,从AMQ上的input队列中消费,将其(通过XStream)反序列化为Java对象,将其发送到thingProcessor,然后将该处理器的结果放在{ {1}}队列。
  2. 如果发生异常,例如output抛出thingProcessor,我们将异常堆栈跟踪记录到应用程序日志中,然后转换原始XML(我们消耗RuntimeException队列),将其反序列化为POJO,然后将其发送到input进行处理。最后,我们回滚JMS事务。

有时在失败时消息上会出现CamelFilePath header,如果消息头存在,我希望errorProcessor接受并执行特殊的逻辑。

当前我的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队列中消耗的原始(反序列化)XML / POJO。

我的问题

有时,{/ {1}}标头会出现在消息/交换上,有时则不会。如何调整路线,以便 if 存在于“快乐路径”路线上,它将被复制并出现在“错误”路线上(即从{{1}内部}定义)?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以在路线中使用选择和简单子句。
我只在Java dsl上知道,但是要进行xml转换

.choice().when().simple("${header.CamelFilePath} != null && ${header.CamelFilePath} not contains ''").wireTap("bean:errorProcessor");


在xml上是这样的:

<choice>
 <when>
  <simple>
    ${header.CamelFilePath} != null &amp;&amp; ${header.CamelFilePath} not contains ''
  </simple>
  <wireTap uri="bean:errorProcessor" copy="true"/>
 </when>