1. 簡介
Mule ESB是一個基於Java的輕量級企業服務總線和集成平台,允許開發人員快速便利地連接多個應用,並支持應用間的數據交換。Mule ESB支持集成現有系統而無論其底層采用何種技術,如JMS、Web Services、JDBC、HTTP以及其他技術。
2. 整體結構
圖 整體結構
從上圖可見,Mule通過Transports/Connectors與外圍的異構系統連接,提供Routing(路由)、Transaction Management(事務管理)、Transformation(轉換)、Message Broker(消息代理)、Transportation Management(傳輸管理)、Security(安全)等核心模塊。Mule可以單獨使用,也可以架設在常用的應用服務器上。
圖 架構簡圖
外圍系統的服務請求通過Mule ESB的Transport接入,Mule通過Transformer進行數據的格式轉換,然后經過Inbound Router進行消息過濾(內部通過配置filter實現)后交給Mule的Component進行業務邏輯處理,處理后的結果通過Outbound Router確定傳遞給哪個接收方,然后通過Transformer進行數據格式轉換,通過Transport連接至接收方,傳遞信息。
此圖描述的是Mule中的一個典型場景的處理過程,涵蓋了Mule中的各個關鍵組件。其中某些處理步驟不是必須的,如Inbound Router、Transformer。后續可以看到一些其他場景的處理。
3. 功能
a. 服務中介
將業務邏輯和消息發送分離
屏蔽服務的消息格式和協議
提供任意位置的服務調用
提供協議橋接
b. 數據轉換
在應用間交換不同格式的信息
操作消息的負載內容,包括加密、壓縮和編碼轉換
在異構的傳輸協議的數據類型間格式化消息
c. 消息路由
基於消息內容和復雜規則路由消息
消息的過濾、聚合以及重新排列序號
d. 服務創建和托管
暴露端點、EJB、Spring Bean以及POJO作為服務
作為輕量級的服務容器進行服務托管
Mule ESB中有一些基本的概念,理解這些基本概念后才能理解Mule的內部機制。從中也可以看到Mule解決問題的基本思路。
4. 基本概念
4.1 Model
Model表示托管各個服務的運行時環境。
4.2 Service
Service是用來處理服務請求的基本單位,它調用各個組件進行服務請求的處理。
4.3 Transport
Transport管理消息的接收和發送,數據轉換的過程也是在Transport中通過調用Transformer完成的。
4.3.1 Connector
Connector用於管控特定協議的使用,如HTTP Connector、JMS Connector等。
4.3.2 End-Point
Endpoint用於表示一種協議的特定使用方式,如listening/polling、從中讀取、向指定地址寫入等,定義了發送和接收消息的通道。Endpoint控制的是底層的實體在Connector中如何被使用。
Endpoint定義於Inbound和Outbound Router中。
4.4 Transformer
Transformer用於轉換消息的內容。
4.5 Router
Router使用Filter基於消息中的屬性信息進行消息的分發。
圖 Router
Router在Service中的位置決定了Router的性質(inbound、outbound和response)和擔任的角色(pass-through、aggregator等)。
4.6 Component
Component是Service的核心部件,是Service的業務邏輯的實現。
圖 Component: implicit bridge component
Component可以是Java Class(POJO、Spring Bean)、Web Service、Script等。
Component可定義自己的生命周期:initialise、start、stop、dispose,不過需要實現Mule的LifeCycle接口。Mule 3.0版本開始提供@PostConstruct和@PreDestroy的注解,對應生命周期的initialise和dispose階段,不需要實現Mule的LifeCycle接口了。
4.7 Flow(@since 3.0)
Flow是Mule 3.0新引入的,包含一個消息源(Message Source)和多個消息處理器組成的處理器鏈。
圖 Flow
根據實際需求着重檢查了一下Mule ESB的消息傳遞方式。Mule支持常用的幾種消息傳遞方式,能夠滿足要求。
5. 消息傳遞方式
5.1 異步方式
異步方式是一種單向調用,調用者不需要獲得響應。
圖 Asynchronous
異步方式通過inbound和outbound endpoint的exchange-pattern=”one-way”實現。
使用基本的Stdio Transport驗證,通過標准輸入傳輸字符串,將其原樣傳遞給標准輸出進行顯示。相應配置如下:
xml 代碼
1.<service name="echo">
2. <inbound>
3. <stdio:inbound-endpoint system="IN" exchange-pattern="one-way" />
4. </inbound>
5.
6. <component>
7. <singleton-object class="demo.mule.umo.StdIo" />
8. </component>
9.
10. <outbound>
11. <pass-through-router>
12. <stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" />
13. </pass-through-router>
14. </outbound>
15.</service>
運行服務,控制台顯示結果如下:
1.Please enter: Hello, world!
2.INFO 2010-12-07 19:21:18,877 [ConsoleConnector.dispatcher.1]
3. org.mule.lifecycle.AbstractLifecycleManager: Initialising:
4. 'ConsoleConnector.dispatcher.23255376'. Object is: StdioMessageDispatcher
5.INFO 2010-12-07 19:21:18,877 [ConsoleConnector.dispatcher.1]
6. org.mule.lifecycle.AbstractLifecycleManager: Starting:
7. 'ConsoleConnector.dispatcher.23255376'. Object is: StdioMessageDispatcher
8.Hello, world!
其中INFO輸出是Mule第一次初始化相應Connector打印出來的,之后調用服務不會再次顯示。
異步方式適用於簡單的消息傳遞的場景。
5.2 請求-響應方式
請求-響應方式即請求方調用服務后,服務立即處理並返回響應結果,不需將消息再次傳遞。
請求-響應方式通過input endpoint的exchange-pattern=”request-response”實現,相應配置如下:
xml 代碼
1.<strong>
2. <strong>
3. <model name="services">
4. <service name="echoService">
5. <inbound>
6. <inbound-endpoint address="http://localhost:7007/services/Echo"
7. exchange-pattern="request-response">
8. <cxf:jaxws-service />
9. </inbound-endpoint>
10. </inbound>
11. <component>
12. <singleton-object class="demo.mule.umo.Echo" />
13. </component>
14. </service>
15. </model>
16. </strong>
17.</strong>
上邊是通過service配置的,通過flow配置如下:
xml 代碼
1.<flow name="EchoFlow">
2. <inbound-endpoint address="http://localhost:7007/services/Echo"
3. exchange-pattern="request-response" />
4. <cxf:jaxws-service serviceClass="demo.mule.umo.Echo" />
5. <component>
6. <singleton-object class="demo.mule.umo.Echo" />
7. </component>
8.</flow>
在瀏覽器中輸入“http://localhost:7007/services/Echo/echo/text/hello,world”,瀏覽器中會顯示“hello,world”的輸出信息。
請求-響應方式適用於單次服務調用的場景。
5.3 同步方式
同步方式即請求方調用服務后,component將處理結果發送給另一個外部服務處理,並將處理結果反方向返回。
圖 Synchronous
同步方式通過inbound和outbound endpoint的exchange-pattern=”request-response”實現,相應配置如下:
xml 代碼
1.<flow name="echo">
2. <inbound-endpoint address="http://localhost:7007/services/Echo"
3. exchange-pattern="request-response" />
4. <cxf:jaxws-service serviceClass="demo.mule.umo.Echo" />
5. <component>
6. <singleton-object class="demo.mule.umo.StdIo" />
7. </component>
8. <vm:outbound-endpoint path="vm" exchange-pattern="request-response" />
9.</flow>
10.<flow name="vm">
11. <vm:inbound-endpoint path="vm" exchange-pattern="request-response" />
12. <component>
13. <singleton-object class="demo.mule.umo.Vm" />
14. </component>
15. <stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" />
16.</flow>
同步方式適用於通過Mule調用遠程服務的場景。
5.4 異步請求-響應方式
異步請求-響應方式即請求方調用服務后不需要立即獲得返回結果,component將請求發送給其他外圍系統處理(可能有多個),全部處理完畢后通過指定的異步應答Router返回給請求方。
圖 Asynchronous Request-Response
異步請求-響應方式通過在OutBound Endpoint中增加reply-to以及增加async-reply節點實現,響應配置如下:
xml 代碼
1.<flow name="echo">
2. <inbound-endpoint address="http://localhost:7007/services/Echo"
3. exchange-pattern="request-response" />
4. <cxf:jaxws-service serviceClass="demo.mule.umo.Echo" />
5. <component>
6. <singleton-object class="demo.mule.umo.StdIo" />
7. </component>
8. <vm:outbound-endpoint path="vm" exchange-pattern="request-response" />
9.</flow>
10.<flow name="vm">
11. <vm:inbound-endpoint path="vm" exchange-pattern="request-response" />
12. <component>
13. <singleton-object class="demo.mule.umo.Vm" />
14. </component>
15. <stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" />
16.</flow>
異步請求-響應方式適用於請求需要被多個遠程服務並行處理,結果需要匯總處理后返回的場景。
注:上述代碼未運行通過,queue1和queue2獲得了請求消息並正常處理,但返回至async-reply時拋出異常,暫未定位到問題。
后將collection-async-reply-router改為single-async-reply-router未報異常,代碼示例如下:
xml 代碼
1.<em><service name="async req-rep">
2. <inbound>
3. <stdio:inbound-endpoint ref="stdioInEndpoint" />
4. </inbound>
5. <component class="demo.mule.umo.Echo" />
6. <outbound>
7. <multicasting-router>
8. <vm:outbound-endpoint path="async.queue1" exchange-pattern="one-way" />
9. <vm:outbound-endpoint path="async.queue2" exchange-pattern="one-way" />
10. <reply-to address="vm://reply" />
11. </multicasting-router>
12. </outbound>
13. <async-reply timeout="5000" failOnTimeout="true">
14. <vm:inbound-endpoint path="reply" exchange-pattern="one-way" />
15. <single-async-reply-router />
16. </async-reply>
17.</service></em>
6. 配置模式
Mule 3.0版本提供了“pattern”的機制。Pattern總結了實際使用過程中的常見場景,以簡化的服務配置方式提供。
6.1 簡單服務模式(simple service pattern)
簡單服務模式用於簡化同步服務調用的配置,對應消息傳遞方式中的請求-響應方式。
簡單服務模式通過simple-service 元素配置,主要的元素屬性包括:
屬性 說明
address 服務監聽的地址,如vm:in
component-class Component的實現類
type direct: 默認;
jax-ws: 將component暴露為soap式的web service(component必須基於jax-ws的注解),address一般為Http Transport;
jax-rs: 將component暴露為rest式的web service(component必須基於@Path的注解),address一般為Http或Servlet Transport
代碼示例:
<simple-service name="simple-service" address="vm://simple.in"
component-class="demo.mule.umo.Echo" />Mule針對服務請求接入可以做額外的處理,比如增加Transformer配置進行數據轉換。
6.2 橋接模式(bridge pattern)
橋接模式用於在inbound endpoint和outbound endpoint之間建立直接連接,不需要component提供業務邏輯。
橋接模式通過bridge元素配置,主要屬性包括:
屬性 說明
inboundAddress 服務請求接入地址
outboundAddress 服務接出的實際地址
exchange-pattern request-response: 默認,返回處理結果;
one-way: 單向
transacted true: 在向outbound endpoint分發時使用事務;
false: 不使用事務
代碼示例:
<bridge name="queue-to-topic" transacted="true" inboundAddress="jms://myQueue"
outboundAddress="jms://topic:myTopic" />
Mule在接入、接出的過程中可以做額外的處理,比如增加Transformer配置進行數據轉換。如果使用事務控制,對於異構的協議之間的事務需要有支持XA的事務控制器。
6.3 校驗器模式(validator pattern)
校驗器模式通過定義一個校驗過濾器過濾服務請求,並同步返回ACK(ACKnowledge)或NACK(Not Acknowledge)結果。通過校驗的服務請求被異步分發給處理方。
校驗器模式通過validator元素配置,主要屬性包括:
屬性 說明
inboundAddress 服務請求接入地址
outboundAddress 服務接出地址
ackExpression 表達式,用於構建服務請求被接收時的信息
nackExpression 表達式,用於構建服務請求被拒絕時的信息
errorExpression @since 3.0.1
表達式,用於構建在服務請求分發出錯時的信息
validationFilter-ref 過濾器的引用,也可以使用子元素指定
用於確定服務請求是否被接收
代碼示例:
<validator name="integer-validator" inboundAddress="vm://validator.in"
ackExpression="#[string:GOOD:#[message:payload]@#[context:serviceName]]"
nackExpression="#[string:BAD:#[message:payload]@#[context:serviceName]]"
outboundAddress="vm://test-service.in">
<payload-type-filter expectedType="java.lang.Integer" />
</validator>注:Mule的表達式后續補充。
6.4 web服務代理模式(web service proxy pattern)
Web服務代理模式用於將Web Service請求直接轉發至遠程目標Web Service服務端,Mule本身不提供實際的Web Service。
Web服務代理模式通過ws-proxy元素配置,主要屬性包括:
屬性 說明
inboundAddress Mule對外提供的地址
outboundAddress Web Service的實際地址
代碼示例:
<ws:proxy name="ws-proxy"
inboundAddress="http://localhost:7006/services/Echo"
outboundAddress="http://localhost:8000/services/Echo?method=echo">
</ws:proxy>
Mule在轉發的過程中可以做額外的處理,比如增加Transformer配置進行數據轉換。
7. 總結
一. 服務調用
1. Mule實現並提供Web Service
在Mule上開發並發布一個Web Service供客戶端調用。
•示例配置
<flow name="local-ws">
<core:inbound-endpoint address="http://localhost:65082/services/Echo1"
exchange-pattern="request-response" doc:name="Generic" doc:description="Generic endpoint specified by address URI" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"
doc:description="Make a web service available via CXF" />
<component doc:name="Component" doc:description="Invoke a Java component">
<singleton-object class="demo.mule.component.Echo" />
</component>
</ flow >
•測試方法
在瀏覽器地址欄中輸入“ http://localhost:65082/services/Echo1/echo/text/hello ”,回車后瀏覽器中將顯示返回結果信息。地址中的“ echo ”是服務的方法,“ text ”是方法的參數,“ hello ”是參數的值。
2. Web Service Proxy
Web Service Proxy用來將客戶端的WS請求直接轉發至相應的遠程WS服務端處理,並返回處理結果。Mule本身不做任何處理。
2.1 配置方式1
•示例配置
<flow name="local2remote-ws">
<http:inbound-endpoint keep-alive="false" address="http://localhost:65000"
encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response" doc:name="HTTP"
doc:description="" />
<http:outbound-endpoint method="GET" keep-alive="false"
address="http://localhost:5050#[header:INBOUND:http.request]" responseTimeout="10000" encoding="UTF-8"
disableTransportTransformer="false" followRedirects="false" exchange-pattern="request-response"
doc:name="HTTP" doc:description="" />
</ flow >
•說明
注意 outbound-endpoint 中 address 參數中的表達式。
•測試方法
瀏覽器中通過“ http://localhost:65000/webservice/EchoService?wsdl ”(將內容復制,保存為 *.wsdl ),然后使用 SoapUI 測試。
2.2 配置方式2
•示例配置
<pattern:web-service-proxy name="ws-proxy" inboundAddress="http://localhost:65082/services/Echo2"
outboundAddress="http://localhost:65082/services/Echo1?method=echo">
</pattern:web-service-proxy>
•說明
Mule 為這種常見的場景提供了現成的模式,以簡化配置。
•測試方法
通過“ http://localhost:65082/services/Echo2?wsdl ”獲取 wsdl 文件,然后使用 SoapUI 測試。
3. Web Service to Web Service
Web Service To Web Service用於在Mule中提供Web Service供客戶端調用,Mule接收請求后調用遠端的Web Service進行處理,並返回結果。
•示例配置
<flow name="local-ws2remote-ws">
<core:inbound-endpoint address="http://localhost:65082/services/Echo8"
disableTransportTransformer="false" exchange-pattern="request-response" doc:name="Generic"
doc:description="Generic endpoint specified by address URI" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"
doc:description="Make a web service available via CXF" />
<core:outbound-endpoint
address="wsdl-cxf:http://server1:5050/mule-business/webservice/EchoService?wsdl&method=Echo" />
</ flow >
•說明
注意outbound-endpoint中address參數的配置方式,使用了wsdl-cxf前綴表示此web service是由cxf提供的。
•測試方法
在瀏覽器中輸入“ http://localhost:65082/services/Echo8/echo/text/hello ”進行測試。
4. Socket to Socket
Socket To Socket用於將客戶端的Socket請求轉發至遠程的Socket服務端處理,並返回處理結果。
•示例配置
<flow name="tcp2tcp">
<tcp:inbound-endpoint host="localhost" port="7100" responseTimeout="10000"
encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response" doc:name="TCP"
doc:description="The TCP transport enables events to be sent and received over TCP sockets." />
<tcp:outbound-endpoint host="localhost" port="7000" responseTimeout="10000"
encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response" doc:name="TCP"
doc:description="The TCP transport enables events to be sent and received over TCP sockets." />
</ flow >
•說明
主要配置 host 、 port 參數,表明服務地址。
•測試方法
通過 SimpleServer 和 SimpleClient 測試類,首先啟動 SimpleServer ,然后啟動 SimpleClient ,發送請求並接收處理結果。
5. JMS Topic
客戶端發送Web Service請求,Mule將請求消息發送至遠程JMS的Topic中。
•示例配置
<flow name="local-ws2jms-topic">
<core:inbound-endpoint address="http://localhost:65082/services/Echo3"
responseTimeout="10000" encoding="UTF-8" disableTransportTransformer="false" mimeType="text/plain"
exchange-pattern="one-way" doc:name="Generic" doc:description="Generic endpoint specified by address URI" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"
doc:description="Make a web service available via CXF" />
<jms:outbound-endpoint topic="topic1" responseTimeout="10000" encoding="UTF-8"
disableTransportTransformer="false" disableTemporaryReplyToDestinations="false" exchange-pattern="one-way"
connector-ref="activemqConnector" doc:name="JMS" doc:description="Send or receive messages from a JMS queue" />
</flow>
<flow name="jms-topic2echo">
<jms:inbound-endpoint topic="topic1" responseTimeout="10000" encoding="UTF-8"
disableTransportTransformer="false" disableTemporaryReplyToDestinations="false" exchange-pattern="one-way"
connector-ref="activemqConnector" doc:name="JMS" doc:description="Send or receive messages from a JMS queue" />
<echo-component doc:name="Echo" doc:description="Echoes message payload." />
</ flow >
•說明
JMS endpoint 是單向的,不需要返回值。通過 topic 屬性指定 JMS Server 的 Topic 名稱, connector-ref 指明了使用的 JMS 連接。
•測試方法
在瀏覽器地址欄中輸入“ http://localhost:65082/services/Echo3/echo/text/hello ”發送請求, Mule 控制台上輸出訂閱者的處理結果(上述示例中通過 Mule 配置了一個 JMS 的訂閱者)。也可以通過 ActiveMQ 的控制台,查看到 Topic 中增加了一條發布的消息。
二. 基於消息內容的路由
Mule提供基於消息內容的路由機制,根據消息中的指定信息,將消息發送至不同的服務端進行處理。
1. Socket to Socket 路由
•示例配置
<flow name="tcp2tcp-router">
<tcp:inbound-endpoint host="localhost" port="7101" responseTimeout="10000"
encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response" doc:name="TCP"
doc:description="The TCP transport enables events to be sent and received over TCP sockets." />
<choice>
<when evaluator="jxpath" expression="(req/area)='bj'">
<tcp:outbound-endpoint host="server1" port="7101"
responseTimeout="10000" encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response"
doc:name="TCP" doc:description="The TCP transport enables events to be sent and received over TCP sockets." />
</when>
<when evaluator="jxpath" expression="(req/area)='sh'">
<tcp:outbound-endpoint host="server1" port="7102"
responseTimeout="10000" encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response"
doc:name="TCP" doc:description="The TCP transport enables events to be sent and received over TCP sockets." />
</when>
</choice>
</ flow >
•說明
路由使用了 <choice> 、 <when> 元素,表示路由分支。 When 元素使用 evaluator 指明表達式的解析方式,使用 expression 描述消息內容的判斷條件。
•測試方法
同 Socket To Socket 測試,消息內容分別為 <req><area>bj</area></req> 、 <req><area>sh</area></req> ,查看發送至不同服務器的輸出。
2. Web Service to JMS Topic 路由
•示例配置
<flow name="local-ws2jms-topic-router">
<core:inbound-endpoint address="http://localhost:65082/services/Echo7"
disableTransportTransformer="false" exchange-pattern="request-response" doc:name="Generic"
doc:description="Generic endpoint specified by address URI" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"
doc:description="Make a web service available via CXF" />
<choice>
<when evaluator="jxpath" expression="(req/area)='bj'">
<jms:outbound-endpoint topic="topic1" responseTimeout="10000" encoding="UTF-8"
disableTransportTransformer="false" disableTemporaryReplyToDestinations="false"
exchange-pattern="one-way" connector-ref="activemqConnector" doc:name="JMS"
doc:description="Send or receive messages from a JMS queue" />
</when>
<when evaluator="jxpath" expression="(req/area)='sh'">
<jms:outbound-endpoint topic="topic2" responseTimeout="10000" encoding="UTF-8"
disableTransportTransformer="false" disableTemporaryReplyToDestinations="false"
exchange-pattern="one-way" connector-ref="activemqConnector" doc:name="JMS"
doc:description="Send or receive messages from a JMS queue" />
</when>
</choice>
</ flow >
•測試方法
通過“ http://localhost:65082/services/Echo7?wsdl ”獲取 wsdl 文件,然后通過 SoapUI 發送請求,查看返回結果。修改消息內容,查看結果的變化。
3. Web Service to Web Service 路由
•示例配置
<flow name="local-ws2jms-topic-router">
<core:inbound-endpoint address="http://localhost:65082/services/Echo9"
disableTransportTransformer="false" exchange-pattern="request-response" doc:name="Generic"
doc:description="Generic endpoint specified by address URI" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"
doc:description="Make a web service available via CXF" />
<choice>
<when evaluator="jxpath" expression="(req/area)='bj'">
<core:outbound-endpoint
address="wsdl-cxf:http://server1:5050/mule-business/webservice/CalcService?wsdl&method=processXml" />
</when>
<when evaluator="jxpath" expression="(req/area)='sh'">
<core:outbound-endpoint
address="wsdl-cxf:http://server2:5050/mule-business/webservice/CalcService?wsdl&method=processXml" />
</when>
</choice>
</ flow >
•測試方法
使用“ <![CDATA[<req><seq>1</seq><area>bj</area><price>123.45</price><count>10</count></req>]]> ”數據進行測試。
三. 數據轉換
1. 壓縮解壓
Mule原生提供了gzip壓縮方式的Transformer。
•示例配置
<flow name="gzip">
<stdio:inbound-endpoint ref="stdioInEndpoint" />
<core:string-to-byte-array-transformer encoding="UTF-8" />
<component class="demo.mule.component.Passthrough" />
<core:gzip-compress-transformer encoding="UTF-8" />
<component class="demo.mule.component.Passthrough" />
<core:gzip-uncompress-transformer encoding="UTF-8" />
<component class="demo.mule.component.Passthrough" />
<core:byte-array-to-string-transformer encoding="UTF-8" />
<stdio:outbound-endpoint ref="stdioOutEndpoint" />
</ flow >
•說明
gzip-compress-transformer 針對 byte[] 進行壓縮處理,因此對於字符串類型的消息,首先需要通過 string-to-byte-array-transformer 進行轉換。
•測試方法
在控制台的提示信息后輸入測試字符串,完成后控制台輸出同樣的信息。
2. 加密解密
加密、解密是一種特定的數據轉換方式,因此通過自定義Transformer的形式支持。
•示例配置
<flow name="encrypt">
<core:inbound-endpoint address="http://localhost:65082/services/Echo11"
responseTimeout="10000" encoding="UTF-8" disableTransportTransformer="false" mimeType="text/plain"
exchange-pattern="one-way" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" />
<component>
<singleton-object class="demo.mule.component.Echo" />
</component>
<core:custom-transformer class="demo.mule.transformer.DesEncryptTransformer" encoding="UTF-8" />
<component class="demo.mule.component.Passthrough" />
<core:custom-transformer class="demo.mule.transformer.DesDecryptTransformer" encoding="UTF-8" />
<stdio:outbound-endpoint ref="stdioOutEndpoint" />
</ flow >
•說明
DesEncryptTransformer 是自定義的壓縮轉換器, DesDecryptTransformer 是對應的解壓轉換器。
•測試方法
在瀏覽器地址欄中輸入“ http://localhost:65082/services/Echo11/echo/text/ 測試字符串”,在控制台中可見加密后的字符串和最終解密后與原串相同的字符串。
3. 自定義Transformer
•示例代碼
import demo.mule.dto.Envelope;
public class String2EnvelopeTransformer extends AbstractTransformer {
private static Log log = LogFactory.getLog(String2EnvelopeTransformer.class);
@Override
protected Object doTransform(Object src, String enc) throws TransformerException {
Envelope env = new Envelope();
if (src instanceof String) {
StringTokenizer st = new StringTokenizer((String) src, ",");
String area = st.nextToken();
String data = st.nextToken();
env.create(area, data);
}
log.debug(env);
return env;
}
}
•說明
自定義 Transformer 需要繼承 AbstractTransformer 類,實現其 doTransform 方法。該方法接收兩個參數,一個是消息對象,一個是消息的 Encoding ,方法拋出 TransformerException ,返回轉換后的消息對象。
•實例配置
<flow name="local-ws">
<core:inbound-endpoint address="http://localhost:65082/services/Echo1"
exchange-pattern="request-response" doc:name="Generic" doc:description="Generic endpoint specified by address URI" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"
doc:description="Make a web service available via CXF" />
<component doc:name="Component" doc:description="Invoke a Java component">
<singleton-object class="demo.mule.component.Echo" />
</component>
<core:custom-transformer class="demo.mule.transformer.String2EnvelopeTransformer"></core:custom-transformer>
<core:outbound-endpoint address="stdio://System.out" exchange-pattern="one-way" />
</ flow >
•測試方法
在瀏覽器中輸入“ http://localhost:65082/services/Echo1/echo/text/bj,hello ”進行測試,觀察控制台輸出結果。
Mule ESB是一個基於Java的輕量級企業服務總線和集成平台,允許開發人員快速便利地連接多個應用,並支持應用間的數據交換。Mule ESB支持集成現有系統而無論其底層采用何種技術,如JMS、Web Services、JDBC、HTTP以及其他技術。
2. 整體結構

