EnjoyingSoft之Mule ESB開發教程第三篇:Mule message structure - Mule message結構


Mule ESB是一個使用Java語言編寫的開源企業服務總線,企業服務總線英文Enterprise Service Bus,簡稱ESB。其相關源代碼也托管在GitHub上,可以在https://github.com/mulesoft/mule這里找到相關的Source Code。

MuleESB在眾多開源的ESB中處於領先者的地位,已擁有超過數百萬的下載量,以及來自世界各地數十萬個開發人員。MuleSoft公司也作為開源軟件中的獨角獸,2017年在紐交所成功上市。我們作為MuleSoft的重要合作伙伴也參與其中,在六年多的時間里,使用Mule ESB社區版實施,或者Mule ESB企業版實施,構建眾多Mule ESB開發案例,幫助國內眾多的企業成功上線企業集成項目。

我們使用Mule ESB開發的過程中,體會到它優秀的架構設計和高效的開發速度。同時也深感Mule ESB書籍,Mule ESB中文文檔資料非常稀少,所以使用8篇文章來寫Mule ESB的基礎課程系列,講解Mule ESB功能和開發。

1. 探索Mule Message結構

很多開發者在開始使用Mule開發,很大原因是因為Mule的圖形化開發環境非常友好,同時Mule Esb Transport也非常多,但對Mule最重要的Mule message概念並不特別熟悉。本篇重點講解Mule的Message。

上一篇教程中已經說到,Flow的結構和構成元素,在Flow中流動的就是Mule Message。

Mule Message是一個數據結構,也有相對應的Java Class。它包括幾部分Payload,Property,Attachment。如下圖所示:

如何理解這幅圖,大致可以和HTTP協議類比。

  • Property

    Mule Message的Property又分成Inbound Properties和Outbound Properties。這一點類似於HTTP協議的請求頭和響應頭。

  • 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="#[&quot;Mule Message&quot;]" 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:
}

2. Mule Message的Payload

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="#[&quot;Mule Message&quot;]" doc:name="Set Payload"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>

3. Mule Message的Property

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="#[&quot;userName&quot;]" value="#[&quot;Tom&quot;]" doc:name="Property"/>
</flow>

4. Mule Message的Attachment

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="#[&quot;doc&quot;]" value="#[payload]" contentType="application/pdf" doc:name="Attachment"/>
</flow>

5. Mule的Variable

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="#[&quot;1238&quot;]" doc:name="Variable"/>
</flow>

6. 使用Java操作Mule Message

對程序員來說,千言萬語不如代碼,如何使用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 BlogsCSDN簡書

訪問EnjoyingSoft 網站,獲取更多Mule ESB 社區版 實施幫助。

歡迎轉載,但必須保留原文和此段聲明,且在文章頁面明顯位置給出原文鏈接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM