This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Sample 601 : Advance Rule Based Routing - Switching Routing Decision According to the Rules - Rule Mediator as Switch mediator

Note that WSO2 EI is shipped with the following changes to what is mentioned in this documentation:

  • <PRODUCT_HOME>/repository/samples/ directory that includes all Integration profile samples is changed to <EI_HOME>/samples/service-bus/.
  • <PRODUCT_HOME>/repository/samples/resources/ directory that includes all artifacts related to the Integration profile samples is changed to <EI_HOME>/samples/service-bus/resources/.

Objective:  Rule based routing - Switching routing decision according to the rules.

<!-- Simple rule based routing  of messages - same as filter mediator -->
<definitions xmlns="http://ws.apache.org/ns/synapse">

    <sequence name="main">

        <in>
            <rule xmlns="http://wso2.org/carbon/rules">
                <source>soapBody</source>
                <target action="replace" resultXpath="//accept/child::text()">$accept</target>
                <ruleSet>
                    <properties/>
                    <rule resourceType="regular" sourceType="inline">
                        <![CDATA[ package SimpleRoutingRules;

                        rule "Invoke IBM" no-loop true
                        when
                        symbol: String()eval( symbol.equals("IBM") )
                        then
                         update(drools.getWorkingMemory().getFactHandle(symbol),"ibmEndPoint");
                        end

                        rule "Invoke SUN" no-loop true
                        when
                        symbol: String()eval( symbol.equals("SUN") )
                        then
                         update(drools.getWorkingMemory().getFactHandle(symbol),"sunEndPoint");
                        end

                        rule "Invoke MFST" no-loop true
                        when
                        symbol: String()eval( symbol.equals("MFST") )
                        then
                         update(drools.getWorkingMemory().getFactHandle(symbol),"mfstEndPoint");
                        end
                        ]]>
                    </rule>
                </ruleSet>
                <input namespace="http://services.samples" wrapperElementName="getQuote">
                    <fact xmlns:m0="http://services.samples" elementName="symbol" namespace="http://services.samples"
                          type="java.lang.String" xpath="//m0:getQuote/m0:request/m0:symbol/child::text()"/>
                </input>
                <output namespace="http://services.samples" wrapperElementName="getQuoteRespone">
                    <fact elementName="accept" namespace="" type="java.lang.String"/>
                </output>
            </rule>


            <switch source="get-property('accept')">
                <case regex="ibmEndPoint">
                    <send>
                        <endpoint>
                            <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                        </endpoint>
                    </send>
                </case>
                <case regex="sunEndPoint">
                    <sequence key="nonExistentService"/>
                </case>
                <case regex="mfstEndPoint">
                     <sequence key="nonExistentService"/>
                </case>
            </switch>

            <drop/>

        </in>
        <out>
            <send/>
        </out>

    </sequence>

    <sequence name="nonExistentService" onError="myFaultHandler">

        <send>
            <endpoint>
                <address uri="http://localhost:9009/services/NonExistentStockQuoteService"/>
            </endpoint>
        </send>
        <drop/>

    </sequence>
    <sequence name="myFaultHandler">
        <makefault>
            <code value="tns:Receiver" xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/>
            <reason expression="get-property('ERROR_MESSAGE')"/>
        </makefault>

        <property name="RESPONSE" value="true"/>
        <header name="To" expression="get-property('ReplyTo')"/>
        <send/>
    </sequence>

</definitions>

 

Prerequisites:

  • Start the Synapse configuration numbered 601: i.e. (i.e. ./wso2esb-samples.sh -sn 601)
  • Start the Axis2 server and deploy the SimpleStockQuoteService if not already deployed
  • In rule script , there are three cases each for 'IBM','SUN' and 'MSFT'. When condition is match , then corresponding rule will be got fire.
Invoke IBM rule by running client as,

 

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ 

You will get stock quote price successfully

Then invoke SUN (or MSFT) rule by running client as,

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN

Then will get,

<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<faultcode>soapenv:Server</faultcode>
<faultstring>java.net.ConnectException: Connection refused
</faultstring>
<detail />
</soapenv:Fault>



 

Â