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 >
·说明
注意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&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 >
·测试方法
使用“ <![CDATA[<req><seq>1</seq><area>bj</area><price>123.45</price><count>10</count></req>]]> ”数据进行测试。