差異處

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

連向這個比對檢視

java:apache_camel:aggregator:helloworld [2019/03/17 21:20]
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;​ 
-  
- public AggregatorGroupRouteBuilder(Object eventHandler) { 
- this.eventHandler = eventHandler;​ 
- 
- } 
-  
- @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(500) 
- .bean(eventHandler).endRest();​ 
- } 
-} 
-</​code>​ 
- 
- 
- 
-===== Reference ===== 
-  * [[http://​camel.apache.org/​aggregator.html|Camel - Aggregator]] 
- 
-=====    ===== 
----- 
-\\ 
-~~DISQUS~~