目錄html
Mule ESB是一個使用Java語言編寫的開源企業服務總線,企業服務總線英文Enterprise Service Bus,簡稱ESB。其相關源代碼也託管在GitHub上,能夠在https://github.com/mulesoft/mule這裏找到相關的Source Code。java
MuleESB在衆多開源的ESB中處於領先者的地位,已擁有超過數百萬的下載量,以及來自世界各地數十萬個開發人員。MuleSoft公司也做爲開源軟件中的獨角獸,2017年在紐交所成功上市。咱們做爲MuleSoft的重要合做夥伴也參與其中,在六年多的時間裏,使用Mule ESB社區版實施,或者Mule ESB企業版實施,構建衆多Mule ESB開發案例,幫助國內衆多的企業成功上線企業集成項目。git
咱們使用Mule ESB開發的過程當中,體會到它優秀的架構設計和高效的開發速度。同時也深感Mule ESB書籍,Mule ESB中文文檔資料很是稀少,因此使用8篇文章來寫Mule ESB的基礎課程系列,講解Mule ESB功能和開發。程序員
不少開發者在開始使用Mule開發,很大緣由是由於Mule的圖形化開發環境很是友好,同時Mule Esb Transport也很是多,但對Mule最重要的Mule message概念並不特別熟悉。本篇重點講解Mule的Message。github
在上一篇教程中已經說到,Flow的結構和構成元素,在Flow中流動的就是Mule Message。spring
Mule Message是一個數據結構,也有相對應的Java Class。它包括幾部分Payload,Property,Attachment。以下圖所示:json
如何理解這幅圖,大體能夠和HTTP協議類比。數據結構
Property架構
Mule Message的Property又分紅Inbound Properties和Outbound Properties。這一點相似於HTTP協議的請求頭和響應頭。app
Payload
Mule的Payload是一個對象,類型是不固定的。多是Stream,也多是Hashmap,也多是XML字符串。這一點相似於HTTP協議的請求正文,或者說是請求體。
Attachment
Mule的Attachment就是消息的附件,這一點相似於HTTP協議中的multipartform-data請求。
若是你想看到整個MuleMessage的結構,使用Mule的Logger組件能夠很方便的看到Message完整的組成。使用Logger打印出message,logger組件會重載message的toString方法,打印出Pretty格式的message。
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd"> <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/> <flow name="loggertestFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/> <set-payload value="#["Mule Message"]" doc:name="Set Payload"/> <logger message="#[message]" level="INFO" doc:name="Logger"/> </flow> </mule>
咱們能夠從下圖的記錄中找到和上圖Message Structure相對應的節點。出於篇幅緣由,作了簡略處理。
org.mule.DefaultMuleMessage { id=f88d0090-074c-11e9-89b7-0c5415358ba9 payload=java.lang.String correlationId=<not set> correlationGroup=-1 correlationSeq=-1 encoding=UTF-8 exceptionPayload=<not set> Message properties: INVOCATION scoped properties: INBOUND scoped properties: accept=*/* accept-encoding=gzip, deflate, br accept-language=zh-CN,zh;q=0.9,en;q=0.8 cache-control=no-cache connection=keep-alive content-length=2 content-type=text/plain;charset=UTF-8 host=localhost:8081 http.listener.path=/ http.method=POST http.query.params=ParameterMap{[]} http.query.string= http.relative.path=/ http.remote.address=/127.0.0.1:57630 http.request.path=/ http.request.uri=/ http.scheme=http http.uri.params=ParameterMap{[]} http.version=HTTP/1.1 SESSION scoped properties: }
Payload翻譯成中文是負荷,負載的意思。它是Mule Message的主要部分,也是Mule處理的主要對象。咱們後續說的數據轉換就是對Payload的轉換。注意Mule Message的Payload是有可能爲空的,好比接收到一個Http Get請求,Http Get請求的請求體是空的,因此這個時候Mule Message的Payload是空的。
在Flow中,最經常使用的動做就是給payload賦值,給Payload賦值會使用set-payload組件。若是咱們在Flow中想獲取payload,可使用MEL表達式。
下面的源代碼表示payload的賦值和取值。
<flow name="payloadFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/> <set-payload value="#["Mule Message"]" doc:name="Set Payload"/> <logger message="#[payload]" level="INFO" doc:name="Logger"/> </flow>
Mule Message的Property是一個鍵值對,有name和對應的value。Mule Message有兩種類型的Property,Inbound Properties和Outbound Properties。Inbound Properties或者Outbound Properties能夠有多個Property,也就是多個鍵值對。
Inbound Properties是不可變的,是由Message Source產生的。就相似於Http的請求參數,是由用戶的數據請求,通過Java的Servlet,或者Asp.Net等框架封裝成Http Request對象。
Outbound Properties是可變的,咱們在Mule的Flow中新增或者改變這些屬性。注意,好比轉換器,有些Mule Processor會自動增長有些屬性。
在Mule中設定Property使用set-property組件,若是須要獲取,一樣使用MEL表達式。詳細的MEL表達式,咱們下篇會展開講解。
<flow name="propertyFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/> <set-property propertyName="#["userName"]" value="#["Tom"]" doc:name="Property"/> </flow>
Attachment,正如字面上意思,能夠理解成消息的附件。想象一封郵件,有郵件發送人等頭信息,也有郵件正文,一樣還有郵件附件。和Property同樣,Attachment也有兩種類型,Inbound Attachment和Outbound Attachment。咱們一般將一些大的對象做爲附件傳輸。
使用set-attachment設置附件,這裏將payload做爲pdf文檔附件供消費者下載。
<flow name="attachmentFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/> <set-attachment attachmentName="#["doc"]" value="#[payload]" contentType="application/pdf" doc:name="Attachment"/> </flow>
Variable也就是變量,有幾種類型的變量,或者說幾種不一樣範圍的變量,以下:Flow Variable,Session Variable,Record Variable。Flow Variable在一個Flow是有效的,Session Variable是能夠跨Flow的,Record Variable則是處理數據列表時會用到。
這裏不詳細講述。從使用上說,有些相似於Java裏面的局部變量,Session變量,但不徹底一致。後續實戰文章會分析這一點。
在Mule裏,使用set-variable和MEL表達式對變量作賦值和取值操做。
<flow name="variableFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/> <set-variable variableName="orderNo" value="#["1238"]" doc:name="Variable"/> </flow>
對程序員來講,千言萬語不如代碼,如何使用Java操做Mule Message呢?經過Java代碼咱們能夠清楚的看到Mule Message的結構,成員變量和方法等。
public void explorMessage(MuleMessage message) { // 獲取InboundProperty String requestPath = message.getInboundProperty("http.request.path"); // 設定OutboundProperty message.setOutboundProperty("content-type", "application/json"); // 獲取Payload Object payload = message.getPayload(); // 獲取InboundAttachment DataHandler fileAttachment = message.getInboundAttachment("fileName"); // 獲取flow變量 message.getProperty("flowVarTest", PropertyScope.INVOCATION); }
下圖是Mule Message的類圖,類圖中只列表了重要的方法和屬性。
本文同步發文於EnjoyingSoft Blogs ,CSDN,簡書
訪問EnjoyingSoft 網站,獲取更多Mule ESB 社區版 實施幫助。
歡迎轉載,但必須保留原文和此段聲明,且在文章頁面明顯位置給出原文連接,不然保留追究法律責任的權利。