代码之家  ›  专栏  ›  技术社区  ›  hotmeatballsoup

让ApacheCamel在OnException路由上有条件地注入头文件

  •  2
  • hotmeatballsoup  · 技术社区  · 6 年前

    Java 8 /骆驼2.19。我有以下路由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. 在快乐的道路上,从 input 在AMQ上排队,将它(通过xStand)反序列化为Java对象,将其发送到 thingProcessor ,并将该处理器的结果放在 output 排队。
    2. 如果发生异常,请说 处理器 抛出一个 RuntimeException ,我们将异常stacktrace记录到应用程序日志中,然后转换原始XML(我们在 输入 队列),将其反序列化为POJO,并将其发送到 errorProcessor 为了处理。最后,我们回滚JMS事务。

    有时 CamelFilePath header 失败时将出现在消息中,我希望 错误处理器 接受并执行特殊逻辑(如果存在头)。

    当前我的 错误处理器 看起来像:

    @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 输入 排队。

    我的问题

    有时 骆驼文件路径 消息/交换中会出现标题,有时不会出现。我怎样才能调整我的路线 如果 它存在于“快乐路径”路径上,将被复制并显示在“错误”路径上(即从 <onException> 定义)以及?

    事先谢谢!

    1 回复  |  直到 6 年前
        1
  •  1
  •   Anatoly Utkin    6 年前

    您可以在路由中使用choice和simple子句。
    我只知道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>