分裂期间骆驼交换属性丢失

时间:2016-12-22 12:57:10

标签: java apache-camel osgi apache-karaf blueprint

我有以下骆驼路线设置:

<route id="firstRoute">
    <from uri="..." />

    <!-- This processor puts a list of items as the out body -->
    <process ref="collectItemsProcessor" />

    <!-- Now all items should be processed one by one: -->
    <split>
        <simple>${body}</simple>
        <to uri="direct:secondRoute" />
    </split>
</route>

<route id="secondRoute">
    <from uri="direct:secondRoute" />

    <process ref="itemProcessor" />
</route>

itemProcessor我想通过将一个属性放入交易所来计算成功处理的项目数量:

exchange.setProperty("PROCESSED_ITEMS", exchange.getProperty("PROCESSED_ITEMS", Integer.class) + 1);

出于某种原因,在每次调用处理器时,属性再次为null。文档说:

  

Exchange还将整个生命周期中的元数据存储为   可以使用各种getProperty(String)方法访问的属性。

https://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html

最初在collectItemsProcessor中设置属性时,会保留此值。我怀疑每次调用分裂路由时都会复制交换,但是如何在整个生命周期中真正保留“元数据”?

1 个答案:

答案 0 :(得分:1)

Split为每个项目创建一个新的交换。此交换的生命周期仅涵盖拆分元素内部的内容。

如果您只想要处理已处理元素的计数器,那么只需使用属性&#34; CamelSplitIndex&#34;。拆分器会自动填充此属性。

相关问题