Child pages
  • Sample 600 : Simple Message Transformation - Rule Mediator for Message Transformation

Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.

Objective: Introduction to simple rule based routing Simple message transformation - Rule mediator for message transformation.

Code Block
<!-- Simple rule based routingtransformation (changing of messages - same as filter mediator message )  -->
<definitions xmlns="">

   <in> <sequence name="main">
      <rule>  <in>

         <ruleset >  <rule xmlns="">
             <source>   <source>soapBody</source>
                <target <package nameaction="SimpleRoutingRulesreplace"                              xmlnsxmlns:m0="" resultXpath="//m0:symbol"
            xs:schemaLocation=" drools-4.0.xsd">                         <import name="org.apache.synapse.MessageContext"<properties/>
                        <rule nameresourceType="regular"Invoke IBM  sourceType="inline">
                        <![CDATA[ package SimpleRoutingRules;

<lhs>                        rule InvokeIBM
       <pattern object-type="MessageContext" identifier="mc"></pattern>               when
                 <pattern object-type="String" identifier="symbol"></pattern>     symbol: String()
                        eval(  <eval>symbolsymbol.equals("IBMMSFT")</eval> || symbol.equals("SUN") )
</lhs>                             <rhs>mc.setProperty("execute_children","true");</rhs>update(drools.getWorkingMemory().getFactHandle(symbol),"IBM");
 </rule>                      </package> ]]>
               </source>     </rule>
           <creation>     </ruleSet>
               <property <input namenamespace="sourcehttp://services.samples" valuewrapperElementName="xmlgetQuote"/>
                </creation>             </ruleset>
            <session type="stateless"/>
<fact xmlns:m0="http://services.samples" elementName="symbol" namespace="http://services.samples"
           <facts>                 <fact nametype="mcjava.lang.String" typexpath="context//m0:getQuote/m0:request/m0:symbol/child::text()"/>
                <fact name="symbol" type="java.lang.String"</input>
                        expression<output namespace="http://m0:getQuote/m0:request/m0:symbol/child::text()"services.samples" wrapperElementName="getQuote">
                    <fact  xmlns:m0elementName="symbol" namespace="http://services.samples" type="java.lang.String"/>
            </facts>    </output>
        <childMediators>    </rule>
                        <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>

   </childMediators>         <<send/rule>>
    </in>     <out>



  • Start the Synapse configuration numbered 600: (i.e. ./ -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 the client as

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

Or as

Code Block
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=IBMSUN

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

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


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


check the axis2server log or console

Code Block
Wed July 04 16:33:05 IST 2012 :: Generating quote for : IBM