WSO2 ESB - Message Mediation Samples

Running the Message Mediation samples with WSO2 Enterprise Service Bus (ESB)

Sample 0: Introduction to ESB

<definitions xmlns="http://ws.apache.org/ns/synapse">
    <!-- log all attributes of messages passing through -->
    <log level="full"/>

    <!-- Send the messageto implicit destination -->
    <send/>
</definitions>

Objective: Introduction to ESB. Shows how a message could be made to pass through ESB and logged before it is delivered to its ultimate receiver.

The Stock quote client can operate in the following modes for this example.

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

See sample # 1

Prerequisites:
Start the Synapse configuration numbered 0: e.g. wso2esb-samples -sn 0
Start the Axis2 server and deploy the SimpleStockQuoteService if not already deployed

Execute the Smart Client

By tracing the execution of ESB with the log output level set to DEBUG, you will see the client request arriving at ESB with a WS-Addressing 'To' set to EPR http://localhost:9000/services/SimpleStockQuoteService. The ESB engine logs the message at the "full" log level (i.e. all the message headers and the body) then sends the message to its implicit 'To' address which is http://localhost:9000/services/SimpleStockQuoteService. You will see a message in the Axis2 server console confirming that the message got routed to the sample server and the sample service hosted at the sample server generating a stock quote for the requested symbol.

Sat Nov 18 21:01:23 IST 2006 SimpleStockQuoteService :: Generating quote for : IBM

The response message generated by the service is again received by ESB, and flows through the same mediation rules, which logs the response message and then sends it back. This time to the client. On the client console you should see an output similar to the following based on the message received by the client.

Standard :: Stock price = $95.26454380258552

Execute the Proxy Client

You will see the exact same behaviour as per the previous example when you run this scenario. However this time the difference is at the client, as it sends the message to the WS-Addressing 'To' address http://localhost:9000/services/SimpleStockQuoteService, but the transport specifies ESB as the http proxy.

Sample 1: Simple content based routing (CBR) of messages

<definitions xmlns="http://ws.apache.org/ns/synapse">
    <!-- filtering of messages with XPath and regex matches -->
    <filter source="get-property('To')" regex=".*/StockQuote.*">
        <send>
            <endpoint>
                <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
            </endpoint>
        </send>
        <drop/>
    </filter>
    <send/>
</definitions> 

Objective: Introduction to simple content based routing. Shows how a message could be made to pass through ESB using the Dumb Client mode, where ESB acts as a gateway to accept all messages and then perform mediation and routing based on message properties or content.

Prerequisites:
Start the Synapse configuration numbered 1: i.e. wso2esb-samples -sn 1
Start the Axis2 server and deploy the SimpleStockQuoteService if not already deployed

Execute the Dumb Client as:

ant stockquote -Dtrpurl=http://localhost:8280/services/StockQuote

This time you will see ESB receiving a message for which ESB was set as the ultimate receiver of the message. Based on the 'To' EPR of http://localhost:8280/services/StockQuote, ESB performs a match to the path '/StockQuote' and as the request matches the XPath expression of the filter mediator, the filter mediator's child mediators execute. This sends the message to a different endpoint as specified by the endpoint definition. The 'drop' mediator terminates further processing of the current message in a configuration. During response processing, the filter condition fails, and thus the implicit 'send' mediator forwards the response back to the client.

Sample 2: CBR with the Switch-case mediator, using message properties

<definitions xmlns="http://ws.apache.org/ns/synapse">
    <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/>
</definitions>

Objective: Introduce switch-case mediator and writing and reading of local properties set on a message instance

Prerequisites:
Start the Synapse configuration numbered 2: i.e. wso2esb-samples -sn 2
Start the Axis2 server and deploy the SimpleStockQuoteService if not already done.

Execute the 'ant stockquote ..' request again in the smart client mode, specifying 'IBM', 'MSFT' and 'SUN' as the stock symbols. When the symbol IBM is requested, viewing the mediation logs you will see that the case statements' first case for 'IBM' is executed and a local property named 'symbol' was 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.

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

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

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

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

Sample 3: Local Registry entry definitions, reusable endpoints and sequences

