This documentation is for WSO2 Enterprise Service Bus version 4.8.1 . View documentation for the latest release.

All docs This doc
Skip to end of metadata
Go to start of metadata

Introduction

This sample demonstrates the functionality of a Switch-Case Mediator. A message is passed through the ESB in the Smart Client Mode. The ESB acts as a gateway to accept all messages, writes and reads local properties on a message instance and then performs mediation based on the message properties or content.

Prerequisites

For a list of prerequisites, see Prerequisites to Start the ESB Samples.

Building the sample

The XML configuration for this sample is as follows: 

<definitions xmlns="http://ws.apache.org/ns/synapse">
    <sequence name="main">
        <in>
            <switch source="//m0:getQuote/m0:request/m0:symbol" xmlns:m0="http://services.samples">
                <case regex="IBM">
                    <!-- the property mediator sets a local property on the *current* message -->
                    <property name="symbol" value="Great stock - IBM"/>
                </case>
                <case regex="MSFT">
                    <property name="symbol" value="Are you sure? - MSFT"/>
                </case>
                <default>
                    <!-- it is possible to assign the result of an XPath expression as well -->
                    <property name="symbol"
                              expression="fn:concat('Normal Stock - ', //m0:getQuote/m0:request/m0:symbol)"
                              xmlns:m0="http://services.samples"/>
                </default>
            </switch>
            <log level="custom">
                <!-- the get-property() XPath extension function allows the lookup of local message properties
                    as well as properties from the Axis2 or Transport contexts (i.e. transport headers) -->
                <property name="symbol" expression="get-property('symbol')"/>
                <!-- the get-property() function supports the implicit message headers To/From/Action/FaultTo/ReplyTo -->
                <property name="epr" expression="get-property('To')"/>
            </log>
            <!-- Send the messages where they are destined to (i.e. the 'To' EPR of the message) -->
            <send/>
        </in>
        <out>
            <send/>
        </out>
    </sequence>
</definitions>

This configuration file  synapse_sample_2.xml is available in the <ESB_HOME>/repository/samples directory.

To build the sample

  1. Start the ESB with the sample 2 configuration. For instructions on starting a sample ESB configuration, see Starting the ESB with a sample configuration.

    The operation log keeps running until the server starts, which usually takes several seconds. Wait until the server has fully booted up and displays a message similar to "WSO2 Carbon started in n seconds."

  2. Start the Axis2 server. For instructions on starting the Axis2 server, see Starting the Axis2 server.

  3. Deploy the back-end service SimpleStockQuoteService. For instructions on deploying sample back-end services, see Deploying sample back-end services.

    Now you have a running ESB instance and a back-end service deployed. In the next section, we will send a message to the back-end service through the ESB using a sample client.

Executing the sample

The sample client used here is the Stock Quote Client, which can operate in several modes. For further details on this sample client and its operation modes, see Stock Quote Client.

To execute the sample client

  • Run each of the following commands from the <ESB_HOME>/samples/axis2Client directory, specifying IBM, MSFT and SUN as the stock symbols.

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

Analyzing the output

Analyze the mediation log on the ESB start-up console.

When the symbol IBM is requested, you will see that the case statements first case which is IBM in the synapse_sample_2.xml file is executed and a local property named symbol is set to Great stock - IBM. Subsequently, this local property value is looked up by the Log Mediator  and logged using the get-property()XPath extension function. 

The mediation log on the ESB start-up console will be as follows:

INFO LogMediator - symbol = Great stock - IBM, epr = http://localhost:9000/axis2/services/SimpleStockQuoteService

When the symbol MSFT is requested, the mediation log on the ESB start-up console will be as follows:

INFO LogMediator - symbol = Are you sure? - MSFT, epr = http://localhost:9000/axis2/services/SimpleStockQuoteService

When the symbol SUN is requested, the mediation log on the ESB start-up console will be as follows:

INFO LogMediator - symbol = Normal Stock - SUN, epr = http://localhost:9000/axis2/services/SimpleStockQuoteService
  • No labels