写之前的内容时,Mule刚刚3.0.1版本,很多官方文档还没有更新(尤其示例代码),维持在V2的状态。经过了一年多的时间,Mule社区版发展至了3.2版本,并且推出了Mule Studio可视化开发工具(当前beta状态,支持Mule 3.1.2)。

将以前自己验证的示例代码在3.1.2版本上又跑了一遍(有些变化),在此做一下记录。


一. 服务调用

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;

publicclassString2EnvelopeTransformerextendsAbstractTransformer{

privatestaticLoglog=LogFactory.getLog(String2EnvelopeTransformer.class);

@Override

protectedObjectdoTransform(Objectsrc,Stringenc)throws TransformerException {

Envelopeenv=newEnvelope();

if(srcinstanceofString){

StringTokenizerst=newStringTokenizer((String)src,",");

Stringarea=st.nextToken();

Stringdata=st.nextToken();

env.create(area,data);

}

log.debug(env);

returnenv;

}

}

  • 说明
  • 自定义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 学习笔记(11)Web Service Proxy(这里是一个可以正常运行的例子)

    一.WebSevice Proxy 简介      WebService Proxy 是ESB中最常见的使用场景之一,即通过 ESB 直接转发 WebService Client 的 SOAP 请求, ...

  2. Mule ESB 学习笔记

    写之前的内容时,Mule刚刚3.0.1版本,很多官方文档还没有更新(尤其示例代码),维持在V2的状态.经过了一年多的时间,Mule社区版发展至了3.2版本,并且推出了Mule Studio可视化开发工 ...

  3. jboss esb 学习笔记与html的传输,Jboss_ESB学习笔记1.doc

    Jboss_ESB学习笔记1 JBoss ESB学习笔记1--搭建ESB开发环境 最近由于工作需要用到了JBoss ESB,可在网上这方面的资料实在少的让人不敢恭维,而JBoss ESB的官方文档对初 ...

  4. ESB学习笔记(Spring Integration实战)

    http://wangleifire.iteye.com/blog/351749 介绍 Spring Integration是Spring公司的一套ESB框架. 前面ESB介绍中我也做了一定了解.我们 ...

  5. Mule ESB开发与学习

    今天依旧是公司让调研,嗯嗯,学无止境 还是上次的问题,由于公司用了IBM的IIB,价格比较昂贵,所以调研一个开源的ESB用于小项目来使用,既然是小项目,估计也就用不到高可用这种集群模式了,所以老板让我 ...

  6. mule esb 集成_集成框架比较– Spring集成,Mule ESB或Apache Camel

    mule esb 集成 公司之间的数据交换增加了很多. 必须集成的应用程序数量也增加了. 这些接口使用不同的技术,协议和数据格式. 然而,这些应用程序的集成应以标准化的方式建模,有效实现并由自动测试支 ...

  7. 集成框架比较– Spring集成,Mule ESB或Apache Camel

    公司之间的数据交换增加了很多. 必须集成的应用程序数量也增加了. 这些接口使用不同的技术,协议和数据格式. 但是,这些应用程序的集成应以标准化的方式建模,有效实现并由自动测试支持 . JVM环境中提供 ...

  8. 数据治理专业认证CDMP学习笔记(思维导图与知识点)- 第八章数据集成和互操作篇...

    大家好,我是独孤风,一位曾经的港口煤炭工人,目前在某国企任大数据负责人,公众号大数据流动主理人.在最近的两年的时间里,因为公司的需求,还有大数据的发展趋势所在,我开始学习数据治理的相关知识. 数据治理 ...

  9. Spring Cloud 学习笔记一 微服务架构

    文章目录 前言 一.微服务 二.微服务的特点 1.小型化 2.自治化 3. 扁平化 4. 轻量级设计 5. 渐进式设计 微服务架构和整体式架构的区别 微服务架构和SOA的比较 区别一:微服务通信的轻量 ...

最新文章

  1. [WARNING] unable to add QUERY_STRING=XXXX to uwsgi packet, consider increasing buffer size
  2. 过年回家应对七大姑八大姨,程序猿绝了!
  3. cad怎么查找未闭合_CAD无法填充的这四种方法肯定能解你燃眉之急
  4. 信息系统项目管理知识--项目范围管理
  5. 修改显卡型号软件_马甲显卡又双叕来了 怎么选知道吗?
  6. 使用Freemarker来页面静态化,与Spring整合使用
  7. linux ftp 操作命令
  8. Xamarin.Android 引导页
  9. python+php+变量传递,将变量从php传递给python,将python传递给php
  10. IntelliJ Idea 主题(黑色)+代码高亮显示
  11. 华为电脑管家PcManager多屏协同功能破解
  12. 【SDK】Android海康网络SDK实现人脸和车牌等报警图片抓拍功能
  13. Python中的乘方计算
  14. 微信小程序页面栈超过【10层】之后无法打开其他页面原因
  15. linux还原数据库报错,RMAN还原数据库报错问题解决案例
  16. Openstack 简体中文汉化
  17. 走进Linux操作系统世界
  18. 【C语言】平均分的战争。有m个人的成绩存放在score数组中,请编写程序求出平均成绩并且罗列出低于平均分的分数。
  19. Mysql模拟银行数据库系统
  20. antvue table 表格实现 首行搜索

热门文章

  1. mysql数据库入门教程(12):变量讲解大全
  2. VTK:直线用法实战
  3. VTK:图像卷积用法实战
  4. JavaScript实现QuickSort快速排序算法(附完整源码)
  5. JavaScript实现shortestCommonSupersequence最短公共超序列算法(附完整源码)
  6. wxHtml 示例:帮助测试
  7. boost::polygon模块实现多边形点相关的测试程序
  8. boost::fusion::nil用法的测试程序
  9. 测试lazy_enable_if的所有变体
  10. Boost:prefer的使用测试程序