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

相關文章
相關標籤/搜索