gpt4 book ai didi

properties - Camel Exchange 属性不使用 xml 中的简单进行评估

转载 作者:行者123 更新时间:2023-12-02 04:36:16 25 4
gpt4 key购买 nike

我正在尝试在 Exchange.property 上设置属性 isEven,然后使用选择 何时 在路由中评估它。
正在设置该属性,但无论 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>

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

谁能看到我错过了什么?

谢谢,

安德鲁

在 Peter 展示他可以轻松做到之后,我尝试了他的两个我之前没有尝试过的例子。这是一个。它对我不起作用。无论 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 :: **

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

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

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

最佳答案

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

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

我更喜欢最后一个版本。

编辑:

如果属性设置正确,为了调试,启用showProperties在你的 Spring 配置文件中:

<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 之前尝试记录该属性并查看该属性是否仍然设置。如果没有,请查看 bean。

关于properties - Camel Exchange 属性不使用 xml 中的简单进行评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21911816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com