【发布时间】: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.
整个是否包括跨不同的路线?
【问题讨论】:
-
你用的是什么版本的骆驼?