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 WSO2 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 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 to the Setting up the Redis Connector documentation. Follow the steps listed under
Setting up the Redis server
section to setup the Redis server andSet up the back-end service
section to setup the Stockquote service. This example demonstrates how to use Redis connector to:
- 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' stock volume details. Since the “symbol” that is sent in the request is “WSO2”, the request is routed to the WSO2 endpoint. Once the response from the WSO2 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 into the Redis hashmap).
- Retrieve all stock volume details from the Redis server.
- Remove stock volume details from the Redis server.
All three operations are exposed via a StockQuoteAPI
API. The API with the context /stockquote
has three 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 WSO2 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.
If you do not want to configure this yourself, you can simply get the project and run it.
Setup the Integration Project¶
-
Launch Visual Studio Code with the Micro Integrator for VS Code extension (MI for VS Code) installed.
Info
Follow the Install Micro Integrator for VS Code documentation for a complete installation guide.
-
Create an integration project.
Add integration logic¶
Create the Endpoint¶
-
Navigate to MI Project Explorer > Endpoints and click on the + icon next to Endpoints.
-
Select Address Endpoint.
-
In Endpoint Artifact interface that appears, specify the following values and click Create.
Parameter Value Endpoint Name StockQuoteEP
URI Template http://localhost:9000/services/SimpleStockQuoteService
Format SOAP 1.1
Create the REST API¶
-
Navigate to MI Project Explorer > APIs and click on the + sign next to APIs to open the Synapse API Artifact creation form.
-
Specify the API name as
SampleRedisAPI
and the API context as/stockquote
and click Create.After creating the API artifact, the Service Designer pane will be displayed with the default API resource.
-
Click on the options icon and select edit to edit the resource.
Specify values for the required resource properties:
Parameter Value Url Style URL_TEMPLATE
Uri Template /getstockquote/{symbol}
Methods GET
-
To create the other two resources, click on the + Resource button on the Service Designer and configure the following values.
Parameter resource 2 resource 3 Url Style URL_TEMPLATE
URL_TEMPLATE
Uri Template /getstockvolumedetails
/deletestockvolumedetails
Methods GET
POST
Create the mediation logic¶
Configure a resource for the getstockquote operation¶
Configure a resource that sets up a 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 WSO2 stock volume details. To achieve this, follow the steps below.
-
Click on the
GET getstockquote
API resource under available resources on the Service Designer.You will now see the graphical view of the
SampleRedisAPI
. -
Click on the + icon under start to add a mediator.
-
Add a payload factory mediator from the mediator palette to extract the selected stock details. In this sample, we attempt to get WSO2 stock details from the
SimpleStockQuoteService
.Parameter Value Payload Format Inline
Media Type xml
Template Type default
Payload <m0:getQuote xmlns:m0="http://services.samples"> <m0:request> <m0:symbol>$1</m0:symbol> </m0:request> </m0:getQuote>
Add parameter:
Argument value (Expression) get-property('uri.var.symbol')
Evaluator xml
-
Add a header to get a quote from the
SimpleStockQuoteService
.Parameter Value Header Name Action
Header Action set
Scope default
Header Value Type LITERAL
Header Value Literal urn:getQuote
-
Add an address endpoint using the call mediator to access
SimpleStockQuoteService
. Select the previously createdStockQuoteEP
from the dropdown. -
In this example, we copy the original payload to a property using the Enrich mediator.
-
To get the input values into the
hSet
, we can use the property mediator. Navigate into the Mediator Palette and select theProperty
mediators. -
Add the property mediator to capture the
symbol
value from the response ofSimpleStockQuoteService
. The 'symbol' contains the company name of the stock quote.Property Name symbol
Property Value (Expression) $body/soapenv:Envelope/soapenv:Body/ns:getQuoteResponse/ax21:symbol
-
Add the property mediator to capture the
volume
values. The 'volume' contains the stock quote volume of the selected company.Property Name volume
Property Value (Expression) $body/soapenv:Envelope/soapenv:Body/ns:getQuoteResponse/ax21:volume
-
Add Redis connector operation.
- Click + icon under the property mediator.
-
Select the Redis connector from the Connectors section.
-
Select hSet from the connector operations.
- Next, you have to create a Redis connection. Click the Add new connection.
-
Configure the values as shown below and click Add.
redisHost The Redis hostname (default: localhost)
redisPort The port on which the Redis server is running (default: 6379)
-
Navigate to the Add hSet form and select the created
redis
connection from the Connection Type dropdown. -
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.
Configure the following values and click submit
redisKey StockVolume
redisField ctx:symbol</code></td> </tr> <tr> <td>redisValue</td> <td><code>ctx:volume
In this example, the
redisKey
value is configured as StockVolume. While invoking the API, the aboveredisField
,redisValue
parameter values are extracted from the response of the SimpleStockQuoteService. Then they are populated as an input value for the RedishSet
operation.
-
Then, to get the original payload, we replace the message body with the property value using the Enrich mediator as follows.
-
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 of the current message and sends the message back to the client as a response.-
Add the respond mediator to the Resource view.
-
Once you have setup the resource, you can see the
getstockquote
resource as shown below.
-
Configure a resource for the getstockvolumedetails operation¶
-
Navigate to the Service Designer view of the API and select the
getstockvolumedetails
resource. -
Click + under start and select the Redis connector from the Connectors section.
-
Select hGetAll from the connector operations.
- Select
REDIS_CONNECTION_1
from the connection dropdown. -
You only need to send redisKey as a parameter. In this example
redisKey
value is configured as StockVolume -
To forward the backend response to the API caller add the Respond Mediator.
Configure a resource for the deletestockvolumedetails operation¶
-
Navigate to the Service Designer view of the API and select the
deletestockvolumedetails
resource. -
Click the + under the start and select the Redis connector from the Connectors section.
-
Select hDel from the connector operations. This operation deletes one or more hash fields
- Select
REDIS_CONNECTION_1
from the connection dropdown. -
Configure the values.
redisKey StockVolume
redisFields $ctx:redisFields
-
To forward the backend response to the API caller add the Respond Mediator.
Now you can switch to the Source view and check the XML configuration files of the created API and endpoint.
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" template-type="default">
<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" action="set" scope="default" value="urn:getQuote"/>
<call>
<endpoint key="StockQuoteEP"/>
</call>
<enrich description="">
<source clone="true" type="body"/>
<target action="replace" type="property" property="ORIGINAL_PAYLOAD"/>
</enrich>
<property name="symbol" scope="default" type="STRING" expression="$body/soapenv:Envelope/soapenv:Body/ns:getQuoteResponse/ax21:symbol" xmlns:ax21="http://services.samples/xsd" xmlns:ns="http://services.samples" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/>
<property name="volume" scope="default" type="STRING" expression="$body/soapenv:Envelope/soapenv:Body/ns:getQuoteResponse/ax21:volume" xmlns:ax21="http://services.samples/xsd" xmlns:ns="http://services.samples" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/>
<redis.hSet configKey="REDIS_CONNECTION_1">
<redisKey>StockVolume</redisKey>
<redisField>{$ctx:symbol}</redisField>
<redisValue>{$ctx:volume}</redisValue>
</redis.hSet>
<enrich description="">
<source clone="true" property="ORIGINAL_PAYLOAD" type="property"/>
<target action="replace" type="body"/>
</enrich>
<log category="INFO" level="simple"/>
<respond/>
</inSequence>
<faultSequence>
</faultSequence>
</resource>
<resource methods="GET" uri-template="/getstockvolumedetails">
<inSequence>
<redis.hGetAll configKey="REDIS_CONNECTION_1">
<redisKey>StockVolume</redisKey>
</redis.hGetAll>
<respond/>
</inSequence>
<faultSequence/>
</resource>
<resource methods="POST" uri-template="/deletestockvolumedetails">
<inSequence>
<property expression="json-eval($.redisFields)" name="redisFields" scope="default" type="STRING"/>
<redis.hDel configKey="REDIS_CONNECTION_1">
<redisKey>StockVolume</redisKey>
<redisFields>{$ctx:redisFields}</redisFields>
</redis.hDel>
<respond/>
</inSequence>
<faultSequence/>
</resource>
</api>
StockQuoteEP Endpoint
<?xml version="1.0" encoding="UTF-8"?>
<endpoint name="StockQuoteEP" xmlns="http://ws.apache.org/ns/synapse">
<address format="soap11" uri="http://localhost:9000/services/SimpleStockQuoteService">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</address>
</endpoint>
REDIS_CONNECTION_1
The created Redis connection will be saved as a local entry.
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="REDIS_CONNECTION_1" xmlns="http://ws.apache.org/ns/synapse">
<redis.init>
<connectionType>REDIS</connectionType>
<name>REDIS_CONNECTION_1</name>
<redisHost>127.0.0.1</redisHost>
<redisPort>6379</redisPort>
</redis.init>
</localEntry>
Get the project¶
You can download the ZIP file and extract the contents to get the project code.
Deployment¶
Follow these steps to deploy the exported Carbon Application (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 WSO2 Enterprise Integrator 6
-
You can copy the composite application to the
<PRODUCT-HOME>/repository/deployment/server/carbonapps
folder and start the server. -
WSO2 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.
-
You can see that the API is deployed under the API section.
Testing¶
Deploy the backend service SimpleStockQuoteService
.
- Download the ZIP file of the back-end service from here.
- Extract the downloaded zip file.
- Open a terminal and navigate to the
axis2Server/bin/
directory inside the extracted folder. -
Execute the following command to start the axis2server with the SimpleStockQuote back-end service:
sh axis2server.sh
axis2server.bat
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/getstockquote/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/stockquote/getstockquote/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
}
}
}
}
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/stockquote/getstockvolumedetails" -H "Content-Type:application/json"
Expected Response
{
"output": "{IBM=7791, WSO2=7791}"
}
3. 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
{
"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
{
"output": "{IBM=7791}"
}
Inserted list can retrieve using redis-cli
Log in to the redis-cli
and execute HGETALL StockVolume
command to retrieve the list length.
127.0.0.1:6379> HGETALL StockVolume
1) "IBM"
2) "7791"
127.0.0.1:6379>