这篇文章介绍如何通过WSO2 ESB实现消息路由的功能:基于内容的路由,根据消息内容的不同路由到不同的服务进行处理。
场景:投诉管理
描述:公众发起对律师或公证员的投诉,输入0转到律师服务进行处理,输入1转到公证员服务进行处理,其他值转到默认服务进行处理
3个服务:律师服务、公证员服务、默认服务
1 三个Axis2服务
默认服务Route:http://10.22.1.20:9763/services/Route?wsdl
package service.hello.ws.demo.com; public class Route{ public String litigate(String msg) { System.out.println("Route Test: " + msg + " coming! return 'Route: Hello " + msg + "'"); return "litigate Type " + msg; } }
律师服务Lawyer:http://10.22.1.20:9763/services/Lawyer?wsdl
package service.hello.ws.demo.com; public class Lawyer{ public String litigate(String msg) { System.out.println("Lawyer: " + msg + " coming! return 'Lawyer Litigation " + msg + "'"); return "Lawyer Type "+msg; } }
公证员服务Notary:http://10.22.1.20:9763/services/Notary?wsdl
package service.hello.ws.demo.com; public class Notary{ public String litigate(String msg) { System.out.println("Notary: " + msg + " coming! return 'Notary Litigation " + msg + "'"); return "Notary Type" +msg; } }
2 三个服务的三个端点
为上述三个服务分别建立端点。
3 一个代理服务RouteProxy
<?xml version="1.0" encoding="UTF-8"?> <proxy xmlns="http://ws.apache.org/ns/synapse" name="RouteProxy" transports="https,http" statistics="enable" trace="enable" startOnLoad="true"> <target inSequence="routeInSequence" outSequence="routeOutSequence"/> <publishWSDL uri="http://10.22.1.20:9763/services/Route?wsdl"/> <description/> </proxy>
3.1 routeInSequence
<pre name="code" class="html"><span style="font-weight: normal;"><?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse"
name="routeInSequence"
trace="enable"
statistics="enable">
<switch xmlns:ns="http://org.apache.synapse/xsd"
xmlns:p="http://com.demo.ws.hello.service"
source="$body//p:litigate/p:msg">
<case regex="0">
<property name="STATE" value="0" scope="default" type="STRING"/>
<send>
<endpoint key="RouteLawyerEndpoint"/>
</send>
</case>
<case regex="1">
<property name="STATE" value="1" scope="default" type="STRING"/>
<send>
<endpoint key="RouteNotaryEndpoint"/>
</send>
</case>
<default>
<send>
<endpoint key="RouteEndpoint"/>
</send>
</default>
</switch>
</sequence></span>
3.2 routeOutSequence
<sequence xmlns="http://ws.apache.org/ns/synapse" name="routeOutSequence" trace="enable"> <switch xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd" source="get-property('STATE')"> <case regex="0"> <log level="full"> <property name="Sequence" value="0"></property> </log> <send></send> </case> <case regex="1"> <log level="full"> <property name="Sequence" value="1"></property> </log> <send></send> </case> <default> <send></send> </default> </switch> </sequence>
4 测试
输入0
输入1
输入其他值
具体参考 http://download.csdn.net/detail/szh1124/8447887
时间: 2024-10-10 08:12:58