How to Store & Retrieve Messages in WSO2?

How to Store & Retrieve Messages in WSO2?

The cache mediator plays a crucial role in the Enterprise Service Bus (ESB) by optimizing performance and reducing backend load. It uses hashing to quickly determine if an incoming request is already stored in the cache. When a request arrives, the ESB generates a hash value and checks if it exists in the cache. If so, the relevant response is immediately sent back to the client. Otherwise, the message is forwarded to the backend system, and the result is stored in the cache before being returned to the client.

The cache mediator uses hashing to determine if the request is already in the cache. When a request comes into the ESB, it generates a hash value for the request.

If the hash value exists, ESB sends the relevant response back to the client. Otherwise, it sends the message to the backend. Just before sending the result back to the client, ESB stores it in the cache.

Walkthrough:

Create new project in integration studio >> File >> New >> Integration Project and give the Project name.

After creating project Right Click on API >> New >> RestAPI >> Create new arifact given required filelds as Name and context >> Finish

Drag and Drop the Cache Mediator

Configure the Cache Properties:

Cache Type: FINDER

Cache Timeout(S): 10 the cache Timeout (cache Timeout is the amount of time cache is stored and response is sent back to client without further going in to next mediator)

Max Message Size(bytes): 2000

Drag and Drop the Payload Factory

Configure the Payload Factory Properties:

Drag and Drop the Respond Mediator

After cache mediator, Drag and Drop the Log Mediator

Drag and Drop the Send Mediator

In Send Mediator, Drag and Drop the Default HTTP Endpoint

Build and run the artifacts:

  • Right-click Cache-MediatorCompositeExporter and click Export Project Artifacts and Run
  • You will see the following dialog box. Select the Cache-MediatorConfigs folder in the artifact list and click Finish.

Get the response as shown in the figure below:

{

  “args”: {},

  “headers”: {

    “Accept”: “text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7”,

    “Accept-Encoding”: “gzip, deflate, br, zstd”,

    “Accept-Language”: “en-US,en;q=0.9”,

    “Host”: “httpbin.org”,

“Sec-Ch-Ua”: “\”Chromium\”;v=\”122\”, \”Not(A:Brand\”;v=\”24\”, \”Google Chrome\”;v=\”122\””,

    “Sec-Ch-Ua-Mobile”: “?0”,

    “Sec-Ch-Ua-Platform”: “\”Windows\””,

    “Sec-Fetch-Dest”: “document”,

    “Sec-Fetch-Mode”: “navigate”,

    “Sec-Fetch-Site”: “none”,

    “Sec-Fetch-User”: “?1”,

    “Upgrade-Insecure-Requests”: “1”,

    “User-Agent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36”,

    “X-Amzn-Trace-Id”: “Root=1-65e00c8f-2d53d6a310d7047c4de47297”

  },

  “origin”: “14.194.44.162”,

  “url”: https://httpbin.org/get  }

From above response X-Amzn-Trace-Id will change for every request. By using the cache mediator, we will catch the response for certain period of time (cache Timeout field in cache mediator).

In the outsequence

Drag and Drop the Cache Mediator

Configure the Cache Properties:

Cache Mediator Implementation: Default

Cache Type: COLLLECTOR

Drag and Drop the Payload Factory

Configure the Payload Factory Properties:

Drag and Drop the Send Mediator to return the response to client.

Execute the integration studio with a GET request to generate a URL within the integration studio for accessing the API via Chrome or any web browser.

It is noticeable that the response is same till the cache timeout value is exceeded. Once the cache timeout value (Time in sec) is exceeded new value for X-Amzn-Trace-Id is generated.

The key steps in this process are to use the Send Mediator to return the response to the client, execute the integration studio with a GET request to generate a URL for accessing the API, and be aware that the response will remain the same until the cache timeout value is exceeded.

At that point, a new X-Amzn-Trace-Id value will be generated, indicating that the cached response is no longer valid. By understanding these core concepts, developers can effectively leverage the integration studio to interact with APIs and manage caching behavior to ensure their applications function as expected. For more information, please write to [email protected].

Connect With Us

Connect with us
Scroll to Top