Camel In Action 读书笔记 (5)

原创
2013/06/09 11:35
阅读数 802

<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>

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部