圖 整體結構
從上圖可見,Mule通過Transports/Connectors與外圍的異構系統連接,提供Routing(路由)、Transaction Management(事務管理)、Transformation(轉換)、Message Broker(消息代理)、Transportation Management(傳輸管理)、Security(安全)等核心模塊。Mule可以單獨使用,也可以架設在常用的應用服務器上。

圖 架構簡圖
外圍系統的服務請求通過Mule ESB的Transport接入,Mule通過Transformer進行數據的格式轉換,然后經過Inbound Router進行消息過濾(內部通過配置filter實現)后交給Mule的Component進行業務邏輯處理,處理后的結果通過Outbound Router確定傳遞給哪個接收方,然后通過Transformer進行數據格式轉換,通過Transport連接至接收方,傳遞信息。
此圖描述的是Mule中的一個典型場景的處理過程,涵蓋了Mule中的各個關鍵組件。其中某些處理步驟不是必須的,如Inbound Router、Transformer。后續可以看到一些其他場景的處理。
3. 功能
a. 服務中介
將業務邏輯和消息發送分離
屏蔽服務的消息格式和協議
提供任意位置的服務調用
提供協議橋接
b. 數據轉換
在應用間交換不同格式的信息
操作消息的負載內容,包括加密、壓縮和編碼轉換
在異構的傳輸協議的數據類型間格式化消息
c. 消息路由
基於消息內容和復雜規則路由消息
消息的過濾、聚合以及重新排列序號
d. 服務創建和托管
暴露端點、EJB、Spring Bean以及POJO作為服務
作為輕量級的服務容器進行服務托管
Mule ESB中有一些基本的概念,理解這些基本概念后才能理解Mule的內部機制。從中也可以看到Mule解決問題的基本思路。
4. 基本概念
4.1 Model
Model表示托管各個服務的運行時環境。

