Redis Connector Example

The Redis connector allows you to access the Redis commands from an integration sequence.

What you'll build

Given below is a sample scenario that demonstrates how to work with the ESB Redis Connector and access the Redis server, using Redis commands.

The user sends the request to invoke an API to get stock details. This REST call will get converted into a SOAP message and is sent to the back-end service. While the response from the backend service is converted back to JSON and sent back to the API caller, the integration runtime will extract stock volume details from the response and store it into a configured Redis server. When users need to retrieve stock volumes collected, they can invoke the getstockvolumedetails resource. This example also demonstrates how users can manipulate this stock volume details by removing unwanted items from the Redis server.

Note: In this scenario you need to set up the Redis Server in your local machine. Please refer the Setting up the Redis Connector documentation. Follow the steps listed under Setting up the Redis server section to setup the Redis server and Set up the back-end service section to setup the Stockquote service. This example demonstrates how to use Redis connector to:

  1. Retrieve stock volume details from the Stockquote back-end service. This is done while extracting the stock volume, creating a Redis hash map, and adding stock volume details to the Redis server. (In this example, Redis hashes are used to store different companies' stocks volume details. Since the “symbol” that is sent in the request is “WSO2”, the request is routed to the ESB endpoint. Once the response from the ESB endpoint is received, it is transformed according to the specified template and sent to the client. Then create a hash map and insert extracted details to the Redis hashmap).
  2. Retrieve all stock volume details from the Redis server.
  3. Remove stock volume details from Redis server.

All three operations are exposed via an StockQuoteAPI API. The API with the context /stockquote has four resources

  • /getstockquote/{symbol}: This is used to get stock volume details while extracting and sending details to the Redis hash map.
  • /getstockvolumedetails : Retrieve information about the inserted stock volume details.
  • /deletestockvolumedetails : Remove unwanted stock volume details.

The following diagram shows the overall solution. The user creates a hash map, stores ESB stock volume details into the list, and then receives it back and removes unwanted hash map items. To invoke each operation, the user uses the same API.

Redis connector example

If you do not want to configure this yourself, you can simply get the project and run it.

Configure the connector in ESB Integration Studio

Connectors can be added to integration flows in ESB Integration Studio. Once added, the operations of the connector can be dragged onto your canvas and added to your resources.

Import the connector

Follow these steps to set up the Integration Project and the Connector Exporter Project.

  1. Open ESB Integration Studio and create an Integration Project. Creating a new Integration Project

  2. Right-click the project that you created and click on Add or Remove Connector -> Add Connector. You will get directed to the Connector Store.

  3. Search for the specific connector required for your integration scenario and download it to the workspace. Search Connector in the Connector Store

  4. Click Finish, and your Integration Project is ready. The downloaded connector is displayed on the side palette with its operations.

  5. You can drag and drop the operations to the design canvas and build your integration logic. Drag connector operations

  6. Right click on the created Integration Project and select New -> Rest API to create the REST API.

Add integration logic

First create an API, which will be where we configure the integration logic. Right click on the created Integration Project and select, New -> Rest API to create the REST API. Specify the API name as SampleRdisAPI and API context as /resources.

Adding a Rest API

Configuring the API

Configure a resource for the getstockquote operation

Create a resource that sets up Redis hash map and sets a specific field in a hash to a specified value. In this sample, the user sends the request to invoke the created API to get ESB stock volume details. To achieve this, add the following components to the configuration.

  1. Add an address endpoint using the send mediator to access SimpleStockQuoteService.

Address endpoint

  1. Add a header to get a quote from the SimpleStockQuoteService.

Add Header to get Quote

  1. Add a payload factory to extract the selected stock details. In this sample, we attempt to get ESB stock details from the SimpleStockQuoteService.

Add payloadfactory to extract ESB details

  1. In this example, we copy the original payload to a property using the Enrich mediator.

Add enrich mediator

When we need the original payload, we replace the message body with this property value using the Enrich mediator as follows.

Add enrich mediator

  1. Initialize the connector.

    1. Navigate into the Palette pane and select the graphical operations icons listed under Redis Connector section. Then drag and drop the init operation into the Design pane.

      Drag and drop init operation

    2. Add the property values into the init operation as shown below. Replace the redisHost, redisPort, redisTimeout with your values.

      • redisHost: The Redis host name (default localhost).
      • redisPort: The port on which the Redis server is running (the default port is 6379).
      • redisTimeout : The server TTL (Time to live) in milliseconds.

      Add values to the init operation

  2. Set up the hSet operation. This operation sets a specific field in a hash to a specified value.

    1. Navigate into the Palette pane and select the graphical operations icons listed under Redis Connector section. Then drag and drop the hSet operation into the Design pane.

      Drag and drop hSet operation

    2. In this operation we are going to set a hash map to the Redis server. The hSet operation sets a specific field in a hash to a specified value.

      • redisKey : The name of the key where the hash is stored.
      • redisField : The field for which you want to set a value.
      • redisValue : The value that you want to set for the field.

      In this example, redisKey value is configured as StockVolume. While invoking the API, the above redisField,redisValue parameter values are extracted from the response of the SimpleStockQuoteService. Then they are populated as an input value for the Redis hSet operation.

      hSet parameters

  3. To get the input values in to the hSet, we can use the property mediator. Navigate into the Palette pane and select the graphical mediators icons listed under Mediators section. Then drag and drop the Property mediators onto the Design pane as shown below.
    > Note: The properties should be added to the pallet before creating the operation.

    The parameters available for configuring the Property mediator are as follows:
    1. Add the property mediator to capture the symbol value from the response of SimpleStockQuoteService. The 'symbol' contains the company name of the stock quote.

      • name : symbol
      • value expression : $body/soapenv:Envelope/soapenv:Body/ns:getQuoteResponse/ax21:symbol

      Add property mediators to get symbol

    2. Add the property mediator to capture the volume values. The 'volume' contains stock quote volume of the selected company.

      • name : volume
      • value expression : $body/soapenv:Envelope/soapenv:Body/ns:getQuoteResponse/ax21:volume

      Add property mediators to get volume

  4. Forward the backend response to the API caller.

    When you are invoking the created resource, the request of the message is going through the /getstockquote resource. Finally, it is passed to the Respond mediator. The Respond Mediator stops the processing on the current message and sends the message back to the client as a response.

    1. Drag and drop respond mediator to the Design view.

      Add Respond mediator

    2. Once you have setup the resource, you can see the getstockquote resource as shown below.

      Resource design view

Configure a resource for the getstockvolumedetails operation
  1. Initialize the connector. You can use the same configuration to initialize the connector. Please follow the steps given in section 5 for setting up the init operation to the getstockquote operation.

    • redisKey : The name of the key where the hash is stored.
  2. Set up the lLen operation. Navigate into the Palette pane and select the graphical operations icons listed under Redis Connector section. Then drag and drop the hGetAll operation into the Design pane. The hGetAll operation retrieves all the fields and values in a hash.

You only need to send redisKey as parameter. In this example redisKey value is configured as StockVolume

Drag and drop hGetAll operation

  1. Forward the backend response to the API caller. Please follow the steps given in section 8 in the getstockquote operation.
Configure a resource for the deletestockvolumedetails operation
  1. Initialize the connector. You can use the same configuration to initialize the connector. Please follow the steps given in section 5 for setting up the init operation to the getstockquote operation.

  2. Set up the operation.

Navigate into the Palette pane and select the graphical operations icons listed under Redis Connector section. Then drag and drop the hDel operation into the Design pane. The hDel operation deletes one or more hash fields

  - **redisKey** : The name of the key where the hash is stored.
  - **redisFields** : The fields that you want to delete.

  <img src="https://esb-docs.m-ware.eu/assets/img/integrate/connectors/redis-hdell-drag-and-drop.png" title="Drag and drop hDell operation" width="500" alt="Drag and drop hDell operation"/>
  1. Forward the backend response to the API caller. Please follow the steps given in section 8 in the getstockquote operation.

Now you can switch into the Source view and check the XML configuration files of the created API and sequences.

??? note "StockQuoteAPI.xml" <?xml version="1.0" encoding="UTF-8"?> <api context="/stockquote" name="StockQuoteAPI" xmlns="http://ws.apache.org/ns/synapse"> <resource methods="GET" uri-template="/getstockquote/{symbol}"> <inSequence> <payloadFactory media-type="xml"> <format> <m0:getQuote xmlns:m0="http://services.samples"> <m0:request> <m0:symbol>$1</m0:symbol> </m0:request> </m0:getQuote> </format> <args> <arg evaluator="xml" expression="get-property('uri.var.symbol')"/> </args> </payloadFactory> <header name="Action" scope="default" value="urn:getQuote"/> <call> <endpoint> <address format="soap11" uri="http://localhost:9000/services/SimpleStockQuoteService"> <suspendOnFailure> <initialDuration>-1</initialDuration> <progressionFactor>1</progressionFactor> </suspendOnFailure> <markForSuspension> <retriesBeforeSuspension>0</retriesBeforeSuspension> </markForSuspension> </address> </endpoint> </call> <enrich> <source clone="false" type="body"/> <target property="ORIGINAL_PAYLOAD" type="property"/> </enrich> <property expression="$body/soapenv:Envelope/soapenv:Body/ns:getQuoteResponse/ax21:symbol" name="symbol" scope="default" type="STRING" xmlns:ax21="http://services.samples/xsd" xmlns:ns="http://services.samples" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/> <property expression="$body/soapenv:Envelope/soapenv:Body/ns:getQuoteResponse/ax21:volume" name="volume" scope="default" type="STRING" xmlns:ax21="http://services.samples/xsd" xmlns:ns="http://services.samples" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/> <redis.init> <redisHost>127.0.0.1</redisHost> <redisPort>6379</redisPort> <redisTimeout>10000000000</redisTimeout> </redis.init> <redis.hSet> <redisKey>StockVolume</redisKey> <redisField>{$ctx:symbol}</redisField> <redisValue>{$ctx:volume}</redisValue> </redis.hSet> <enrich> <source clone="false" property="ORIGINAL_PAYLOAD" type="property"/> <target type="body"/> </enrich> <log level="full"/> <respond/> </inSequence> <outSequence/> <faultSequence/> </resource> <resource methods="GET" uri-template="/getstockvolumedetails"> <inSequence> <redis.init> <redisHost>127.0.0.1</redisHost> <redisPort>6379</redisPort> <redisTimeout>10000000000</redisTimeout> </redis.init> <redis.hGetAll> <redisKey>StockVolume</redisKey> </redis.hGetAll> <respond/> </inSequence> <outSequence/> <faultSequence/> </resource> <resource methods="POST" uri-template="/deletestockvolumedetails"> <inSequence> <property expression="json-eval($.redisFields)" name="redisFields" scope="default" type="STRING"/> <redis.init> <redisHost>127.0.0.1</redisHost> <redisPort>6379</redisPort> <redisTimeout>10000000000</redisTimeout> </redis.init> <redis.hDel> <redisKey>StockVolume</redisKey> <redisFields>{$ctx:redisFields}</redisFields> </redis.hDel> <respond/> </inSequence> <outSequence/> <faultSequence/> </resource> </api>

Get the project

You can download the ZIP file and extract the contents to get the project code.

Download ZIP

Deployment

Follow these steps to deploy the exported CApp in the integration runtime.

Deploying on Micro Integrator

You can copy the composite application to the <PRODUCT-HOME>/repository/deployment/server/carbonapps folder and start the server. Micro Integrator will be started and the composite application will be deployed.

You can further refer the application deployed through the CLI tool. See the instructions on managing integrations from the CLI.

Click here for instructions on deploying on ESB Enterprise Integrator 6
  1. You can copy the composite application to the <PRODUCT-HOME>/repository/deployment/server/carbonapps folder and start the server.

  2. ESB EI server starts and you can login to the Management Console https://localhost:9443/carbon/ URL. Provide login credentials. The default credentials will be admin/admin.

  3. You can see that the API is deployed under the API section.

Testing

Invoke the API as shown below using the curl command. Curl Application can be downloaded from here.

  1. Retrieve stock volume details from the Stockquote back-end service.

Sample request 1

```
 curl -v GET "http://localhost:8290/stockquote/view/WSO2" -H "Content-Type:application/json"    
```

Expected Response

 ```json
 {
     "Envelope": {
         "Body": {
             "getQuoteResponse": {
                 "change": -2.86843917118114,
                 "earnings": -8.540305401672558,
                 "high": -176.67958828498735,
                 "last": 177.66987465262923,
                 "low": -176.30898912339075,
                 "marketCap": 56495579.98178506,
                 "name": "ESB Company",
                 "open": 185.62740369461244,
                 "peRatio": 24.341353665128693,
                 "percentageChange": -1.4930577008849097,
                 "prevClose": 192.11844053187397,
                 "symbol": "WSO2",
                 "volume": 7791
             }
         }
     }
 }
 ```

Sample request 2

 ```
  curl -v GET "http://localhost:8290/stockquote/view/IBM" -H "Content-Type:application/json"    
 ```

Expected Response

 ```json
 {
     "Envelope": {
         "Body": {
             "getQuoteResponse": {
                 "change": -2.86843917118114,
                 "earnings": -8.540305401672558,
                 "high": -176.67958828498735,
                 "last": 177.66987465262923,
                 "low": -176.30898912339075,
                 "marketCap": 56495579.98178506,
                 "name": "IBM Company",
                 "open": 185.62740369461244,
                 "peRatio": 24.341353665128693,
                 "percentageChange": -1.4930577008849097,
                 "prevClose": 192.11844053187397,
                 "symbol": "IBM",
                 "volume": 7791
             }
         }
     }
 }      

 ```

Inserted hash map can check using redis-cli

 Log in to the `redis-cli` and execute `HGETALL StockVolume` command to retrieve inserted hash map details.

 ```
 127.0.0.1:6379> HGETALL StockVolume
 1) "IBM"
 2) "7791"
 3) "WSO2"
 4) "7791"
 127.0.0.1:6379>
 ```
  1. Retrieve all stock volume details from the Redis server.

Sample request

```
 curl -v GET "http://localhost:8290/stockquote/getstockvolumedetails" -H "Content-Type:application/json"    
```

Expected Response

 ```json
 {
     "output": "{IBM=7791, WSO2=7791}"
 }
 ```
  1. Remove stock volume details.

Sample request 1

```
 curl -v POST -d {"redisFields":"WSO2"}  "http://localhost:8290/stockquote/deletestockvolumedetails" -H "Content-Type:application/json"    
```

Expected Response

 ```json
 {
     "output": 1
 }
 ```

Sample request 2 : Check the remaining stock volume details

Sample request

 ```
   curl -v GET "http://localhost:8290/stockquote/getstockvolumedetails" -H "Content-Type:application/json"    
 ```

Expected Response

 ```json
 {
      "output": "{IBM=7791}"
 }
 ```

Inserted list can retrieve using redis-cli

 Log in to the `redis-cli` and execute `HGETALL StockVolume` command to retrieve list length.

 ```
 127.0.0.1:6379> HGETALL StockVolume
 1) "IBM"
 2) "7791"
 127.0.0.1:6379>
 ```
Top