【问题标题】:Getting Apache Camel to conditionally inject headers on the onException route让 Apache Camel 有条件地在 onException 路由上注入标头
【发布时间】: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>

所以,很简单:

  1. 在愉快的路径上,从 AMQ 上的 input 队列消费,将其反序列化(通过 XStream)为 Java 对象,将其发送到 thingProcessor,并将该处理器的结果放在 output 队列中。
  2. 如果发生异常,比如 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 它存在于“快乐路径”路线上,它将被复制并出现在“错误”路线上(即,从 &lt;onException&gt; 定义内部) 也一样?

提前致谢!

【问题讨论】:

    标签: java-8 apache-camel


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2012-12-21
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多