Camel In Action 讀書筆記 (5)

<p>接下來講說第四章,</p> <p>第四章介紹如何在Camel中使用普通bean,關於bean的使用方式前面已有介紹:javaDSL和SpringDSL兩種方式。</p> <p>下面講下bean的Camel中的使用模式。</p> <h3><em>The Service Activator pattern</em></h3> <p>翻譯過來喂服務激活模式,感受有點彆扭,說成服務代理模式貌似更好些理解些。以下圖:</p> <p><a href="http://static.oschina.net/uploads/img/201306/09113459_SJBm.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201306/09113459_fOtB.png" width="591" height="158" /></a> </p> <p>service是一個POJO,service activator 做爲Camel的一箇中間代理,接受請求request,並調用service.</p> <h3><em>Camel’s bean registries</em></h3> <p>Camel對bean的管理採用註冊(registry)的方式,下圖展現了registry在spring下的工做方式。</p> <p><a href="http://static.oschina.net/uploads/img/201306/09113459_K9IG.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201306/09113459_mI2E.png" width="551" height="159" /></a> </p> <p></p> <p>Requester須要查找某個bean時,由Camel Registry去經過真正的registry(Spring 的ApplicationContext)去查找對應的bean.</p> <p>Camel提供了以下registry:</p> <p>&#160;</p> <table border="0" cellspacing="0" cellpadding="0"><tbody> <tr> <td width="234">Registry</td> <td width="523">Description</td> </tr> <tr> <td>SimpleRegistry</td> <td>是Reistry的一個簡單實現,通常早測試,GAE,或者不多代碼的狀況下。</td> </tr> <tr> <td>JndiRegistry</td> <td>查找JNDI中的bean.</td> </tr> <tr> <td>ApplicationContextRegistry</td> <td>查找Spring中的bean.</td> </tr> <tr> <td>OsgiServiceRegistry</td> <td>查找Osgi中的service.</td> </tr> </tbody></table> <p>&#160;</p> <h3><em>Selecting bean methods</em></h3> <p>對於一個bean若是有多個方法時,調用bean的方法時,Camel有一套複雜的算法以下圖。</p> <p>&#160;</p> <p><a href="http://static.oschina.net/uploads/img/201306/09113459_ro7b.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201306/09113459_NOn7.png" width="532" height="473" /></a> </p> <p><a href="http://static.oschina.net/uploads/img/201306/09113500_ofcD.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201306/09113500_ZHm4.png" width="533" height="658" /></a> </p> <p>&#160;</p> <h3><em>Binding with multiple parameters</em></h3> <p>最後一節講bean的參數綁定。</p> <p>綁定有以下兩種形式:</p> <p><em><strong>1.Binding using built-in types(類型綁定)</strong></em></p> <p>Camel對Exchange/Message/CamelContext/TypeConverter/Registry/Exception 提供了類型綁定的功能。若是咱們的方法中有這些類型的參數,則會自動綁定相應對象</p> <p>示例以下:</p> <p><em>public string echo(String echo, Registry registry) { <br />OtherBean other = registry.lookup(&quot;other&quot;, OtherBean.class); <br />... <br />}</em></p> <p><em><strong>2.Binding using Camel annotations (註解綁定)</strong></em></p> <table border="0" cellspacing="0" cellpadding="2" width="862"><tbody> <tr> <td valign="top" width="147">Annotation</td> <td valign="top" width="713">Description</td> </tr> <tr> <td valign="top" width="147">@Attachments</td> <td valign="top" width="713">綁定消息的附屬信息,類型必須是Map</td> </tr> <tr> <td valign="top" width="147">@Body</td> <td valign="top" width="713">綁定消息body.</td> </tr> <tr> <td valign="top" width="147">@Header(name)</td> <td valign="top" width="713">綁定消息header中的數據</td> </tr> <tr> <td valign="top" width="147">@Headers</td> <td valign="top" width="713">綁定消息header,類型必須是Map</td> </tr> <tr> <td valign="top" width="147">@OutHeaders</td> <td valign="top" width="713">綁定返回消息header,類型必須是Map</td> </tr> <tr> <td valign="top" width="147">@Property(name)</td> <td valign="top" width="713">綁定exchange的property中對應name的值。</td> </tr> <tr> <td valign="top" width="147">@Properties</td> <td valign="top" width="713">綁定exchange的property,類型必須是Map</td> </tr> </tbody></table> <p></p> <p>示例以下:</p> <p><em>public String orderStatus(@Body Integer orderId, @OutHeaders Map headers) { <br />... <br />headers.put(&quot;status&quot;, &quot;APPROVED&quot;); <br />headers.put(&quot;confirmId&quot;, &quot;444556&quot;); <br />return &quot;OK&quot;; <br />}</em></p> <p>Camel還提供了利用其餘語言的方式,好比XPath,JavaScript,XQuery等等。詳見手冊。</p> <p>示例以下:</p> <p><em>public Document handleIncomingOrder(@Body Document xml, <br />@XPath(&quot;/order/@customerId&quot;) int customerId, <br />@Bean(ref = &quot;guid&quot;, method=&quot;generate&quot;) int orderId);</em></p>java

相關文章
相關標籤/搜索