<definitions xmlns="http://ws.apache.org/ns/synapse">
    <!-- define a string resource entry to the local registry -->
    <localEntry key="version">0.1</localEntry>
    <!-- define a reuseable endpoint definition -->
    <endpoint name="simple">
        <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
    </endpoint>

    <!-- define a reusable sequence -->
    <sequence name="stockquote">
        <!-- log the message using the custom log level. illustrates custom properties for log -->
        <log level="custom">
            <property name="Text" value="Sending quote request"/>
            <property name="version" expression="get-property('version')"/>
            <property name="direction" expression="get-property('direction')"/>
        </log>
        <!-- send message to real endpoint referenced by key "simple" endpoint definition -->
        <send>
            <endpoint key="simple"/>
        </send>
    </sequence>

    <sequence name="main">
        <in>
            <property name="direction" value="incoming"/>
            <sequence key="stockquote"/>
        </in>
        <out>
            <send/>
        </out>
    </sequence>
</definitions>

Objective: Illustrates local registry entry definitions, reusable endpoints and sequences

Prerequisites:
Start the Synapse configuration numbered 3: i.e. wso2esb-samples -sn 3
Start the Axis2 server and deploy the SimpleStockQuoteService if not already done

This example uses a sequence named as "main" that specifies the main mediation rules to be executed. This is equivalent to directly specifying the mediators of the main sequence within the <definitions> tags. This is the recommended and also a better approach for non-trivial configurations. Execute the 'ant stockquote ..' request again, and following through the mediation logs you will now notice that the sequence named "main" is executed. Then for the incoming message flow the <in> mediator executes, and it calls the sequence named "stockquote".

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/
DEBUG SequenceMediator - Sequence mediator <main> :: mediate()
DEBUG InMediator - In mediator mediate()
DEBUG SequenceMediator - Sequence mediator <stockquote> :: mediate()

As the "stockquote" sequence executes, the log mediator dumps a simple text/string property, result of an XPath evaluation, that picks up the key named "version", and a second result of an XPath evaluation that picks up a local message property set previously by the <property> mediator. The get-property() XPath extension function is able to read message properties local to the current message, local or remote registry entries, Axis2 message context properties as well as transport headers. The local entry definition for "version" defines a simple text/string registry entry for that which is visible to all messages that pass through ESB.

[HttpServerWorker-1] INFO  LogMediator - Text = Sending quote request, version = 0.1, direction = incoming
[HttpServerWorker-1] DEBUG SendMediator - Send mediator :: mediate()
[HttpServerWorker-1] DEBUG AddressEndpoint - Sending To: http://localhost:9000/services/SimpleStockQuoteService 

Sample 4: Introduction to error handling

<definitions xmlns="http://ws.apache.org/ns/synapse">

    <!-- the default fault handling sequence used by Synapse - named 'fault' -->
    <sequence name="fault">
        <log level="custom">
            <property name="text" value="An unexpected error occured"/>
            <property name="message" expression="get-property('ERROR_MESSAGE')"/>
        </log>
        <drop/>
    </sequence>

    <sequence name="sunErrorHandler">
        <log level="custom">
            <property name="text" value="An unexpected error occured for stock SUN"/>
            <property name="message" expression="get-property('ERROR_MESSAGE')"/>
        </log>
        <drop/>
    </sequence>

    <!-- default message handling sequence used by Synapse - named 'main' -->
    <sequence name="main">
        <in>
            <switch source="//m0:getQuote/m0:request/m0:symbol" xmlns:m0="http://services.samples">
                <case regex="IBM">
                    <send>
                        <endpoint><address uri="http://localhost:9000/services/SimpleStockQuoteService"/></endpoint>
                    </send>
                </case>
                <case regex="MSFT">
                    <send>
                        <endpoint key="bogus"/>
                    </send>
                </case>
                <case regex="SUN">
                    <sequence key="sunSequence"/>
                </case>
            </switch>
            <drop/>
        </in>

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

    <sequence name="sunSequence" onError="sunErrorHandler">
        <send>
            <endpoint key="sunPort"/>
        </send>
    </sequence>

</definitions> 

Objective: Introduction to error handling with the 'fault' sequence

Prerequisites:
Start the Synapse configuration numbered 4: i.e. wso2esb-samples -sn 4
Start the Axis2 server and deploy the SimpleStockQuoteService if not already done

When the IBM stock quote is requested, the configuration routes it to the defined inline endpoint, which routes the message to the SimpleStockQuoteService on the local Axis2 instance. Hence a valid response message is shown at the client.

