Camel Exchange属性未使用xml中的simple进行评估

oymdgrw7  于 2022-11-07  发布在  Apache
关注(0)|答案(3)|浏览(202)

我尝试在www.example.com上设置一个属性isEvenExchange.property,然后在路由中使用choice****when对其求值。
正在设置属性,但无论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]}以下是输出日志[isEven属性为::${属性[为偶数]}]
如果我改变这个简单的表达式来显式地检查是否为真,我总是得到ACK,不管属性设置为什么。

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

我在网上搜索过,但没有找到很多使用简单和交换属性的例子。
有人知道我错过了什么吗?
谢谢你,
安德鲁
在Peter展示了他可以很容易地做到这一点后,我尝试了他的两个例子,我还没有试过。这里有一个。它对我不起作用。它生成了Hello::无论isEven为true还是false,均会发出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}

看到最后的偶数了吗?下一个追踪器没有它

/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 Java文档

An Exchange is the message container holding the information during the entire routing of a Message received by a Consumer.
  • entire* 是否包括跨不同路线?
0sgqnhkj

0sgqnhkj1#

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

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

r8uurelv

r8uurelv2#

对于较新版本的apache camel,请使用exchangeProperty而不是property
${交换属性.isEven}
Camel 2.x中不推荐使用简单语言属性函数,该函数已被删除。请使用exchangeProperty作为函数名称。

brgchamk

brgchamk3#

您不需要使用引号。请尝试

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

相关问题