Integrating with Redis in WSO2 Enterprise Integrator 7.0.0.

Suman Mohan
5 min readMar 31, 2021

This article explains how we can integrate with Redis in WSO2 Enterprise Integrator 7.0.0.

Overview:

We will create a REST API in WSO2 Integration Studio having 3 resources with the below 3 functionalities: -

1. Call a back-end service named Stockquote service to get stock details, insert some of that response data into Redis and respond back to the client with the response data.

2. Get stock data from Redis.

3. Delete stock data from Redis.

Prerequisites:

1. A configured Redis server on your machine (Download and setup link: https://redis.io/download).

2. WSO2 EI 7.1.0 (https://wso2.com/integration/#).

3. WSO2 Integration Studio (https://wso2.com/integration/integration-studio/).

4. Set up the Stockquote back-end service.

a) Download the stockquote_service.jar.

b) Open a terminal, navigate to the location of the downloaded service, and run it using the following command:

java -jar stockquote_service.jar

API implementation in WSO2 Integration studio:

1. Open WSO2 Integration Studio and create an Integration Project. Select Create Composite Application Project and Create Connector Exporter Project while creating the Integration Project.

2. Right click on the main project and click on Add or Remove Connector à Add Connector. You will get directed to the WSO2 Connector Store.

3. Search for the specific connector (Redis connector in this case) required for your integration scenario and download it to the workspace.

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

5. Additionally, we must also add the connector to the connector exporter project. This is needed to access the Redis connector and its operations at runtime.

6. Right click on the connector exporter project and click on New-> Add/Remove Connectors.

7. Then in the Add or Remove Connectors window, click on Next -> Workspace. The Redis connector should be available. Select it and click on OK. Then click on Finish.

8. The Redis connector zip file should be now be present under the Connector Exporter project.

9. Right click on the created Integration Project and select, New -> Rest API to create the REST API. Specify the API name as SampleRedisAPI and API context as /resources.

10. Go to the Source view of the API and paste the below XML code.

<?xml version=”1.0" encoding=”UTF-8"?>

<api context=”/resources” name=”SampleRedisAPI” xmlns=”http://ws.apache.org/ns/synapse">

<resource methods=”GET” uri-template=”/stockquote/view/{symbol}”>

<inSequence>

<payloadFactory description=”simplestockquoteserviceinput” 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 description=”passAction” 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>

<maximumDuration>0</maximumDuration>

</suspendOnFailure>

<markForSuspension>

<retriesBeforeSuspension>0</retriesBeforeSuspension>

</markForSuspension>

</address>

</endpoint>

</call>

<enrich description=”storebackendresponse”>

<source clone=”true” type=”body”/>

<target property=”backendresponse” type=”property”/>

</enrich>

<property description=”getSymbol” 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 description=”getVolume” 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>localhost</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=”true” property=”backendresponse” type=”property”/>

<target type=”body”/>

</enrich>

<respond/>

</inSequence>

<outSequence/>

<faultSequence/>

</resource>

<resource methods=”GET” uri-template=”/getstockquotefromredis”>

<inSequence>

<redis.init>

<redisHost>localhost</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=”/deletestockquote”>

<inSequence>

<redis.init>

<redisHost>localhost</redisHost>

<redisPort>6379</redisPort>

<redisTimeout>10000000000</redisTimeout>

</redis.init>

<redis.hDel>

<redisKey>StockVolume</redisKey>

<redisFields>{$body//redisFields}</redisFields>

</redis.hDel>

<respond/>

</inSequence>

<outSequence/>

<faultSequence/>

</resource>

</api>

11. Go to the Design view and review it. You will see 3 resources as shown below. Save the project.

i) to invoke the Stockquote service, retrieve stock details and save some stock data in Redis.

ii) to get stock data from Redis which was stored using the first resource.

iii) to delete stock data present in Redis.

12. Expand the Composite Application Project, open pom.xml file, select the main project which contains the API and the Connector Exporter Project. Create the CAR file and save it on your machine.

13. Copy the composite application (CAR file) to the <EI_Home>/repository/deployment/server/carbonapps folder and start the EI server. Micro Integrator will be started, and the composite application will be deployed.

Testing: Invoke the API using the below curl commands.

  1. Retrieve stock volume details from the Stockquote back-end service and insert stock data into Redis.

Sample Request 1: curl -v GET “http://localhost:8290/resources/stockquote/view/WSO2" -H “Content-Type:application/json”

Expected Response:

{

“Envelope”: {

“Body”: {

“getQuoteResponse”: {

“change”: -2.86843917118114,

“earnings”: -8.540305401672558,

“high”: -176.67958828498735,

“last”: 177.66987465262923,

“low”: -176.30898912339075,

“marketCap”: 56495579.98178506,

“name”: “WSO2 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/resources/stockquote/view/IBM" -H “Content-Type:application/json”

Expected Response:

{

“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 be checked 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>

2. Retrieve all stock volume details from the Redis server.

Sample Request: curl -v GET “http://localhost:8290/resources/getstockquotefromredis" -H “Content-Type:application/json”

Expected Response:

{

“output”: “{IBM=7791, WSO2=7791}”

}

3. Remove stock volume details from the Redis server.

Sample Request: curl -v POST -d {“redisFields”:”WSO2"} “http://localhost:8290/resources/deletestockquote" -H “Content-Type:application/json”

Expected Response:

{

“output”: 1

}

Now check the remaining data in Redis:

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>

Reference: https://ei.docs.wso2.com/en/7.0.0/micro-integrator/references/connectors/redis-connector/redis-connector-overview/

--

--