If you lookup a stock quote for 'MSFT', ESB is instructed to route the message to the endpoint defined as the 'bogus' endpoint, which does not exist. ESB executes the specified error handler sequence closest to the point where the error was encountered. In this case, the currently executing sequence is 'main' and it does not specify an 'onError' attribute. Whenever ESB cannot find an error handler, it looks for a sequence named 'fault'. Thus the 'fault' sequence can be seen executing, and writing the generic error message to the logs.

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT
[HttpServerWorker-1] DEBUG SendMediator - Send mediator :: mediate()
[HttpServerWorker-1] ERROR IndirectEndpoint - Reference to non-existent endpoint for key : bogus
[HttpServerWorker-1] DEBUG MediatorFaultHandler - MediatorFaultHandler :: handleFault
[HttpServerWorker-1] DEBUG SequenceMediator - Sequence mediator <fault> :: mediate()
[HttpServerWorker-1] DEBUG LogMediator - Log mediator :: mediate()
[HttpServerWorker-1] INFO  LogMediator - text = An unexpected error occured, message = Reference to non-existent endpoint for key : bogus

When the 'SUN' quote is requested, a custom sequence 'sunSequence' is invoked, and it specifies 'sunErrorHandler' as its error handler. Hence when the send fails, you could see the proper error handler invocation and the custom error message printed as follows.

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN
[HttpServerWorker-1] DEBUG SequenceMediator - Sequence mediator <sunSequence> :: mediate()
[HttpServerWorker-1] DEBUG SequenceMediator - Setting the onError handler for the sequence
[HttpServerWorker-1] DEBUG AbstractListMediator - Implicit Sequence <SequenceMediator> :: mediate()
[HttpServerWorker-1] DEBUG SendMediator - Send mediator :: mediate()
[HttpServerWorker-1] ERROR IndirectEndpoint - Reference to non-existent endpoint for key : sunPort
[HttpServerWorker-1] DEBUG MediatorFaultHandler - MediatorFaultHandler :: handleFault
[HttpServerWorker-1] DEBUG SequenceMediator - Sequence mediator <sunErrorHandler> :: mediate()
[HttpServerWorker-1] DEBUG AbstractListMediator - Implicit Sequence <SequenceMediator> :: mediate()
[HttpServerWorker-1] DEBUG LogMediator - Log mediator :: mediate()
[HttpServerWorker-1] INFO  LogMediator - text = An unexpected error occured for stock SUN, message = Reference to non-existent endpoint for key : sunPort

Sample 5: Creating SOAP fault messages and changing the direction of a message

<definitions xmlns="http://ws.apache.org/ns/synapse">

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

        <send/>
    </sequence>

    <sequence name="main" onError="myFaultHandler">
        <in>
            <switch source="//m0:getQuote/m0:request/m0:symbol"
                    xmlns:m0="http://services.samples">
                <case regex="MSFT">
                    <send>
                        <endpoint><address uri="http://bogus:9000/services/NonExistentStockQuoteService"/></endpoint>
                    </send>
                </case>
                <case regex="SUN">
                    <send>
                        <endpoint><address uri="http://localhost:9009/services/NonExistentStockQuoteService"/></endpoint>
                    </send>
                </case>
            </switch>
            <drop/>
        </in>

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

</definitions>

Objective: Makefault mediator and sending back error responses

Prerequisites:
Start the Synapse configuration numbered 5: i.e. wso2esb-samples -sn 5
Start the Axis2 server and deploy the SimpleStockQuoteService if not already done

When the MSFT stock quote is requested, an unknown host exception would be generated. A connection refused exception would be generated for the SUN stock request. This error message is captured and returned to the original client as a SOAP fault in this example.

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

returns,

<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><faultcode>soapenv:Client</faultcode>
    <faultstring>Connection refused or failed for : bogus:9000 </faultstring><detail /></soapenv:Fault>

And

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

returns,

<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><faultcode>soapenv:Client</faultcode>
    <faultstring> Connection refused or failed for : localhost/127.0.0.1:9009</faultstring><detail /></soapenv:Fault>

Sample 6: Manipulating SOAP headers, and filtering incoming and outgoing messages

<definitions xmlns="http://ws.apache.org/ns/synapse">
    <in>
        <header name="To" value="http://localhost:9000/services/SimpleStockQuoteService"/>
    </in>
    <send/>
</definitions>

Objective: Introduction to header, in (out) mediators

Prerequisites:
Start the Synapse configuration numbered 6: i.e. wso2esb-samples -sn 6
Start the Axis2 server and deploy the SimpleStockQuoteService if not already done

In this example we use the stockquote client in the dumb client mode, setting the 'To' EPR of the message to ESB. Then the 'in' mediator processes the incoming messages, and manipulates the 'To' header to refer to the stock quote service on the sample Axis2 server. Thus it is now possible to request for a stock quote as follows.

ant stockquote -Dtrpurl=http://localhost:8280/

Sample 7: Introduction to local Registry entries and using Schema validation

