Camel In Action 讀書筆記 (3)

<p>在第二章開始講Camel的路由配置以及路由經常使用模式。</p> <h3>Camel的路由配置有三種方式:</h3> <p>1.java DSL (純java方式)建立RouteBuilder</p> <p><a href="http://static.oschina.net/uploads/img/201306/02141322_TlsL.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201306/02141325_BcWq.png" width="414" height="224" /></a></p> <p>2.Spring DSL(純spring方式) </p> <p><a href="http://static.oschina.net/uploads/img/201306/02141326_P36Q.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201306/02141327_epoH.png" width="439" height="123" /></a></p> <p>3.定義bean的形式(Java+Spring)</p> <p><a href="http://static.oschina.net/uploads/img/201306/02141327_AfZO.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201306/02141328_Cb1c.png" width="434" height="73" /></a></p> <h3>路由經常使用模式</h3> <h4><em>1.Using a content-based router(基於內容的路由)</em></h4> <p><a href="http://static.oschina.net/uploads/img/201306/02141329_WHi6.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201306/02141331_q8JC.png" width="547" height="254" /></a></p> <p>如上圖:路由到下一個節點時須要根據文件類型來判斷是xmlOrders仍是csvOrdersks能夠經過choice…..when的形式來定義</p> <p>headerCamel的表達式經常使用的有constant,simple,header,body.</p> <p><em>from(&quot;jms:incomingOrders&quot;)&#160;&#160;&#160;&#160;&#160;&#160; <br />.<font color="#ff0000">choice()</font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160; <font color="#ff0000">.when</font>(header(&quot;CamelFileName&quot;) <br />&#160;&#160;&#160; .endsWith(&quot;.xml&quot;))&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; .to(&quot;jms:xmlOrders&quot;)&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160; <font color="#ff0000">.when</font>(header(&quot;CamelFileName&quot;) <br />&#160;&#160;&#160; .endsWith(&quot;.csv&quot;))&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; .to(&quot;jms:csvOrders&quot;);</em>&#160;&#160;&#160; <br /></p> <p>&#160;</p> <h4><em>2.Using message filters(消息過濾)</em></h4> <p><a href="http://static.oschina.net/uploads/img/201306/02141331_5dM8.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201306/02141333_pQrg.png" width="496" height="159" /></a></p> <p>如上圖:若是消息(xml)order節點包含test屬性則自動過濾掉。經過filter處理,若是是xml形式的消息可用xpath來解析消息</p> <p><em>&lt;route&gt; <br />&#160; &lt;from uri=&quot;jms:xmlOrders&quot;/&gt; <br />&#160; <font color="#ff0000">&lt;filter&gt; <br /></font>&#160;&#160;&#160; &lt;xpath&gt;/order[not(@test)]&lt;/xpath&gt; <br />&#160;&#160;&#160; &lt;process ref=&quot;orderLogger&quot;/&gt; <br /><font color="#ff0000">&#160; &lt;/filter&gt; <br /></font>&lt;/route&gt;</em></p> <h4><em>3.Using multicasting(多點廣播)</em></h4> <p><a href="http://static.oschina.net/uploads/img/201306/02141334_DNiE.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201306/02141335_FVRt.png" width="483" height="164" /></a></p> <p>如上圖,xmlOrders收到的消息要同時發送到accounting和production兩個隊列中去。經過multicast()來實現。</p> <p><em>ExecutorService executor = Executors.newFixedThreadPool(16); <br />from(&quot;jms:xmlOrders&quot;) <br />&#160;&#160; <font color="#ff0000"> .multicast().</font>parallelProcessing().executorService(executor) <br />&#160;&#160;&#160; .to(&quot;jms:accounting&quot;, &quot;jms:production&quot;);</em></p> <p>注:能夠不指定線程池,系統默認會建立一個線程池大小 爲10:</p> <p><em>from(&quot;jms:xmlOrders&quot;) <br />&#160;&#160;&#160; .multicast().parallelProcessing() <br />&#160;&#160;&#160; .to(&quot;jms:accounting&quot;, &quot;jms:production&quot;);</em></p> <h4><em>4.Using recipient lists(收件人列表模式)</em></h4> <p><a href="http://static.oschina.net/uploads/img/201306/02141336_P3Wu.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201306/02141336_hjv7.png" width="457" height="200" /></a></p> <p>如上圖,根據收件人列表來決定發送到哪些消息隊列中去。</p> <p><em>from(&quot;jms:xmlOrders&quot;) <br />.setHeader(&quot;customer&quot;, xpath(&quot;/order/@customer&quot;)) <br />.process(new Processor() { <br />&#160;&#160;&#160; public void process(Exchange exchange) throws Exception { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; String recipients = &quot;jms:accounting&quot;; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; String customer = <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; exchange.getIn().getHeader(&quot;customer&quot;, String.class); <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (customer.equals(&quot;honda&quot;)) { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; recipients += &quot;,jms:production&quot;; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; exchange.getIn().setHeader(&quot;recipients&quot;, recipients); <br />&#160;&#160;&#160; } <br />}) <br />.recipientList(header(&quot;recipients&quot;));</em></p> <p><strong>recipientList也支持註解的形式:</strong></p> <p><em>from(&quot;jms:xmlOrders&quot;).bean(RecipientListBean.class);</em></p> <p><em>public class RecipientListBean { <br />&#160;&#160;&#160; <font color="#ff0000">@RecipientList <br /></font>&#160;&#160;&#160; public String[] route(@XPath(&quot;/order/@customer&quot;) String customer) { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (isGoldCustomer(customer)) { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return new String[] {&quot;jms:accounting&quot;, &quot;jms:production&quot;}; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } else { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return new String[] {&quot;jms:accounting&quot;}; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } <br />&#160;&#160;&#160; }</em></p> <p><em>&#160;&#160;&#160; private boolean isGoldCustomer(String customer) { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; return customer.equals(&quot;honda&quot;); <br />&#160;&#160;&#160; } <br />}</em></p> <h4><em>6.Using the wireTap method(消息監聽,複製)</em></h4> <p><a href="http://static.oschina.net/uploads/img/201306/02141337_3IY8.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201306/02141338_SxxA.png" width="408" height="196" /></a></p> <p>如上圖,在消息送到目的地的過程當中對消息複製一份送到Tapdestination.</p> <p><em>from(&quot;jms:incomingOrders&quot;) <br />.wireTap(&quot;jms:orderAudit&quot;) <br />.choice() <br />&#160;&#160;&#160; .when(header(&quot;CamelFileName&quot;).endsWith(&quot;.xml&quot;)) <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; .to(&quot;jms:xmlOrders&quot;) <br />&#160;&#160;&#160; .when(header(&quot;CamelFileName&quot;).regex(&quot;^.*(csv|csl)$&quot;)) <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; .to(&quot;jms:csvOrders&quot;) <br />&#160;&#160;&#160; .otherwise() <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; .to(&quot;jms:badOrders&quot;);</em></p>java

相關文章
相關標籤/搜索