【问题标题】:Camel Exchange property not evaluating with simple in xmlCamel Exchange 属性未使用简单的 xml 进行评估
【发布时间】:2014-02-20 15:12:15
【问题描述】:

我正在尝试在 Exchange.property 上设置一个属性 isEven,然后使用 choice when 在路由中对其进行评估。
正在设置该属性,但无论 isEven 设置为什么,我总是得到 otherwise 结果 (NACK)。

这是我设置的地方:

// Below is used for development
// If the property.isEven == true then an ACK will be returned from the Mock HRM
// If false then NACK

    int lastDigit = Integer.parseInt(exchange.getExchangeId().substring(exchange.getExchangeId().length() - 1));

    // check if lastDigit is odd or even
    if ((lastDigit & 1) == 0)
    {
        exchange.setProperty("isEven", Boolean.TRUE);
        System.out.println("\n\n\n********** Exchange Id lastDigit " + lastDigit + " isEven: " + exchange.getProperty("isEven") + " ***********");

    }
    else
    {
        exchange.setProperty("isEven", Boolean.FALSE);
        System.out.println("\n\n\n********** Exchange Id lastDigit " + lastDigit + " isEven: " + exchange.getProperty("isEven") + " ***********");

    }

我正在设置的 println 显示即使是我所期望的方式。 路线如下:

<!-- Used to ramdomly send Ack or Nack -->
<log message="isEven property is :: ${property[isEven]}" />
    <camel:choice>
        <camel:when>
            <simple>"${property[isEven]}"</simple>
                <transform>
            <constant>ACK</constant>
            </transform>    
        </camel:when>
        <camel:otherwise>
            <transform>
            <constant>NACK</constant>
        </transform>    
        </camel:otherwise>
    </camel:choice>

日志消息从不计算表达式 ${property[isEven]} 这是输出 log[isEven 属性是 :: ${property[isEven]}]

如果我将简单表达式更改为显式检查是否为真,无论属性设置为什么,我总是会收到 ACK。

<simple>"${property[isEven]} == true"</simple>

我在网上搜索过,但找不到很多使用简单和 Exchange 属性的示例。

谁能看到我错过了什么?

谢谢,

安德鲁

在彼得证明他可以轻松做到之后,我尝试了他的两个我还没有尝试过的例子。这是一个。它对我不起作用。无论 isEven 是真还是假,它都会生成 Hello :: NACK:

<camel:choice>
            <camel:when>
                <simple>${property[isEven]} == "true"</simple>
                <log message="HELLO :: ACK" />
                <!-- <transform>
                    <constant>ACK</constant>
                </transform> -->    
            </camel:when>
            <camel:otherwise>
                <log message="HELLO :: NACK" />
                <!-- <transform>
                    <constant>NACK</constant>
                </transform> -->    
            </camel:otherwise>
        </camel:choice>

这里有一些有趣的东西。它看起来像下面的日志说它是空的最后一个喜欢

********** Exchange Id lastDigit 2 isEven: true ***********

14/02/20 14:09:13 INFO interceptor.Tracer: >>> (toHRMRoute) bean://hl7handler?method=handleORM --> log[isEven property is :: ${property[isEven]}] <<< Pattern:InOut, Properties {CamelToEndpoint=bean://hl7handler?method=handleORM, CamelMessageHistory [DefaultMessageHistory[routeId=toHRMRoute, node=to3], DefaultMessage History[routeId=toHRMRoute, node=log1]], CamelCreatedTimestamp=Thu Feb 20 14:09:13 CST 2014}

14/02/20 14:09:13 INFO toHRMRoute:  ** isEven property is :: **

我认为彼得是正确的,因为它必须是我设置路线的方式。

<endpoint id="hrmMockHL7Listener"
        uri="netty:tcp://localhost:9200?sync=true" />
<!-- Sending data using postman to a rest server-->
<route id="pushRESTRoute">
<from uri="cxfrs://bean://pushRESTServer" />

    <!-- this process is where we set isEven on the Exchange-->
<process ref="transformer"/>
    <!-- Send it to a tcp listener at port 9200-->
<to ref="hrmMockHL7Listener" /> 
</route>
<!-- Changed routes does the Exchange keep properties? -->
<route id="toMRoute">
<from uri="hrmMockHL7Listener" />
<to uri="bean:hl7handler?method=handleORM" />
<!-- Used to ramdomly send Ack or Nack -->
<log message="isEven property is :: ${property[isEven]}">
    // see the beginning of the question for choice code.

查看输出,似乎 isEven 属性在路由之间被删除:

14/02/21 09:37:26 INFO interceptor.Tracer: >>> (pushRESTRoute) ref:transformer --> tcp://localhost:9200 <<< Pattern:InOut, Properties {CamelMessageHistory=[DefaultMessageHistory[routeId=pushRESTRoute, node=process1], DefaultMessageHistory[routeId=pushRESTRoute, node=to1]], CamelCreatedTimestamp=Fri Feb 21 09:37:26 CST 2014, isEven=true}    

看到最后的 isEven 了吗?下一个来的示踪剂没有它