<definitions xmlns="http://ws.apache.org/ns/synapse">

    <localEntry key="validate_schema">
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    xmlns="http://www.apache-synapse.org/test" elementFormDefault="qualified"
                    attributeFormDefault="unqualified"
                    targetNamespace="http://services.samples">
            <xs:element name="getQuote">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="request">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="stocksymbol" type="xs:string"/>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </localEntry>

    <in>
        <validate>
            <schema key="validate_schema"/>
            <on-fail>
                <!-- if the request does not validate againt schema throw a fault -->
                <makefault>
                    <code value="tns:Receiver"
                            xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/>
                    <reason value="Invalid custom quote request"/>
                </makefault>
                <property name="RESPONSE" value="true"/>
                <header name="To" expression="get-property('ReplyTo')"/>
            </on-fail>
        </validate>
    </in>
    <send/>
</definitions> 

Objective: Introduction to local (static) registry entries and the validate mediator

Prerequisites:
Start the Synapse configuration numbered 7: i.e. wso2esb-samples -sn 7
Start the Axis2 server and deploy the SimpleStockQuoteService if not already done

This example shows how a static XML fragment could be made available to the ESB local registry. Resources defined in the local registry are static (i.e. never changes over the lifetime of the configuration) and may be specified as a source URL, inline text or inline xml. In this example the schema is made available under the key 'validate_schema'.

The validate mediator by default operates on the first child element of the SOAP body. You may specify an XPath expression using the 'source' attribute to override this behaviour. The validate mediator now uses the 'validate_schema' resource to validate the incoming message, and if the message validatation fails it invokes the 'on-fail' sequence of mediators.

If you send a stockquote request using 'ant stockquote ...' you will get a fault back with the message 'Invalid custom quote request' as the schema validation failed. This is because the schema used in the example expects a slightly different message than what is created by the stock quote client. (i.e. expects a 'stocksymbol' element instead of 'symbol' to specify the stock symbol)

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

Sample 8: Introduction to static and dynamic registry resources, and using XSLT transformations

