写之前的内容时,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 >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 >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>
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&method=Echo" />
</ flow >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 >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 >
二. 基于消息内容的路由
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 >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 >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&method=processXml" />
</when>
<when evaluator="jxpath" expression="(req/area)='sh'">
<core:outbound-endpoint
address="wsdl-cxf:http://server2:5050/mule-business/webservice/CalcService?wsdl&method=processXml" />
</when>
</choice>
</ flow >
三. 数据转换
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 >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 >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;
}
}<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 >
仅以上述示例代码作为这一段时间Mule文档阅读及试用的总结。
PS:程序代码插件有时有点问题,前几段代码字体是CN,后边就突然变成一种非等宽字体了。而且XML的格式不是很直观,因此直接粘贴了。。