Running the Rule samples with WSO2 Enterprise Service Bus (ESB)

Introduction

Rule mediator integrates the WSO2 rules component into the WSO2 ESB to provide you with a suitable capability to define your organization's dynamic integration decisions in terms of rules.This guide provides you with two samples, which are quite simple. The goal of the samples is to make you an expertise in using rule mediator. Please follow the configuration guide of the rule mediator before proceeding with the samples if you have not already done so.

Sample 600 : Simple Message Transformation - Rule Mediator for Message Transformation

<!-- Simple rule based transformation (changing message )  -->
<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" xmlns:m0="http://services.samples" resultXpath="//m0:symbol"
                        xpath="//m0:getQuote/m0:request/m0:symbol">soapBody</target>
                <ruleSet>
                    <properties/>
                    <rule resourceType="regular" sourceType="inline">
                        <![CDATA[ package SimpleRoutingRules;

                        rule InvokeIBM
                        when
                        symbol: String()
                        eval( symbol.equals("MSFT") || symbol.equals("SUN") )
                        then
                        update(drools.getWorkingMemory().getFactHandle(symbol),"IBM");
                        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="getQuote">
                    <fact elementName="symbol" namespace="http://services.samples" type="java.lang.String"/>
                </output>
            </rule>
            <send>
                <endpoint>
                    <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                </endpoint>
            </send>

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

</definitions>

Objective: Simple message transformation - Rule mediator for message transformation.

Prerequisites:

Start the Synapse configuration numbered 600: (i.e. ./wso2esb-samples.sh -sn 600)

Start the Axis2 server and deploy the SimpleStockQuoteService if not already deployed

In this sample , a simple transformation is happened . If the symbol is either SUN or MSFT , then it will changed into IBM in the SOAP envelope and then invoke the external service.

Run client as

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

Or as

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

Then check the axis2server log or console .

Wed July 04 16:33:05 IST 2012 samples.services.SimpleStockQuoteService :: Generating quote for : IBM

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

<!-- 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>

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

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>