<definitions xmlns="http://ws.apache.org/ns/synapse">

    <!-- the SimpleURLRegistry allows access to a URL based registry (e.g. file:/// or http://) -->
    <registry provider="org.wso2.carbon.mediation.registry.ESBRegistry">
        <!-- the root property of the simple URL registry helps resolve a resource URL as root + key -->
        <parameter name="root">file:./repository/samples/resources/</parameter>
        <!-- all resources loaded from the URL registry would be cached for this number of milli seconds -->
        <parameter name="cachableDuration">15000</parameter>
    </registry>

    <!-- define the request processing XSLT resource as a static URL source -->
    <localEntry key="xslt-key-req" src="file:repository/samples/resources/transform/transform.xslt"/>

    <in>
        <!-- transform the custom quote request into a standard quote requst expected by the service -->
        <xslt key="xslt-key-req"/>
    </in>
    <out>
        <!-- transform the standard response back into the custom format the client expects -->
        <!-- the key is looked up in the remote registry and loaded as a 'dynamic' registry resource -->
        <xslt key="transform/transform_back.xslt"/>
    </out>
    <send/>
</definitions>

Objective: Introduction to static and dynamic registry resources and the XSLT mediator

Prerequisites:
Start the Synapse configuration numbered 8: i.e. wso2esb-samples -sn 8
Start the Axis2 server and deploy the SimpleStockQuoteService if not already done

This example uses the XSLT mediator to perform transformations, and the xslt transformations are specified as registry resources. The first resource 'xslt-key-req' is specified as a 'local' registry entry. Local entries do not place the resource on the registry, but simply make it available to the local configuration. If a local entry is defined with a key that already exists in the remote registry, the local entry will get higher preference and override the remote resource.

In this example you will notice the new 'registry' definition. ESB comes with a simple URL based registry implementation SimpleURLRegistry. During initialization of the registry, the SimpleURLRegistry expects to find a property named 'root', which specifies a prefix for the registry keys used later. When the SimpleURLRegistry is used, this root is prefixed to the entry keys to form the complete URL for the resource being looked up. The registry caches a resource once requested, and caches it internally for a specified duration. Once this period expires, it will reload the meta information about the resource and reload its cached copy if necessary, the next time the resource is requested.

Hence the second XSLT resource key 'transform/transform_back.xslt' concatenated with the 'root' of the SimpleURLRegistry 'file:repository/samples/resources/' forms the complete URL of the resource as 'file:repository/samples/resources/transform/transform_back.xslt' and caches its value for a period of 15000 ms.

Execute the custom quote client as 'ant stockquote -Dmode=customquote ...' and analyze the the ESB debug log output

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote

The incoming message is now transformed into a standard stock quote request as expected by the SimpleStockQuoteService deployed on the local Axis2 instance, by the XSLT mediator. The XSLT mediator uses Xalan-J to perform the transformations. It is possible to configure the underlying transformation engine using properties where necessary. The response from the SimpleStockQuoteService is converted back into the custom format as expected by the client during the out message processing.

During the response processing the SimpleURLRegistry fetches the resource.

If you run the client again immediately (i.e within 15 seconds of the first request) you will not see the resource being reloaded by the registry as the cached value would be still valid.

However if you leave the system idle for 15 seconds or more and then retry the same request, you will now notice that the registry noticed the cached resource has expired and will check the meta information about the resource to check if the resource itself has changed and will require a fresh fetch from the source URL. If the meta data / version number indicates that a reload of the cached resource is not necessary (i.e. unless the resource itself actually changed) the updated meta information is used and the cache lease extended as appropriate.

[HttpClientWorker-1] DEBUG AbstractRegistry - Cached object has expired for key : transform/transform_back.xslt
[HttpClientWorker-1] DEBUG SimpleURLRegistry - Perform RegistryEntry lookup for key : transform/transform_back.xslt
[HttpClientWorker-1] DEBUG AbstractRegistry - Expired version number is same as current version in registry
[HttpClientWorker-1] DEBUG AbstractRegistry - Renew cache lease for another 15s 

Now edit the repository/samples/resources/transform/transform_back.xslt file and add a blank line at the end. Now when you run the client again, and if the cache is expired, the resource would be re-fetched from its URL by the registry and this can be seen by the following debug log messages

[HttpClientWorker-1] DEBUG AbstractRegistry - Cached object has expired for key : transform/transform_back.xslt
[HttpClientWorker-1] DEBUG SimpleURLRegistry - Perform RegistryEntry lookup for key : transform/transform_back.xslt

Thus the SimpleURLRegistry allows resource to be cached, and updates detected so that the changes could be reloaded without restarting the ESB instance.

Sample 9: Introduction to dynamic sequences with the Registry

<definitions xmlns="http://ws.apache.org/ns/synapse">
    <registry provider="org.wso2.carbon.mediation.registry.ESBRegistry">
        <parameter name="root">file:./repository/samples/resources/</parameter>
        <parameter name="cachableDuration">15000</parameter>
    </registry>

    <sequence key="sequence/dynamic_seq_1.xml"/>
</definitions> 

Objective: Introduction to dynamic sequences with a Registry

Prerequisites:
Start the Synapse configuration numbered 9: i.e. wso2esb-samples -sn 9
Start the Axis2 server and deploy the SimpleStockQuoteService if not already done

This example introduces the dynamic behaviour of ESB through the use of a Registry. ESB supports dynamic definitions for sequences and endpoints, and as seen before, for resources. In this example we define a Synapse configuration which references a sequence definition specified as a registry key. The registry key resolves to the actual content of the sequence which would be loaded dynamically by ESB at runtime, and cached appropriately as per its definition in the registry. Once the cache expires, ESB would recheck the meta information for the definition and re-load the sequence definition if necessary and re-cache it again.

Once ESB is started, execute the stock quote client as 'ant stockquote..'. You will notice that that ESB fetches the definition of the sequence from the registry and executes its rules as follows:

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/
[HttpServerWorker-1] DEBUG SequenceMediator - Sequence mediator <dynamic_sequence> :: mediate()
...
[HttpServerWorker-1] INFO  LogMediator - message = *** Test Message 1 ***

Now if you execute the client immediately (i.e. within 15 seconds of the last execution) you will notice that the sequence was not reloaded. If you edit the sequence definition in repository/samples/resources/sequence/dynamic_seq_1.xml (i.e. edit the log message to read as "*** Test Message 2 ***") and execute the client again, you will notice that the new message is not yet visible (i.e. if you execute this within 15 seconds of loading the resource for the first time) However, after 15 seconds elapsed since the original caching of the sequence, you will notice that the new sequence is loaded and executed by ESB from the following log messages.

[HttpServerWorker-1] DEBUG SequenceMediator - Sequence mediator <dynamic_sequence> :: mediate()
...
[HttpServerWorker-1] INFO  LogMediator - message = *** Test Message 2 ***

The cache timeout could be tuned appropriately by configuring the URL registry to suite the environment and the needs.

Sample 10: Introduction to dynamic endpoints with the Registry

<definitions xmlns="http://ws.apache.org/ns/synapse">

    <registry provider="org.wso2.carbon.mediation.registry.ESBRegistry">
        <parameter name="root">file:repository/samples/resources/</parameter>
        <parameter name="cachableDuration">15000</parameter>
    </registry>

    <in>
        <send>
            <endpoint key="endpoint/dynamic_endpt_1.xml"/>
        </send>
    </in>
    <out>
        <send/>
    </out>
</definitions> 

Objective: Introduction to dynamic endpoints with the Registry

Prerequisites:
Start the Synapse configuration numbered 10: i.e. wso2esb-samples -sn 10
Start the Axis2 server and deploy the SimpleStockQuoteService if not already done
Start a second Axis2 server on http port 9001 and https port 9003 as follows:

./axis2server.sh -http 9001 -https 9003

This example introduces dynamic endpoints, where the definition of an endpoint is stored in the Registry. To follow this example execute the stock quote client as 'ant stockquote..' and see that the message is routed to the SimpleStockQuoteService on the default Axis2 instance on http port 9000. Repeat the above example immediately again, and notice that the endpoint is cached and reused by ESB - similarly to example # 8.

ant stockquote -Dtrpurl=http://localhost:8280/

Now edit the repository/samples/resources/endpoint/dynamic_endpt_1.xml definition and update the address to "http://localhost:9001/services/SimpleStockQuoteService". After the cached expires, the Registry loads the new definition of the endpoint, and then the messages can be seen being routed to the second sample Axis2 server on http port 9001.

Sample 11: A full registry based configuration, and sharing a configuration between multiple instances

<definitions xmlns="http://ws.apache.org/ns/synapse">
    <registry provider="org.wso2.carbon.mediation.registry.ESBRegistry">
        <parameter name="root">file:./repository/samples/resources/</parameter>
        <parameter name="cachableDuration">15000</parameter>
    </registry>
</definitions> 

Objective: A full registry based configuration

Prerequisites:
Start the Synapse configuration numbered 11: i.e. wso2esb-samples -sn 11
Start the Axis2 server and deploy the SimpleStockQuoteService if not already done

This example shows a full registry based ESB configuration. Thus it is possible to start a remote configuration from multiple instances of ESB in a clustered environment easily. The Synapse configuration held on a node hosting ESB simply points to the registry and looks up the actual configuration by requesting the key 'synapse.xml'.

(Note: Full registry based configuration is not dynamic atleast for the moment. i.e. it is not reloading itself)

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/
[HttpServerWorker-1] INFO LogMediator - message = This is a dynamic Synapse configuration

The actual synapse.xml loaded is:

<!-- a registry based Synapse configuration -->
<definitions xmlns="http://ws.apache.org/ns/synapse">
    <log level="custom">
        <property name="message" value="This is a dynamic Synapse configuration $$$"/>
    </log>
    <send/>
</definitions>

Sample 12: One way messaging / fireAndForget through ESB

Objective: Demonstrate one way messaging / fireAndForget through ESB

Prerequisites:
Start the Axis2 server and deploy the SimpleStockQuoteService (Refer steps above)
Start the Synapse configuration numbered 0: i.e. wso2esb-samples -sn 0

This example invokes the one-way 'placeOrder' operation on the SimpleStockQuoteService using the custom client which uses the Axis2 ServiceClient.fireAndForget() API. To test this, use 'ant -Dmode=placeorder...' and you will notice the one way message flowing through ESB into the sample Axis2 server instance, which reports the acceptance of the order as follows:

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=placeorder
SimpleStockQuoteService :: Accepted order for : 7482 stocks of IBM at $ 169.27205579038733

If you send your client request through TCPmon, you will notice that the SimpleStockQuoteService replies to ESB with a HTTP 202 reply, and that ESB in-turn replies to the client with a HTTP 202 acknowledgement

Sample 13: Dual channel invocation through synapse

Objective: Demonstrate dual channel messaging through synapse

Prerequisites:
Start the Axis2 server and deploy the SimpleStockQuoteService (Refer steps above)
Start the Synapse configuration numbered 0: i.e. wso2esb-samples.sh -sn 0

This example invokes the same 'getQuote' operation on the SimpleStockQuoteService using the custom client which uses the Axis2 ServiceClient API with useSeparateListener set to true so that the response is coming through a different channel than the one which is used to send the request to a callback defined in the client. To test this, use 'ant -Dmode=dualquote...' and you will notice the dual channel invocation through Synapse into the sample Axis2 server instance, which reports the response back to the client over a different channel:

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=dualquote
Response received to the callback
    Standard dual channel :: Stock price = $57.16686934968289

If you send your client request through TCPmon, you will notice that Synapse replies to the client with a HTTP 202 acknowledgment when you send the request and the communication between synapse and the server happens on a single channel and then you get the response back from synapse to the clients callback in a different channel (which cannot be observed through TCPmon)

Also you could see the wsa:Reply-To header being something like http://localhost:8200/axis2/services/anonService2 which implies that the reply is being on a different channel listening on the port 8200. Please note that it is required to engage addressing when using the dual channel invocation because it requires the wsa:Reply-To header.

Sample 14: Sequences and Endpoints as local registry items

<definitions xmlns="http://ws.apache.org/ns/synapse"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://ws.apache.org/ns/synapse http://synapse.apache.org/ns/2010/04/configuration/synapse_config.xsd">

    <localEntry key="local-enrty-ep-key"
                src="file:repository/conf/sample/resources/endpoint/dynamic_endpt_1.xml"/>

    <localEntry key="local-enrty-sequence-key">
        <sequence name="dynamic_sequence">
            <log level="custom">
                <property name="message" value="*** Test Message 1 ***"/>
            </log>
        </sequence>
    </localEntry>

    <sequence name="main">
        <in>
            <sequence key="local-enrty-sequence-key"/>
            <send>
                <endpoint key="local-enrty-ep-key"/>
            </send>
        </in>
        <out>
            <send/>
        </out>
    </sequence>
</definitions>

Objective: Sequence and Endpoints as local registry entries

Prerequisites:
Start the Axis2 server and deploy the SimpleStockQuoteService (Refer steps above)
Start the Synapse configuration numbered 14: i.e. wso2esb-samples.sh -sn 14

This example shows sequences and endpoints fetched from local registry. Thus it is possible to have endpoints sequences as local registry entries including file entries. Execute the following command to see the sample working, where you will be able to see the log statement from the fetched sequence from the local entry and the endpoint will be fetched from the specified file at runtime and be cached in the system

ant stockquote -Dtrpurl=http://localhost:8280/

Sample 15: Message Enrichment through Synapse

<definitions xmlns="http://synapse.apache.org/ns/2010/04/configuration">
    <sequence name="main">
        <in>
            <enrich>
                <source type="custom"
                        xpath="//m0:getQuote/m0:request/m0:symbol/text()"
                        xmlns:m0="http://services.samples"/>
                <target type="property" property="ORIGINAL_REQ"/>
            </enrich>
            <enrich>
                <source type="body"/>
                <target type="property" property="REQUEST_PAYLOAD"/>
            </enrich>

            <enrich>
                <source type="inline" key="init_req"/>
                <target xmlns:m0="http://services.samples"
                        xpath="//m0:getQuote/m0:request/m0:symbol/text()"/>
            </enrich>

            <send>
                <endpoint>
                    <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                </endpoint>
            </send>
            <drop/>
        </in>
        <out>
            <header xmlns:urn="http://synapse.apache.org" name="urn:lastTradeTimestamp" value="foo"/>
            <enrich>
                <source type="custom"
                        xpath="//ns:getQuoteResponse/ns:return/ax21:lastTradeTimestamp"
                        xmlns:ns="http://services.samples"
                        xmlns:ax21="http://services.samples/xsd"/>
                <target xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                        xmlns:urn="http://synapse.apache.org"
                        xpath="/soapenv:Envelope/soapenv:Header/urn:lastTradeTimestamp"/>
            </enrich>
             <log level="full"/>
            <log>
                <property name="Original Request Symbol" expression="get-property('ORIGINAL_REQ')"/>
                <property name="Request Payload" expression="get-property('REQUEST_PAYLOAD')"/>
            </log>
            <send/>
        </out>

    </sequence>
    <localEntry key="init_req">MSFT</localEntry>
    <localEntry key="price_req">
        <m0:symbol xmlns:m0="http://services.samples">MSFT</m0:symbol>
    </localEntry>

</definitions>

Objective: Introduction to Message Copying and Content Enriching with Enrich Mediator

Prerequisites:
Start the Synapse configuration numbered 15: i.e. synapse -sample 15
Start the Axis2 server and deploy the SimpleStockQuoteService if not already done

./axis2server.sh

This sample demonstrate the various capabilities of Enrich Mediator. Inside the in-sequence we store/copy different parts of the message to properties and just before sending the message to the StockQuoteService, we modify the request value based on the local entry value-init_req. Then in the out-sequence, the enrich mediator is used to enrich a soap header based on the 'lastTradeTimestamp' value of the response.

ant stockquote -Dtrpurl=http://localhost:8280/services/StockQuote

You can try with different local entries as the source with the correct target xpath values.

Sample 16: Introduction to dynamic and static registry keys

<definitions xmlns="http://ws.apache.org/ns/synapse">

    <!-- the SimpleURLRegistry allows access to a URL based registry (e.g. file:/// or http://) -->
    <registry provider="org.wso2.carbon.mediation.registry.ESBRegistry">
        <!-- the root property of the simple URL registry helps resolve a resource URL as root + key -->
        <parameter name="root">file:repository/samples/resources/</parameter>
        <!-- all resources loaded from the URL registry would be cached for this number of milli seconds -->
        <parameter name="cachableDuration">15000</parameter>
    </registry>

    <sequence name="main">
        <in>
            <!-- define the request processing XSLT resource as a property value -->
            <property name="symbol" value="transform/transform.xslt"/>
            <!-- {} denotes that this key is a dynamic one and it is not a static key -->
            <!-- use Xpath expression "get-property()" to evaluate real key from property -->
            <xslt key="{get-property('symbol')}"/>
        </in>
        <out>
            <!-- transform the standard response back into the custom format the client expects -->
            <!-- the key is looked up in the remote registry using a static key -->
            <xslt key="transform/transform_back.xslt"/>
        </out>
        <send/>
    </sequence>

</definitions>

Objective: Introduction to dynamic and static keys

Prerequisites:
Start the Synapse configuration numbered 16: i.e. synapse -sample 16
Start the Axis2 server and deploy the SimpleStockQuoteService if not already done

./axis2server.sh

This Sample demonstrates the use of dynamic keys with mediators. XSLT mediator is used as an example for that and deference between static and dynamic usage of keys are shown with that.

The first registry resource "transform/transform.xslt" is set as a property value. Inside the XSLT mediator the local property value is lookup using the Xpath expression "get-property()". Likewise any Xpath expression can be enclosed inside "{ }" to denote that it is a dynamic key. Then the mediator will evaluate the real value for that expression.

The second XSLT resource "transform/transform_back.xslt" is used simply as a static key as usual. It is not included inside "{ }" and because of the mediator directly use the static value as the key.

Execute the custom quote client as 'ant stockquote -Dmode=customquote' and analys the output which is similar to the Sample 8.

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote

You can try with different local entries as the source with the correct target xpath values.

Sample 17: Transforming / replacing message content with Payload-factory mediator

<definitions xmlns="http://ws.apache.org/ns/synapse">

    <sequence name="main">
        <in>
            <!-- using payloadFactory mediator to transform the request message -->
            <payloadFactory>
                <format>
                    <m:getQuote xmlns:m="http://services.samples">
                        <m:request>
                            <m:symbol>$1</m:symbol>
                        </m:request>
                    </m:getQuote>
                </format>
                <args>
                    <arg xmlns:m0="http://services.samples" expression="//m0:Code"/>
                </args>
            </payloadFactory>
        </in>
        <out>
            <!-- using payloadFactory mediator to transform the response message -->
            <payloadFactory>
                <format>
                    <m:CheckPriceResponse xmlns:m="http://services.samples/xsd">
                        <m:Code>$1</m:Code>
                        <m:Price>$2</m:Price>
                    </m:CheckPriceResponse>
                </format>
                <args>
                    <arg xmlns:m0="http://services.samples/xsd" expression="//m0:symbol"/>
                    <arg xmlns:m0="http://services.samples/xsd" expression="//m0:last"/>
                </args>
            </payloadFactory>
        </out>
        <send/>
    </sequence>

</definitions>

Objective: Introduction to Payload-factory mediator

Prerequisites:
Start the Synapse configuration numbered 17: i.e. wso2esb-samples -sn 17
Start the Axis2 server and deploy the SimpleStockQuoteService if not already done

This sample uses Payload-factory mediator to perform transformations as an alternative to XSLT mediator (Sample 8). In this sample, ESB implements Message Translator EIP and acts an a translator between the client and the backend server.

Execute the custom quote client as 'ant stockquote -Dmode=customquote ...' and analyze the the ESB debug log output

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote

Incoming message is transformed by the Payload-factory mediator into a standard stock quote request as expected by the SimpleStockQuoteService deployed on the Axis2 server. printf() style formatting is used to configure the transformation performed by the mediator. Each argument in the mediator configuration could be a static value or an XPath expression. When an expression is used, argument value is fetched at runtime by evaluating the provided XPath expression against the existing SOAP message.

The response from the SimpleStockQuoteService is converted back into the custom format as expected by the client during the out message processing, once again using Payload-factory mediator.