[英]How to use Aggregator correctly in apache camel to insert huge data into mongodb
[英]Am I using Apache Camel aggregator correctly?
我對 路由的 理解 (以Apache Camel的語言來說)是它代表了從一個端點到另一端點的數據流,並且它將在對數據執行EIP類型操作的過程中停在各種處理器上。
如果這是對路線的正確/合理的評估,那么我正在對一個問題建模,我認為該問題需要在同一CamelContext
使用多個路線(我使用的是Spring):
List<SomePOJO>
,然后將其發送到聚合器 List<SomePOJO>
,然后將其發送到聚合器 List<SomePOJO>
來自兩個路線1和路線2,然后繼續處理的聚集列表 事情是這樣: 兩個List<SomePOJO>
需要同時到達聚合器 ,或者更確切地說,聚合器bean必須等到從兩條路由接收到數據之后才能將2個列表聚合到一個List<SomePOJO>
然后將匯總列表發送到Route 3的其余部分。
到目前為止,我有以下偽編碼的<camelContext>
:
<camelContext id="my-routes" xmlns="http://camel.apache.org/schema/spring">
<!-- Route 1 -->
<route id="route-1">
<from uri="time://runOnce?repeatCount=1&delay=10" />
<!-- Extracts data from Source 1, processes it, and then produces a List<SomePOJO>. -->
<to uri="bean:extractor1?method=process" />
<!-- Send to aggregator. -->
<to uri="direct:aggregator" />
</route>
<!-- Route 2 -->
<route id="route-2">
<from uri="time://runOnce?repeatCount=1&delay=10" />
<!-- Extracts data from Source 2, processes it, and then produces a List<SomePOJO>. -->
<to uri="bean:extractor2?method=process" />
<!-- Send to aggregator. -->
<to uri="direct:aggregator" />
</route>
<!-- Route 3 -->
<route id="route-3">
<from uri="direct:aggregator" />
<aggregate strategyRef="listAggregatorStrategy">
<correlationExpression>
<!-- Haven't figured this part out yet. -->
</correlationExpression>
<to uri="bean:lastProcessor?method=process" />
</aggregate>
</route>
</camelContext>
<bean id="listAggregatorStrategy" class="com.myapp.ListAggregatorStrategy" />
然后在Java中:
public class ListAggregatorStrategy implements AggregatoryStrategy {
public Exchange aggregate(Exchange exchange) {
List<SomePOJO> route1POJOs = extractRoute1POJOs(exchange);
List<SomePOJO> route2POJOs = extractRoute2POJOs(exchange);
List<SomePOJO> aggregateList = new ArrayList<SomePOJO>(route1POJOs);
aggregateList.addAll(route2POJOs);
return aggregateList;
}
}
direct:aggregator
端點將數據從route-1
和route-2
發送出並發送到route-3
的聚合器中? extractor1
在豆route-1
只需要5秒運行,但extractor2
在豆route-2
需要2分鍾來運行。 在t = 5時,聚合器應從extractor1
接收數據,並開始等待(2分鍾),直到extractor2
2完成並將剩余的數據提供給聚合器。 是? 聽起來您處在正確的軌道上,“ 聚合器”頁面上有很多關於此的良好信息。
該<correlationExpression>
的關鍵是從各航線相匹配的Exchange和completionSize可以指定有多少等待。 在您的情況下,似乎每個路由只能運行一次,在這種情況下,表達式可能會使用來自每個Exchange的固定標頭值,否則,每個路由都需要一個計數器類。
這是您的示例的更新:
<route id="route-1">
<from uri="time://runOnce?repeatCount=1&delay=10" />
<to uri="bean:extractor1?method=process" />
<setHeader headerName="id">
<constant>myHeaderValue</constant>
</setHeader>
<to uri="direct:aggregator" />
</route>
<route id="route-2">
<from uri="time://runOnce?repeatCount=1&delay=10" />
<to uri="bean:extractor2?method=process" />
<setHeader headerName="id">
<constant>myHeaderValue</constant>
</setHeader>
<to uri="direct:aggregator" />
</route>
<route id="route-3">
<from uri="direct:aggregator" />
<aggregate strategyRef="listAggregatorStrategy" completionSize="2">
<correlationExpression>
<simple>header.id</simple>
</correlationExpression>
<to uri="bean:lastProcessor?method=process" />
</aggregate>
</route>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.