This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Sample 420: Simple Cache Implemented on ESB for the Actual Service

Note that WSO2 EI is shipped with the following changes to what is mentioned in this documentation:

  • <PRODUCT_HOME>/repository/samples/ directory that includes all Integration profile samples is changed to <EI_HOME>/samples/service-bus/.
  • <PRODUCT_HOME>/repository/samples/resources/ directory that includes all artifacts related to the Integration profile samples is changed to <EI_HOME>/samples/service-bus/resources/.

Objective: Demonstrate the use of Cache mediator in order to cache the response and use that cached response as the response for an identical xml request.

<definitions xmlns="http://ws.apache.org/ns/synapse">
    <in>
        <cache timeout="20" scope="per-host" collector="false"
               hashGenerator="org.wso2.carbon.mediator.cache.digest.DOMHASHGenerator">
            <implementation type="memory" maxSize="100"/>
        </cache>
        <send>
            <endpoint>
                <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
            </endpoint>
        </send>
    </in>
    <out>
        <cache collector="true"/>
        <send/>
    </out>
</definitions>

 

Prerequisites:

  • Deploy the SimpleStockQuoteService in sample Axis2 server and start it on port 9000.
  • Start ESB with the sample configuration 420 (i.e. wso2esb-samples -sn 420).

In this sample, the message sent to ESB is checked for an existing cached response by calculating the hash value of the request. If there is a cache hit in ESB then this request will not be forwarded to the actual service, rather ESB respond to the client with the cached response. In case of a cache miss that particular message will be forwarded to the actual service and cached that response in the out path for the use of consecutive requests of the same type.

To observe this behavior, invoke the client as follows.

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

You could notice that if you send more than one requests within 20 seconds only the first request is forwarded to the actual service, and the rest of the requests will be served by the cache inside ESB. You could observe this by looking at the printed line of the axis2 server, as well as by observing a constant rate as the response to the client instead of the random rate, which changes by each and every 20 seconds.

Synchronize web service invocation with Callout mediator

The Callout mediator calls the given service URL with the request message which is given by the source attribute, waits for the response and attaches the received response to the destination which is given by the target attribute. Both the source and the target can be a key or an XPath. In the case of the source, this key refers to either a message context property or to a local entry. For the target, this key refers to a message context property only.