差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

java:apache_camel:aggregator:helloworld [2019/03/17 21:42]
tony [AggregatorGroupRouteBuilder]
java:apache_camel:aggregator:helloworld [2023/06/25 09:48]
行 1: 行 1:
-====== Camel - Aggregator Hello World ====== 
-===== Introduction ===== 
-Camel Aggregator提供開發人員能夠將大量訊息合成一個的功能。假設你的系統會接受外來通知,並將內容寫進資料庫中;一次連線寫一筆資料會比較快,還是透過一次連線寫入五筆資料快呢?​ 正常來說,一次連線寫入五筆同屬性資料會是比較快的。它與[[java:​apache_camel:​throttler:​helloworld|Throttler]]的最大區別在於,Throttler限制了client的請求,而Aggreator則是收集起來一次請求。因此,如果使用Aggreator,你的程式必須要有對應的處理方式。\\ 
-\\ 
-我將透過HTTP GET請求/​events/​{id}做為範例,說明如何使用Aggregator去將單位時間內的請求,以{id}去分組並Aggredate成各別分組訊息。本篇文章中使用到兩個RouteBuilder,分別為RestRouteBuilder與AggregatorGroupRouteBuilder。RestRouteBuilder負責REST核心相關設定,可參考[[java:​apache_camel:​throttler:​helloworld|先前文章]]。接下來直接說明AggregatorGroupRouteBuilder。 
-===== AggregatorGroupRouteBuilder ===== 
-<code java> 
-package org.tonylin.practice.camel.aggregator;​ 
  
-import static com.google.common.base.Preconditions.checkState;​ 
- 
-import org.apache.camel.builder.RouteBuilder;​ 
-import org.apache.camel.processor.aggregate.GroupedExchangeAggregationStrategy;​ 
- 
-public class AggregatorGroupRouteBuilder extends RouteBuilder { 
- 
- private final static String GET_EVENTS = "​GET_EVENTS";​ 
-  
- private Object eventHandler;​ 
- private int period = 500; 
-  
- public AggregatorGroupRouteBuilder(Object eventHandler) { 
- this.eventHandler = eventHandler;​ 
- 
- } 
-  
- public void setPeriod(int period) { 
- this.period = period; 
- } 
-  
- @Override 
- public void configure() throws Exception { 
- checkState(eventHandler!=null,​ "​Can'​t find eventHandler"​);​ 
-  
- rest("/​events/​{id}"​).get().route().id(GET_EVENTS) 
- .aggregate(new GroupedExchangeAggregationStrategy()) 
- .header("​id"​) 
- .completionInterval(period) 
- .bean(eventHandler).endRest();​ 
- } 
-} 
-</​code>​ 
-以下是在configure中的幾個重點:​ 
-  * aggregate(new GroupedExchangeAggregationStrategy()):​ 使用GroupedExchangeAggregationStrategy去做aggregate,aggregate的內容為Exchange。如果有任何條件限制,要以Exchange去操作。 
-  * header("​id"​):​ 與aggregate一起使用,代表以header id去分組,在這裡就是屬於event的id。 
-  * completionInterval(period):​ 做aggregate的單位時間。 
-  * bean(eventHandler):​ 請求的處理者,必須要有能力處理aggregate後的訊息。 
- 
- 
- 
-===== Reference ===== 
-  * [[http://​camel.apache.org/​aggregator.html|Camel - Aggregator]] 
- 
-=====    ===== 
----- 
-\\ 
-~~DISQUS~~