Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

WSO2 ESB Cache Mediator Tutorial

DZone's Guide to

WSO2 ESB Cache Mediator Tutorial

· Integration Zone
Free Resource

Discover how Microservices are a type of software architecture where large applications are made up of small, self-contained units working together through APIs that are not dependent on a specific language. Brought to you in partnership with AppDynamics.

This tutorial is about Caching Responses Over Requests. It means when WSO2 ESB gets the same request then Cache Mediator will send out Caching Responses. In this example I am using my last service that I created from WSO2 DSS.

1. Start DSS (With my last Services/ or any service you wish to used – remote Services) and WSO2 ESB.

2. Create “Pass Through Proxy”

image

  • Cache Id: Same id for a cache mediator instance in incoming path and the corresponding mediator instance in outgoing message path.
  • Cache Scope: This is important if the service is deployed in a cluster.
    Per-Host: current host in a cluster.
    Per-Mediator: Twhole cluster.
  • Cache Type: Whether the mediator is in the incoming path (check request) or the outgoing path (cache the response).
  • Finder: Set if the message is incoming path. This indicate the mediator find for the request hash of each incoming message.
  • Collector: Set if the message is in outgoing path. This indicate the mediator collect the response message in the cache.
  • Hash Generator: The logic for finding each incoming message.
  • Cache Timeout: expiring time (seconds).
  • Maximum Message Size: The limit of the message to cache in bytes.
  • Implementation Type: Currently only "In-Memory" is available.
  • On Cache Hit: Specify the sequence to follow when the cache mediator is hit. (named sequence of mediators from the registry.)

3. Now add Cache mediator to the proxy.

In Sequence

image

Out Sequence

image

4. Try it out

image

5. Now testing is it coming from cache or not proxy level login

6. Here is my new proxy with logs

<proxy xmlns="http://ws.apache.org/ns/synapse" name="TestingCacheProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <inSequence>
         <log>
            <property name="testing" value="Request arrives in to InSequence"/>
         </log>
         <cache scope="per-host" collector="false" hashGenerator="org.wso2.caching.digest.DOMHASHGenerator" timeout="20" maxMessageSize="500">
            <implementation type="memory" maxSize="1000"/>
         </cache>
         <log>
            <property name="testing" value="Hi, I am after Cache"/>
         </log>
         <send>
            <endpoint>
               <address uri="http://localhost:9764/services/PostgresDataService?wsdl"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <log>
            <property name="testing" value="Hi, I in the top of Out Sequence"/>
         </log>
         <cache scope="per-host" collector="true"/>
         <log>
            <property name="testing" value="I am in Out Sequence and below the Cache"/>
         </log>
         <send/>
      </outSequence>
   </target>
   <publishWSDL uri="http://localhost:9764/services/PostgresDataService?wsdl"/>
   <description></description>
</proxy>

Now call some, and see 

image

image

Read line mapping Show the 1st call,

Blue Line shows next call that was respond from cache .

Cache will speed up you systems/proxies. So it is time to use it.




Discover the six challenges and best practices in managing microservice performance, brought to you in partnership with AppDynamics.

Topics:

Published at DZone with permission of Madhuka Udantha, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}