4.2 Service
Service是用來處理服務請求的基本單位,它調用各個組件進行服務請求的處理。

4.3 Transport
Transport管理消息的接收和發送,數據轉換的過程也是在Transport中通過調用Transformer完成的。

4.3.1 Connector
Connector用於管控特定協議的使用,如HTTP Connector、JMS Connector等。
4.3.2 End-Point
Endpoint用於表示一種協議的特定使用方式,如listening/polling、從中讀取、向指定地址寫入等,定義了發送和接收消息的通道。Endpoint控制的是底層的實體在Connector中如何被使用。
Endpoint定義於Inbound和Outbound Router中。
4.4 Transformer
Transformer用於轉換消息的內容。

4.5 Router
Router使用Filter基於消息中的屬性信息進行消息的分發。

圖 Router
Router在Service中的位置決定了Router的性質(inbound、outbound和response)和擔任的角色(pass-through、aggregator等)。
4.6 Component
Component是Service的核心部件,是Service的業務邏輯的實現。

圖 Component: implicit bridge component
Component可以是Java Class(POJO、Spring Bean)、Web Service、Script等。
Component可定義自己的生命周期:initialise、start、stop、dispose,不過需要實現Mule的LifeCycle接口。Mule 3.0版本開始提供@PostConstruct和@PreDestroy的注解,對應生命周期的initialise和dispose階段,不需要實現Mule的LifeCycle接口了。
4.7 Flow(@since 3.0)
Flow是Mule 3.0新引入的,包含一個消息源(Message Source)和多個消息處理器組成的處理器鏈。

