Camel In Action 读书笔记 (4)

<p>第三章主要讲数据类型转换。</p> <p>数据类型转换有两种形式的转换:</p> <p>Data format transformation ------&#160; 数据格式的转换,好比:csv文件转换成xml.</p> <p>Data type transformation&#160;&#160;&#160; ------&#160; 数据类型的转换,好比:String转换成TextMessage.</p> <p>Camel提供的消息转换有如下几种方式:</p> <table border="0" cellspacing="0" cellpadding="2" width="550"><tbody> <tr> <td valign="top" width="181">Transformation</td> <td valign="top" width="367">Description</td> </tr> <tr> <td valign="top" width="181">在路由中转换</td> <td valign="top" width="367">经过MessageTranslator 或者EnricherEIPS转换</td> </tr> <tr> <td valign="top" width="181">经过components转换</td> <td valign="top" width="367">好比XSLT组件用来转换XML.</td> </tr> <tr> <td valign="top" width="181">经过data formats转换</td> <td valign="top" width="367">&#160;</td> </tr> <tr> <td valign="top" width="181">经过模板转换</td> <td valign="top" width="367">好比Apache Velocity</td> </tr> <tr> <td valign="top" width="181"> <p>Camel’s typeconverter <br />mechanism</p>&#160; 转换</td> <td valign="top" width="367">序列化,反序列化</td> </tr> </tbody></table> <p>具体以下:</p> <h3><em>Using the Message Translator EIP</em></h3> <p><a href="http://static.oschina.net/uploads/img/201306/08160631_wb37.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/08160632_DDu8.png" width="461" height="113" /></a> </p> <p>Camel提供了三种方式:</p> <p>■ Using a Processor</p> <p>Processor 接口提供了方法:public void process(Exchange exchange) throws Exception; <br />■ Using beans</p> <p>经过自定义bean转换 <br />■ Using &lt;transform&gt;</p> <p>transform是DSL中的一个方法,</p> <p><em>from(&quot;direct:start&quot;) <br />.transform(body().regexReplaceAll(&quot;\n&quot;, &quot;&lt;br/&gt;&quot;)) <br />.to(&quot;mock:result&quot;);</em></p> <p>&#160;</p> <h3><em>Using the Content Enricher EIP</em></h3> <p>Enricher EIP是说作消息转换时,消息自己数据时不够的还须要请求另外一个资源以下图:</p> <p><a href="http://static.oschina.net/uploads/img/201306/08160632_uXNZ.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/08160632_weHR.png" width="490" height="152" /></a> </p> <p>Enricher EIP 有两种模式:</p> <p>pollEnrich -- 做为一个消费者来获取第三方消息来转换消息 好比 JMS,文件系统。</p> <p>enrich&#160;&#160;&#160;&#160;&#160; -- 做为一个生产者数据发送到第三方接受返回来转换消息。好比 HTTP,WebService。</p> <p>这两种模式都要实现AggregationStrategy接口。</p> <p><em>from(&quot;quartz://report?cron=0+0+6+*+*+?&quot;) <br />.to(&quot;</em><a href="http://riders.com/orders/cmd=received&quot;)"><em>http://riders.com/orders/cmd=received&quot;)</em></a> <br /><em>.process(new OrderToCSVProcessor()) <br />.pollEnrich(&quot;ftp://riders.com/orders/?username=rider&amp;password=secret&quot;,</em></p> <p><em>new AggregationStrategy() { <br />public Exchange aggregate(Exchange oldExchange, <br />Exchange newExchange) { <br />if (newExchange == null) { <br />return oldExchange; <br />} <br />String http = oldExchange.getIn() <br />.getBody(String.class); <br />String ftp = newExchange.getIn() <br />.getBody(String.class); <br />String body = http + &quot;\n&quot; + ftp; <br />oldExchange.getIn().setBody(body); <br />return oldExchange; <br />} <br />}) <br />.to(&quot;file://riders/orders&quot;);</em></p> <h3><em>Transforming XML</em></h3> <p>XML的转换主要有两种形式:</p> <p>1.XSLT格式化</p> <p><em>from(&quot;file://rider/inbox&quot;) <br />.to(&quot;xslt://camelinaction/transform.xsl&quot;) <br />.to(&quot;activemq:queue:transformed&quot;)</em></p> <p>2.XML与JAVA对象的序列化和反序列化</p> <p>提供了两个方法:marshal/unmarshal</p> <p>a)XSTREAM的形式</p> <p><em>&lt;route&gt; <br />&lt;from uri=&quot;direct:foo&quot;/&gt; <br />&lt;marshal&gt;&lt;xstream/&gt;&lt;/marshal&gt; <br />&lt;to uri=&quot;activemq:queue:foo&quot;/&gt; <br />&lt;/route&gt;</em></p> <p>b)JAXB的形式</p> <p><em>&lt;camelContext id=&quot;camel&quot; xmlns=&quot;</em><a href="http://camel.apache.org/schema/spring&quot;"><em>http://camel.apache.org/schema/spring&quot;</em></a><em>&gt; <br />&lt;dataFormats&gt; <br />&lt;jaxb id=&quot;jaxb&quot; contextPath=&quot;camelinaction&quot;/&gt; <br />&lt;/dataFormats&gt; <br />&lt;route&gt; <br />&lt;from uri=&quot;direct:order&quot;/&gt; <br />&lt;marshal ref=&quot;jaxb&quot;/&gt; <br />&lt;to uri=&quot;activemq:queue:order&quot;/&gt; <br />&lt;/route&gt; <br />&lt;route&gt; <br />&lt;from uri=&quot;activemq:queue:order&quot;/&gt; <br />&lt;unmarshal ref=&quot;jaxb&quot;/&gt; <br />&lt;to uri=&quot;direct:doSomething&quot;/&gt; <br />&lt;/route&gt; <br />&lt;/camelContext&gt;</em></p> <p><font color="#ff0000">注:Camel 还提供了csv和json的数据格式化,若有兴趣请看原文。</font></p> <h3><em>Writing your own data format</em></h3> <p>咱们能够自定义数据格式化,实现接口:</p> <p><em>org.apache.camel.spi.DataFormat</em></p> <p><em>最后讲下</em></p> <h3><em>Using Camel type converters</em></h3> <p>咱们常常取消息时会用</p> <p><em>String custom = exchange.getIn().getBody(String.class);</em></p> <p>这就是type converters的一种形式,TypeConverter 须要注册到TypeConverterRegistry 中去。</p> <p>TypeConverter还有以下的使用方法:</p> <p><em>from(&quot;file://riders/inbox&quot;) <br />.convertBodyTo(String.class) <br />.to(&quot;activemq:queue:inbox&quot;);</em></p> <p>咱们也能够自定义type converter,经过注解的形式@Converter。</p>spring

相关文章
相关标签/搜索