Camel Exchange属性未在xml中进行简单评估

时间:2014-02-20 15:12:16

标签: properties apache-camel

我正在尝试在Exchange.property上设置属性isEven,然后使用选项 时在路径中对其进行评估。
  该属性正在设置中,但无论isEven设置为什么,我总是得到否则结果(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]}]

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

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

我搜索了网页但是找不到使用简单和Exchange属性的很多示例。

有人能看到我错过的东西吗?

谢谢,

安德鲁

彼得表示他可以轻松地做到这一点后,我试了两个我没试过的例子。这是一个。它对我不起作用。它生成了Hello :: NACK,无论isEven是true还是false:

<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.

整个是否包含不同的路线?

2 个答案:

答案 0 :(得分:2)

为了测试我改变了路线:

<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,也会保留该属性。所以,我想你的问题出在<to uri="bean:hl7handler?method=handleORM" />。尝试在调用此bean之前记录该属性,并查看该属性是否仍然设置。如果没有,看看豆。

答案 1 :(得分:0)

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

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