圖 Flow
根據實際需求着重檢查了一下Mule ESB的消息傳遞方式。Mule支持常用的幾種消息傳遞方式,能夠滿足要求。
5. 消息傳遞方式
5.1 異步方式
異步方式是一種單向調用,調用者不需要獲得響應。

圖 Asynchronous
異步方式通過inbound和outbound endpoint的exchange-pattern=”one-way”實現。
使用基本的Stdio Transport驗證,通過標准輸入傳輸字符串,將其原樣傳遞給標准輸出進行顯示。相應配置如下:
xml 代碼
1.<service name="echo">
2. <inbound>
3. <stdio:inbound-endpoint system="IN" exchange-pattern="one-way" />
4. </inbound>
5.
6. <component>
7. <singleton-object class="demo.mule.umo.StdIo" />
8. </component>
9.
10. <outbound>
11. <pass-through-router>
12. <stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" />
13. </pass-through-router>
14. </outbound>
15.</service>
運行服務,控制台顯示結果如下:
1.Please enter: Hello, world!
2.INFO 2010-12-07 19:21:18,877 [ConsoleConnector.dispatcher.1]
3. org.mule.lifecycle.AbstractLifecycleManager: Initialising:
4. 'ConsoleConnector.dispatcher.23255376'. Object is: StdioMessageDispatcher
5.INFO 2010-12-07 19:21:18,877 [ConsoleConnector.dispatcher.1]
6. org.mule.lifecycle.AbstractLifecycleManager: Starting:
7. 'ConsoleConnector.dispatcher.23255376'. Object is: StdioMessageDispatcher
8.Hello, world!
其中INFO輸出是Mule第一次初始化相應Connector打印出來的,之后調用服務不會再次顯示。
異步方式適用於簡單的消息傳遞的場景。
5.2 請求-響應方式
請求-響應方式即請求方調用服務后,服務立即處理並返回響應結果,不需將消息再次傳遞。

