Mule ESB 介绍

博客分类:

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 ”进行测试,观察控制台输出结果。

Mule ESB 介绍的更多相关文章

  1. Mule ESB学习【转-结合了网络上的一些资源】

    1.SOA标准之一:SCA架构思想 SOA在Java领域有两套标准:一个是SUN推出的JBI(没有得到BEA和IBM的承认),另外一个是:IBM和BEA等公司推出的SCA和SDO标准. JBI之关注J ...

  2. EnjoyingSoft之Mule ESB开发教程系列第五篇:控制消息的流向-数据路由

    目录 1. 使用场景 2. 基于消息头的路由 2.1 使用JSON提交订单的消息 2.2 使用XML提交订单的消息 2.3 使用Choice组件判断订单格式 3. 基于消息内容的路由 4. 其他控制流 ...

  3. EnjoyingSoft之Mule ESB开发教程第一篇:初识Mule ESB

    目录 1. Mule ESB基本介绍 2. Mule ESB社区版和企业版 3. Mule ESB常用场景 4. Mule ESB软件安装 客户端安装 服务端安装 5. 第一个Mule ESB应用- ...

  4. EnjoyingSoft之Mule ESB开发教程第二篇:Mule ESB基本概念

    目录 1. 使用Anypoint Studio开发 2. Mule ESB Application Structure - Mule ESB应用程序结构 3. Mule ESB Application ...

  5. EnjoyingSoft之Mule ESB开发教程第四篇:Mule Expression Language - MEL表达式

    目录 1. MEL的优势 2. MEL的使用场景 3. MEL的示例 4. MEL的上下文对象 5. MEL的Variable 6. MEL访问属性 7. MEL操作符 本篇主要介绍Mule表达式语言 ...

  6. EnjoyingSoft之Mule ESB开发教程第六篇:Data Transform - 数据转换

    目录 1. 数据转换概念 2. 数据智能感知 - DataSense 3. 简单数据转换组件 3.1 Object to JSON 3.2 JSON to XML 3.3 JSON to Object ...

  7. Mule ESB 安装基本配置要求

    Hardware Requirements* 2GHz, dual-core CPU, or 2 virtual CPUs in virtualized environments 2GB of RAM ...

  8. EnjoyingSoft之Mule ESB开发教程第三篇:Mule message structure - Mule message结构

    目录 1. 探索Mule Message结构 2. Mule Message的Payload 3. Mule Message的Property 4. Mule Message的Attachment 5 ...

  9. Mule ESB 自带例子hello初体验

    1 配置的流的效果图 2 应用配置文件hello.xml内容 <?xml version="1.0" encoding="UTF-8"?> < ...

随机推荐

  1. Java使用HttpClient上传文件

    Java可以使用HttpClient发送Http请求.上传文件等,非常的方便 Maven <dependency> <groupId>org.apache.httpcompon ...

  2. ORACLE异常处理及函数

    有三种类型的异常错误 :预定义 ( Predefined )错误 ORACLE预定义的异常情况大约有24个.对这种异常情况的处理,无需在程序中定义,由ORACLE自动将其引发. 非预定义 ( Pred ...

  3. 标准正态分布表(scipy.stats)

    0. 标准正态分布表与常用值 Z-score 是非标准正态分布标准化后的 x即 z=x−μσ" role="presentation">z=x−μσz=x−μσ 表 ...

  4. logminer使用测试库进行挖掘分析,10.2.0.5

    上一篇测试是在dg环境进行测试挖掘,但是如果客户存在一个测试库,那样使用日志挖掘的影响性更小.本篇进行测试分析. 测试环境介绍: oracle linux  5.6,vmware虚拟机,安装两套单实例 ...

  5. Python之路,第十一篇:Python入门与基础11

    python3  函数2 全局变量:一直存在 局部变量:函数执行时存在,执行完毕后销毁: lambda 表达式(又称匿名函数表达式) 作用: 创建一个匿名(无名)函数对象, 同 def 类似但不提供函 ...

  6. jquery使用ajax提交form表单

    $.ajax({ type: jqform.attr('method'), // 提交方式 get/post url: jqform.attr('action'), // 需要提交的 url data ...

  7. Unity 项目中委托Delegate用法案例

    Unity中Delegate的用法场景 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar - ...

  8. HPU组队赛B:问题(二进制枚举)

    时间限制1 Second 内存限制 512 Mb 题目描述 你有n个问题,你已经估计了第i个问题的难度为Ci,现在你想使用这些问题去构造一个问题集.比赛的问题集必须包含至少两个问题,而且比赛的总难度必 ...

  9. C++学习(三十四)(C语言部分)之 链表

    1.栈和队列 操作 增查改删重点 插入删除先进先出 -->队列先进后出 -->栈2.链表 写之前先画图存储数据的方式 通过指针将所有的数据链在一起数据结构的目的 管理存储数据 方便快速查找 ...

  10. LeetCode – Number of Islands

    Given a -d grid map of 's (water), count the number of islands. An island is surrounded by water and ...