6. 配置模式java
Mule 3.0版本提供了「pattern」的機制。Pattern總結了實際使用過程當中的常見場景,以簡化的服務配置方式提供。web
6.1 簡單服務模式(simple service pattern)異步
簡單服務模式用於簡化同步服務調用的配置,對應消息傳遞方式中的請求-響應方式。spa
圖 簡單服務模式代理
簡單服務模式經過simple-service 元素配置,主要的元素屬性包括:rest
屬性 | 說明 |
address | 服務監聽的地址,如vm:in |
component-class | Component的實現類 |
type | direct: 默認;code jax-ws: 將component暴露爲soap式的web service(component必須基於jax-ws的註解),address通常爲Http Transport;component jax-rs: 將component暴露爲rest式的web service(component必須基於@Path的註解),address通常爲Http或Servlet Transport |
代碼示例:orm
<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服務代理模式
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配置進行數據轉換。
to be continued...