Objective: Introduction to simple rule based routing.
<!-- Simple rule based routing of messages - same as filter mediator --> <definitions xmlns="http://ws.apache.org/ns/synapse"> <in> <rule> <ruleset > <source> <package name="SimpleRoutingRules" xmlns="http://drools.org/drools-5.0" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="http://drools.org/drools-5.0 drools-4.0.xsd"> <import name="org.apache.synapse.MessageContext"/> <rule name="Invoke IBM "> <lhs> <pattern object-type="MessageContext" identifier="mc"></pattern> <pattern object-type="String" identifier="symbol"></pattern> <eval>symbol.equals("IBM")</eval> </lhs> <rhs>mc.setProperty("execute_children","true");</rhs> </rule> </package> </source> <creation> <property name="source" value="xml"/> </creation> </ruleset> <session type="stateless"/> <facts> <fact name="mc" type="context"/> <fact name="symbol" type="java.lang.String" expression="//m0:getQuote/m0:request/m0:symbol/child::text()" xmlns:m0="http://services.samples"/> </facts> <childMediators> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> <drop/> </childMediators> </rule> </in> <out> <send/> </out> </definitions>
Prerequisites:
Run the client as
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/
Or as
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=IBM
Then, you will get stock quote price in the client side.
In this sample , with in the rule mediator , the value, which is extracted from the SOAP envelope using XPath '//m0:getQuote/m0:request/m0:symbol', is added to the rule engine as a string fact . The message context property named "execute_children" is to indicate it is need to execute the child mediators of rule mediator. That property is set only if the symbol is IBM.
Now use following commands
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN
and
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT
Then , you will get errors . This is because , 'symbol' is not equal to "IBM".