請求-響應方式通過input endpoint的exchange-pattern=”request-response”實現,相應配置如下:
xml 代碼
1.<strong>
2. <strong>
3. <model name="services">
4. <service name="echoService">
5. <inbound>
6. <inbound-endpoint address="http://localhost:7007/services/Echo"
7. exchange-pattern="request-response">
8. <cxf:jaxws-service />
9. </inbound-endpoint>
10. </inbound>
11. <component>
12. <singleton-object class="demo.mule.umo.Echo" />
13. </component>
14. </service>
15. </model>
16. </strong>
17.</strong>
上邊是通過service配置的,通過flow配置如下:
xml 代碼
1.<flow name="EchoFlow">
2. <inbound-endpoint address="http://localhost:7007/services/Echo"
3. exchange-pattern="request-response" />
4. <cxf:jaxws-service serviceClass="demo.mule.umo.Echo" />
5. <component>
6. <singleton-object class="demo.mule.umo.Echo" />
7. </component>
8.</flow>
在瀏覽器中輸入“http://localhost:7007/services/Echo/echo/text/hello,world”,瀏覽器中會顯示“hello,world”的輸出信息。
請求-響應方式適用於單次服務調用的場景。
5.3 同步方式
同步方式即請求方調用服務后,component將處理結果發送給另一個外部服務處理,並將處理結果反方向返回。

圖 Synchronous
同步方式通過inbound和outbound endpoint的exchange-pattern=”request-response”實現,相應配置如下:
xml 代碼
1.<flow name="echo">
2. <inbound-endpoint address="http://localhost:7007/services/Echo"
3. exchange-pattern="request-response" />
4. <cxf:jaxws-service serviceClass="demo.mule.umo.Echo" />
5. <component>
6. <singleton-object class="demo.mule.umo.StdIo" />
7. </component>
8. <vm:outbound-endpoint path="vm" exchange-pattern="request-response" />
9.</flow>
10.<flow name="vm">
11. <vm:inbound-endpoint path="vm" exchange-pattern="request-response" />
12. <component>
13. <singleton-object class="demo.mule.umo.Vm" />
14. </component>
15. <stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" />
16.</flow>
同步方式適用於通過Mule調用遠程服務的場景。
5.4 異步請求-響應方式
異步請求-響應方式即請求方調用服務后不需要立即獲得返回結果,component將請求發送給其他外圍系統處理(可能有多個),全部處理完畢后通過指定的異步應答Router返回給請求方。

圖 Asynchronous Request-Response
異步請求-響應方式通過在OutBound Endpoint中增加reply-to以及增加async-reply節點實現,響應配置如下:
xml 代碼
1.<flow name="echo">
2. <inbound-endpoint address="http://localhost:7007/services/Echo"
3. exchange-pattern="request-response" />
4. <cxf:jaxws-service serviceClass="demo.mule.umo.Echo" />
5. <component>
6. <singleton-object class="demo.mule.umo.StdIo" />
7. </component>
8. <vm:outbound-endpoint path="vm" exchange-pattern="request-response" />
9.</flow>
10.<flow name="vm">
11. <vm:inbound-endpoint path="vm" exchange-pattern="request-response" />
12. <component>
13. <singleton-object class="demo.mule.umo.Vm" />
14. </component>
15. <stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" />
16.</flow>
異步請求-響應方式適用於請求需要被多個遠程服務並行處理,結果需要匯總處理后返回的場景。
注:上述代碼未運行通過,queue1和queue2獲得了請求消息並正常處理,但返回至async-reply時拋出異常,暫未定位到問題。
后將collection-async-reply-router改為single-async-reply-router未報異常,代碼示例如下:
xml 代碼
1.<em><service name="async req-rep">
2. <inbound>
3. <stdio:inbound-endpoint ref="stdioInEndpoint" />
4. </inbound>
5. <component class="demo.mule.umo.Echo" />
6. <outbound>
7. <multicasting-router>
8. <vm:outbound-endpoint path="async.queue1" exchange-pattern="one-way" />
9. <vm:outbound-endpoint path="async.queue2" exchange-pattern="one-way" />
10. <reply-to address="vm://reply" />
11. </multicasting-router>
12. </outbound>
13. <async-reply timeout="5000" failOnTimeout="true">
14. <vm:inbound-endpoint path="reply" exchange-pattern="one-way" />
15. <single-async-reply-router />
16. </async-reply>
17.</service></em>
6. 配置模式
Mule 3.0版本提供了“pattern”的機制。Pattern總結了實際使用過程中的常見場景,以簡化的服務配置方式提供。
6.1 簡單服務模式(simple service pattern)
簡單服務模式用於簡化同步服務調用的配置,對應消息傳遞方式中的請求-響應方式。

簡單服務模式通過simple-service 元素配置,主要的元素屬性包括:
屬性 說明
address 服務監聽的地址,如vm:in
component-class Component的實現類
type direct: 默認;
jax-ws: 將component暴露為soap式的web service(component必須基於jax-ws的注解),address一般為Http Transport;
jax-rs: 將component暴露為rest式的web service(component必須基於@Path的注解),address一般為Http或Servlet Transport
代碼示例:
<simple-service name="simple-service" address="vm://simple.in"
component-class="demo.mule.umo.Echo" />Mule針對服務請求接入可以做額外的處理,比如增加Transformer配置進行數據轉換。
6.2 橋接模式(bridge pattern)
橋接模式用於在inbound endpoint和outbound endpoint之間建立直接連接,不需要component提供業務邏輯。

橋接模式通過bridge元素配置,主要屬性包括:
屬性 說明
inboundAddress 服務請求接入地址
outboundAddress 服務接出的實際地址
exchange-pattern request-response: 默認,返回處理結果;
one-way: 單向
transacted true: 在向outbound endpoint分發時使用事務;
false: 不使用事務
代碼示例:
<bridge name="queue-to-topic" transacted="true" inboundAddress="jms://myQueue"
outboundAddress="jms://topic:myTopic" />
Mule在接入、接出的過程中可以做額外的處理,比如增加Transformer配置進行數據轉換。如果使用事務控制,對於異構的協議之間的事務需要有支持XA的事務控制器。
6.3 校驗器模式(validator pattern)
校驗器模式通過定義一個校驗過濾器過濾服務請求,並同步返回ACK(ACKnowledge)或NACK(Not Acknowledge)結果。通過校驗的服務請求被異步分發給處理方。

校驗器模式通過validator元素配置,主要屬性包括:
屬性 說明
inboundAddress 服務請求接入地址
outboundAddress 服務接出地址
ackExpression 表達式,用於構建服務請求被接收時的信息
nackExpression 表達式,用於構建服務請求被拒絕時的信息
errorExpression @since 3.0.1
表達式,用於構建在服務請求分發出錯時的信息
validationFilter-ref 過濾器的引用,也可以使用子元素指定
用於確定服務請求是否被接收
代碼示例:
<validator name="integer-validator" inboundAddress="vm://validator.in"
ackExpression="#[string:GOOD:#[message:payload]@#[context:serviceName]]"
nackExpression="#[string:BAD:#[message:payload]@#[context:serviceName]]"
outboundAddress="vm://test-service.in">
<payload-type-filter expectedType="java.lang.Integer" />
</validator>注:Mule的表達式后續補充。
6.4 web服務代理模式(web service proxy pattern)
Web服務代理模式用於將Web Service請求直接轉發至遠程目標Web Service服務端,Mule本身不提供實際的Web Service。