/02/21 09:37:26 INFO interceptor.Tracer: >>> (toMRoute) from(tcp://localhost:9200) --> bean://hl7handler?method=handleORM <<< Pattern:InOut, Properties:{CamellMessageHistory=[DefaultMessageHistory[routeId=toMRoute, node=to3]], CamelCreatedTimestamp=Fri Feb 21 09:37:26 CST 2014}

来自 Exchange javadoc

An Exchange is the message container holding the information during the entire routing of a Message received by a Consumer.

整个是否包括跨不同的路线?

【问题讨论】:

  • 你用的是什么版本的骆驼?

标签: properties apache-camel


【解决方案1】:

为了测试,我稍微改变了路线:

<route id="startRoute">
    <from uri="direct:start" />
    <multicast stopOnException="true">
        <to uri="direct:trigger" />
        <to uri="direct:trigger" />
        <to uri="direct:trigger" />
    </multicast>
</route>

<route>
    <from uri="direct:trigger" />
    <process ref="myProcessor" />
    <log message="isEven property is :: ${property[isEven]}" />
    <camel:choice>
        <camel:when>
            <simple>"${property.isEven}"</simple> 
            <log message="HELLO :: ACK" />
        </camel:when>
        <camel:otherwise>
            <log message="HELLO :: NACK" />
        </camel:otherwise>
    </camel:choice>
</route>

<!-- scope singleton is default -->
<bean id="myProcessor" class="ch.keller.test.testcamelspring.util.Trigger"  scope="singleton" />

处理器定义如下:

public class Trigger implements Processor {
    @Override
    public void process(final Exchange exchange) throws Exception {
        // your code comes here
    }
}

对我来说,以下表达式按预期工作:

<simple>"${property[isEven]}"</simple> 

<simple>${property[isEven]}</simple>

<simple>${property[isEven]} == "true"</simple> 

<simple>"${property.isEven}"</simple>

<simple>${property.isEven} == "true"</simple> 

<simple>${property.isEven}</simple>

我更喜欢最后一个版本。

编辑:

为了调试是否正确设置了属性,请在您的 Spring 配置文件中启用showProperties

<bean id="traceFormatter" class="org.apache.camel.processor.interceptor.DefaultTraceFormatter">
    <property name="showBreadCrumb" value="false" />
    <property name="showProperties" value="true" />
</bean>

然后您应该会在日志中看到以下输出(缩短以提高可读性):

[main] Tracer INFO  >>> (route1) log[isEven property is :: ${property[isEven]}] --> choice <<< Pattern:InOnly, Properties:{CamelToEndpoint=direct://trigger, ..., isEven=true, ...

重要的部分是isEven=true

编辑:

该属性在转发到另一个路由时被保留,可以证明如下:

<route>
    <from uri="direct:trigger" />
    <process ref="myProcessor" />
    <log message="isEven property is :: ${property[isEven]}" />
    <to uri="direct:acktarget" />
</route>

<route>
    <from uri="direct:acktarget" />
    <log message="acktarget: isEven property is :: ${property[isEven]}" />
</route>

输出:

exchange.getExchangeId() = ID-pis-iMac-local-54434-1393006139076-0-3
********** Exchange Id lastDigit 3 isEven: false ***********
[                          main] route1                         INFO  isEven property is :: false
[                          main] route2                         INFO  acktarget: isEven property is :: false
exchange.getExchangeId() = ID-pis-iMac-local-54434-1393006139076-0-4
********** Exchange Id lastDigit 4 isEven: true ***********
[                          main] route1                         INFO  isEven property is :: true
[                          main] route2                         INFO  acktarget: isEven property is :: true
exchange.getExchangeId() = ID-pis-iMac-local-54434-1393006139076-0-5
********** Exchange Id lastDigit 5 isEven: false ***********
[                          main] route1                         INFO  isEven property is :: false
[                          main] route2                         INFO  acktarget: isEven property     is :: false

即使我在将消息转发到其他路由之前调用了一个 bean,该属性也会保留。所以,我猜你的问题出在&lt;to uri="bean:hl7handler?method=handleORM" /&gt;。尝试在调用此 bean 之前记录该属性并查看该属性是否仍然设置。如果没有,请查看 bean。

【讨论】:

  • 哇。现在你只是让我看起来很糟糕。 ;-) 我会输入这些日志并尝试一些您的成功示例。
  • 彼得你用 Boolean.TRUE & Boolean.False 设置它吗?如果您的简单表达式对我不起作用,也许是我设置它们的方式?
  • 我实际上使用了你的处理器。但也许它与你如何调用你的路由(见我上面的增强示例)有关
  • +1 表示多播示例。我仔细检查了我没有指定单例或原型,所以我默认使用单例。您是否看到我在问题中添加了日志消息显示 isEven 为空的详细信息。日志消息以 INFO 开头。
  • 我在上面的答案中添加了如果属性设置正确,如何调试。
【解决方案2】:

您不需要使用引号。试试吧

<simple>${property.isEven} == true</simple>

【讨论】:

  • 我就是这样开始的。我改回来了。确保我保存并重新编译。 :) 并再次尝试。没运气。我每次都得到 NACK(否则)。为了记录,isEven 每次都会改变。 lastDigit 从 2 开始,然后是 7,然后重复。
【解决方案3】:

对于较新版本的 apache camel,请使用 exchangeProperty 而不是 property

${exchangeProperty.isEven}

简单语言属性函数在 Camel 2.x 中已弃用并已被删除。使用 exchangeProperty 作为函数名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 2015-07-30
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多