目录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 社区版 实施帮助。
欢迎转载,但必须保留原文和此段声明,且在文章页面明显位置给出原文连接,不然保留追究法律责任的权利。