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

相关文章
相关标签/搜索