Mule ESB 介绍

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&amp;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&amp;method=processXml" />

        </when>
        <when evaluator="jxpath" expression="(req/area)='sh'">

            <core:outbound-endpoint
                address="wsdl-cxf:http://server2:5050/mule-business/webservice/CalcService?wsdl&amp;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 ”进行测试,观察控制台输出结果。


猜你喜欢

转载自maosheng.iteye.com/blog/1929156