Web服務代理模式通過ws-proxy元素配置,主要屬性包括:
屬性 說明
inboundAddress Mule對外提供的地址
outboundAddress Web Service的實際地址
代碼示例:
<ws:proxy name="ws-proxy"
inboundAddress="http://localhost:7006/services/Echo"
outboundAddress="http://localhost:8000/services/Echo?method=echo">
</ws:proxy>
Mule在轉發的過程中可以做額外的處理,比如增加Transformer配置進行數據轉換。
7. 總結
一. 服務調用
1. Mule實現並提供Web Service
在Mule上開發並發布一個Web Service供客戶端調用。
•示例配置
<flow name="local-ws">
<core:inbound-endpoint address="http://localhost:65082/services/Echo1"
exchange-pattern="request-response" doc:name="Generic" doc:description="Generic endpoint specified by address URI" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"
doc:description="Make a web service available via CXF" />
<component doc:name="Component" doc:description="Invoke a Java component">
<singleton-object class="demo.mule.component.Echo" />
</component>
</ flow >
•測試方法
在瀏覽器地址欄中輸入“ http://localhost:65082/services/Echo1/echo/text/hello ”,回車后瀏覽器中將顯示返回結果信息。地址中的“ echo ”是服務的方法,“ text ”是方法的參數,“ hello ”是參數的值。
2. Web Service Proxy
Web Service Proxy用來將客戶端的WS請求直接轉發至相應的遠程WS服務端處理,並返回處理結果。Mule本身不做任何處理。
2.1 配置方式1
•示例配置
<flow name="local2remote-ws">
<http:inbound-endpoint keep-alive="false" address="http://localhost:65000"
encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response" doc:name="HTTP"
doc:description="" />
<http:outbound-endpoint method="GET" keep-alive="false"
address="http://localhost:5050#[header:INBOUND:http.request]" responseTimeout="10000" encoding="UTF-8"
disableTransportTransformer="false" followRedirects="false" exchange-pattern="request-response"
doc:name="HTTP" doc:description="" />
</ flow >
•說明
注意 outbound-endpoint 中 address 參數中的表達式。
•測試方法
瀏覽器中通過“ http://localhost:65000/webservice/EchoService?wsdl ”(將內容復制,保存為 *.wsdl ),然后使用 SoapUI 測試。
2.2 配置方式2
•示例配置
<pattern:web-service-proxy name="ws-proxy" inboundAddress="http://localhost:65082/services/Echo2"
outboundAddress="http://localhost:65082/services/Echo1?method=echo">
</pattern:web-service-proxy>
•說明
Mule 為這種常見的場景提供了現成的模式,以簡化配置。
•測試方法
通過“ http://localhost:65082/services/Echo2?wsdl ”獲取 wsdl 文件,然后使用 SoapUI 測試。
3. Web Service to Web Service
Web Service To Web Service用於在Mule中提供Web Service供客戶端調用,Mule接收請求后調用遠端的Web Service進行處理,並返回結果。
•示例配置
<flow name="local-ws2remote-ws">
<core:inbound-endpoint address="http://localhost:65082/services/Echo8"
disableTransportTransformer="false" exchange-pattern="request-response" doc:name="Generic"
doc:description="Generic endpoint specified by address URI" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"
doc:description="Make a web service available via CXF" />
<core:outbound-endpoint
address="wsdl-cxf:http://server1:5050/mule-business/webservice/EchoService?wsdl&method=Echo" />
</ flow >
•說明
注意outbound-endpoint中address參數的配置方式,使用了wsdl-cxf前綴表示此web service是由cxf提供的。
•測試方法
在瀏覽器中輸入“ http://localhost:65082/services/Echo8/echo/text/hello ”進行測試。
4. Socket to Socket
Socket To Socket用於將客戶端的Socket請求轉發至遠程的Socket服務端處理,並返回處理結果。
•示例配置
<flow name="tcp2tcp">
<tcp:inbound-endpoint host="localhost" port="7100" responseTimeout="10000"
encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response" doc:name="TCP"
doc:description="The TCP transport enables events to be sent and received over TCP sockets." />
<tcp:outbound-endpoint host="localhost" port="7000" responseTimeout="10000"
encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response" doc:name="TCP"
doc:description="The TCP transport enables events to be sent and received over TCP sockets." />
</ flow >
•說明
主要配置 host 、 port 參數,表明服務地址。
•測試方法
通過 SimpleServer 和 SimpleClient 測試類,首先啟動 SimpleServer ,然后啟動 SimpleClient ,發送請求並接收處理結果。
5. JMS Topic
客戶端發送Web Service請求,Mule將請求消息發送至遠程JMS的Topic中。
•示例配置
<flow name="local-ws2jms-topic">
<core:inbound-endpoint address="http://localhost:65082/services/Echo3"
responseTimeout="10000" encoding="UTF-8" disableTransportTransformer="false" mimeType="text/plain"
exchange-pattern="one-way" doc:name="Generic" doc:description="Generic endpoint specified by address URI" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"
doc:description="Make a web service available via CXF" />
<jms:outbound-endpoint topic="topic1" responseTimeout="10000" encoding="UTF-8"
disableTransportTransformer="false" disableTemporaryReplyToDestinations="false" exchange-pattern="one-way"
connector-ref="activemqConnector" doc:name="JMS" doc:description="Send or receive messages from a JMS queue" />
</flow>
<flow name="jms-topic2echo">
<jms:inbound-endpoint topic="topic1" responseTimeout="10000" encoding="UTF-8"
disableTransportTransformer="false" disableTemporaryReplyToDestinations="false" exchange-pattern="one-way"
connector-ref="activemqConnector" doc:name="JMS" doc:description="Send or receive messages from a JMS queue" />
<echo-component doc:name="Echo" doc:description="Echoes message payload." />
</ flow >
•說明
JMS endpoint 是單向的,不需要返回值。通過 topic 屬性指定 JMS Server 的 Topic 名稱, connector-ref 指明了使用的 JMS 連接。
•測試方法
在瀏覽器地址欄中輸入“ http://localhost:65082/services/Echo3/echo/text/hello ”發送請求, Mule 控制台上輸出訂閱者的處理結果(上述示例中通過 Mule 配置了一個 JMS 的訂閱者)。也可以通過 ActiveMQ 的控制台,查看到 Topic 中增加了一條發布的消息。
二. 基於消息內容的路由
Mule提供基於消息內容的路由機制,根據消息中的指定信息,將消息發送至不同的服務端進行處理。
1. Socket to Socket 路由
•示例配置
<flow name="tcp2tcp-router">
<tcp:inbound-endpoint host="localhost" port="7101" responseTimeout="10000"
encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response" doc:name="TCP"
doc:description="The TCP transport enables events to be sent and received over TCP sockets." />
<choice>
<when evaluator="jxpath" expression="(req/area)='bj'">
<tcp:outbound-endpoint host="server1" port="7101"
responseTimeout="10000" encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response"
doc:name="TCP" doc:description="The TCP transport enables events to be sent and received over TCP sockets." />
</when>
<when evaluator="jxpath" expression="(req/area)='sh'">
<tcp:outbound-endpoint host="server1" port="7102"
responseTimeout="10000" encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response"
doc:name="TCP" doc:description="The TCP transport enables events to be sent and received over TCP sockets." />
</when>
</choice>
</ flow >
•說明
路由使用了 <choice> 、 <when> 元素,表示路由分支。 When 元素使用 evaluator 指明表達式的解析方式,使用 expression 描述消息內容的判斷條件。
•測試方法
同 Socket To Socket 測試,消息內容分別為 <req><area>bj</area></req> 、 <req><area>sh</area></req> ,查看發送至不同服務器的輸出。
2. Web Service to JMS Topic 路由
•示例配置
<flow name="local-ws2jms-topic-router">
<core:inbound-endpoint address="http://localhost:65082/services/Echo7"
disableTransportTransformer="false" exchange-pattern="request-response" doc:name="Generic"
doc:description="Generic endpoint specified by address URI" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"
doc:description="Make a web service available via CXF" />
<choice>
<when evaluator="jxpath" expression="(req/area)='bj'">
<jms:outbound-endpoint topic="topic1" responseTimeout="10000" encoding="UTF-8"
disableTransportTransformer="false" disableTemporaryReplyToDestinations="false"
exchange-pattern="one-way" connector-ref="activemqConnector" doc:name="JMS"
doc:description="Send or receive messages from a JMS queue" />
</when>
<when evaluator="jxpath" expression="(req/area)='sh'">
<jms:outbound-endpoint topic="topic2" responseTimeout="10000" encoding="UTF-8"
disableTransportTransformer="false" disableTemporaryReplyToDestinations="false"
exchange-pattern="one-way" connector-ref="activemqConnector" doc:name="JMS"
doc:description="Send or receive messages from a JMS queue" />
</when>
</choice>
</ flow >
•測試方法
通過“ http://localhost:65082/services/Echo7?wsdl ”獲取 wsdl 文件,然后通過 SoapUI 發送請求,查看返回結果。修改消息內容,查看結果的變化。
3. Web Service to Web Service 路由
•示例配置
<flow name="local-ws2jms-topic-router">
<core:inbound-endpoint address="http://localhost:65082/services/Echo9"
disableTransportTransformer="false" exchange-pattern="request-response" doc:name="Generic"
doc:description="Generic endpoint specified by address URI" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"
doc:description="Make a web service available via CXF" />
<choice>
<when evaluator="jxpath" expression="(req/area)='bj'">
<core:outbound-endpoint
address="wsdl-cxf:http://server1:5050/mule-business/webservice/CalcService?wsdl&method=processXml" />
</when>
<when evaluator="jxpath" expression="(req/area)='sh'">
<core:outbound-endpoint
address="wsdl-cxf:http://server2:5050/mule-business/webservice/CalcService?wsdl&method=processXml" />
</when>
</choice>
</ flow >
•測試方法
使用“ <![CDATA[<req><seq>1</seq><area>bj</area><price>123.45</price><count>10</count></req>]]> ”數據進行測試。
三. 數據轉換
1. 壓縮解壓
Mule原生提供了gzip壓縮方式的Transformer。
•示例配置
<flow name="gzip">
<stdio:inbound-endpoint ref="stdioInEndpoint" />
<core:string-to-byte-array-transformer encoding="UTF-8" />
<component class="demo.mule.component.Passthrough" />
<core:gzip-compress-transformer encoding="UTF-8" />
<component class="demo.mule.component.Passthrough" />
<core:gzip-uncompress-transformer encoding="UTF-8" />
<component class="demo.mule.component.Passthrough" />
<core:byte-array-to-string-transformer encoding="UTF-8" />
<stdio:outbound-endpoint ref="stdioOutEndpoint" />
</ flow >
•說明
gzip-compress-transformer 針對 byte[] 進行壓縮處理,因此對於字符串類型的消息,首先需要通過 string-to-byte-array-transformer 進行轉換。
•測試方法
在控制台的提示信息后輸入測試字符串,完成后控制台輸出同樣的信息。
2. 加密解密
加密、解密是一種特定的數據轉換方式,因此通過自定義Transformer的形式支持。
•示例配置
<flow name="encrypt">
<core:inbound-endpoint address="http://localhost:65082/services/Echo11"
responseTimeout="10000" encoding="UTF-8" disableTransportTransformer="false" mimeType="text/plain"
exchange-pattern="one-way" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" />
<component>
<singleton-object class="demo.mule.component.Echo" />
</component>
<core:custom-transformer class="demo.mule.transformer.DesEncryptTransformer" encoding="UTF-8" />
<component class="demo.mule.component.Passthrough" />
<core:custom-transformer class="demo.mule.transformer.DesDecryptTransformer" encoding="UTF-8" />
<stdio:outbound-endpoint ref="stdioOutEndpoint" />
</ flow >
•說明
DesEncryptTransformer 是自定義的壓縮轉換器, DesDecryptTransformer 是對應的解壓轉換器。
•測試方法
在瀏覽器地址欄中輸入“ http://localhost:65082/services/Echo11/echo/text/ 測試字符串”,在控制台中可見加密后的字符串和最終解密后與原串相同的字符串。
3. 自定義Transformer
•示例代碼
import demo.mule.dto.Envelope;
public class String2EnvelopeTransformer extends AbstractTransformer {
private static Log log = LogFactory.getLog(String2EnvelopeTransformer.class);
@Override
protected Object doTransform(Object src, String enc) throws TransformerException {
Envelope env = new Envelope();
if (src instanceof String) {
StringTokenizer st = new StringTokenizer((String) src, ",");
String area = st.nextToken();
String data = st.nextToken();
env.create(area, data);
}
log.debug(env);
return env;
}
}
•說明
自定義 Transformer 需要繼承 AbstractTransformer 類,實現其 doTransform 方法。該方法接收兩個參數,一個是消息對象,一個是消息的 Encoding ,方法拋出 TransformerException ,返回轉換后的消息對象。
•實例配置
<flow name="local-ws">
<core:inbound-endpoint address="http://localhost:65082/services/Echo1"
exchange-pattern="request-response" doc:name="Generic" doc:description="Generic endpoint specified by address URI" />
<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"
doc:description="Make a web service available via CXF" />
<component doc:name="Component" doc:description="Invoke a Java component">
<singleton-object class="demo.mule.component.Echo" />
</component>
<core:custom-transformer class="demo.mule.transformer.String2EnvelopeTransformer"></core:custom-transformer>
<core:outbound-endpoint address="stdio://System.out" exchange-pattern="one-way" />
</ flow >
•測試方法
在瀏覽器中輸入“ http://localhost:65082/services/Echo1/echo/text/bj,hello ”進行測試,觀察控制台輸出結果。