The content in this documentation is for older versions of WSO2 products. For updated information on Enterprise Integration Patterns, go to the latest Micro Integrator documentation. "> The content in this documentation is for older versions of WSO2 products. For updated information on Enterprise Integration Patterns, go to the latest Micro Integrator documentation.

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 46 Next »

This section explains, through an example scenario, how the Message Router EIP can be implemented using the WSO2 ESB. The following topics are covered:

Introduction to Message Router

The Message Router EIP reads the content of a message and routes it to a specific recipient based on its content. When the implementation of a specific logical function is distributed across multiple physical systems, an incoming request needs to be passed on to the correct service based on the request's content. A Message Router is useful in handling such scenarios.

The following diagram depicts the Message Router's behavior where the router performs a logical function (such as an inventory check). It receives a request message (new order), reads it, and routes the request to one of the two recipients according to the message's content. The router is also defined as a special type of a filter.

For more information, go to Message Router.







Figure 1: Message Router EIP

Sample scenario

The example scenario depicts an inventory for stocks, and how Message Router EIP routes a message to a different service based on the message's content. When the router receives a stock request, it reads the content of the request. If the request is made to foo, it is routed to fooOutQueue. If the request is for bar, it is routed to barOutQueue.

The diagram below depicts how to simulate the example scenario using the ESB profile of WSO2 EI.

Figure 2: Example Scenario of the Message Router EIP

Before digging into implementation details, let's take a look at the relationship between the example scenario and the Message Router EIP by comparing their core components.

Message Router EIP (Figure 1)Message Router Example Scenario (Figure 2)

Incoming message

Stock Quote Request

Message router


The Switch and Send mediators of the ESB simulates the Message Router EIP. The Switch Mediator depicts the Router and observes the content of the message, while the Send Mediator sends the message to a selected recipient.

Each case defined should decide on routing the message to the appropriate service.

Outgoing queues

fooOutQueue and barOutQueue act as two separate services in the example scenario.

The ESB configuration

Given below is the ESB configuration of this sample.

<definitions xmlns="http://ws.apache.org/ns/synapse">
   <!-- Receiving sequence which will be the message router -->
   <sequence name="message-router-sequence">
       <!-- Would analyze the data for filtering -->
       <switch xmlns:m0="http://services.samples" source="//m0:getQuote/m0:request/m0:symbol">
           <!-- If the content is "foo" -->
           <case regex="foo">
               <!-- Sends the information to the fooOutQueue -->
               <send>
                   <endpoint>
                       <address uri="http://localhost:9000/services/SimpleStockQuoteService?wsdl"/>
                   </endpoint>
               </send>
           </case>
           <!-- If the content is "bar" -->
           <case regex="bar">
               <!-- Sends the information to the barOutQueue -->
               <send>
                   <endpoint>
                       <address uri="http://localhost:9001/services/SimpleStockQuoteService?wsdl"/>
                   </endpoint>
               </send>
           </case>
           <default>
               <property name="symbol" expression="fn:concat('Normal Stock - ', //m0:getQuote/m0:request/m0:symbol)"/>
           </default>
       </switch>
   </sequence>
   <proxy name="message-router-proxy" startOnLoad="true" transports="http https">
       <target>
           <inSequence>
               <!-- Will call the message router -->
               <sequence key="message-router-sequence"/>
           </inSequence>
           <outSequence>
               <respond/>
           </outSequence>
           <description>Message Router</description>
       </target>
   </proxy>
</definitions>

How the implementation works

Let's investigate the elements of the configuration in detail.

  • proxy service - The proxy service, which should be invoked to execute the configuration.
  • in sequence - The message is directed to this when it is received by the proxy service.
  • out sequence - This is triggered after execution of the in sequence.
  • sequence - An external sequence, which is invoked by the proxy.
  • switch - Observes the message and filters out the content to the given xPath expression.
  • case - The filtered content will be matched with the specified regular expression.
  • send - When a matching case for foo is found, the message is routed to the specified endpoint indicated in the address URI.
  • send - When a matching case for bar is found, the message is routed to the specified endpoint indicated in the address URI.
  • default - If a matching condition is not found, the message will be diverted to the default case.

Simulating the sample scenario

Now, let's try out the sample scenario explained above.

Setting up the environment

Download the Message-Router_1.0.0.zip file, which includes the artifacts of this sample and follow the steps in  Setting up the Environment, to set up the environment.

You need to start two instances of the Axis2 server to run in ports 9000 and 9001 by executing the following commands:

  • ./axis2server.sh -http  9000 -https  9002 -name MyServer1
  • ./axis2server.sh -http  9001 -https  9003 -name MyServer2

Executing the sample

In the Terminal, navigate to the <EI_HOME>/samples/axis2Client/ directory, and execute the following command to send a request using the Stock Quote Client to the ESB profile: 

ant stockquote -Dtrpurl=http://localhost:8280/services/message-router-proxy -Dsymbol=foo

For information on the Stock Quote Client and its operation modes, go to Stock Quote Client in the WSO2 EI Documentation.

The structure of the request is as follows:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd">
   <soapenv:Header>    
   </soapenv:Header> 
   <soapenv:Body>
        <ser:getQuote>         
         <ser:request>
             <ser:symbol>foo</ser:symbol>
         </ser:request>       
      </ser:getQuote>
   </soapenv:Body>
</soapenv:Envelope>

Analyzing the result

After you execute the above command through the client, observe that the request is transferred to foo inventory service. Notice the following processed server log in the Stock Quote Client console: 

Standard :: Stock price = $77.86763451813164

Change the -Dsymbol parameter to bar and run the folloiwng command:

ant stockquote -Dtrpurl=http://localhost:8280/services/message-router-proxy -Dsymbol=bar

Now, the request goes to bar inventory service. Notice the following processed server log in the Stock Quote Client console: 

Standard :: Stock price = $64.81823486260222

  • No labels