The recently upgraded Redis connector for Mule allows you to interact with this NoSQL data-store in a convenient manner. This blog is a tutorial that you can follow in order to get your feet wet with Redis, if you don’t know it already, or Mule, if you have Redis experience and want to see how they both can work together.
In this tutorial, we will build a very simple back-end that captures page visit count for identified users via a web bug. This example illustrates the usage of Mule as a tool for capturing events and routing them to NoSQL storage for later analysis.
In order to follow this tutorial you’ll need:
- A working installation of a stable version of Redis (version 2.4 or above),
- A working installation of a recent version of Mule Studio.
Getting Mule Studio Ready
The Redis module doesn’t come bundled with Mule Studio so we have to install it first. For this, we have to do the following:
- Open Mule Studio and from “Help” menu select “Install New Software…”. The installation dialog – shown below – opens.
- From “Work with” drop down, select “MuleStudio Cloud Connectors Update Site”. The list of available connectors will be shown to you.
- Find and select the Redis module in the list of available connectors, the tree structure that is shown below. A faster way to find a specific connector is to filter the list by typing the name of the connector in the input box above the list. You can choose more than one connector to be installed at once.
- When you are done selecting the Redis module, click on “Next” button. Installation details are shown on the next page. Click on “Next” button again and accept the terms of the license agreement.
- Click on “Finish” button. The Redis module is downloaded and installed onto Studio. You’ll need to restart the Studio for the installation to be completed.
Setting up the project
Now that we’ve got Mule Studio up and running, it’s time to work on the Mule Application. Create a new Mule Project by clicking on “File > New > Mule Project”. In the new project dialog box, the only thing you are required to enter is the name of the project: use “redis-example” or similar. You can click on “Next” to go through the rest of pages.
The first thing to do in our new application is to configure the Redis connector to connect to our local server.
We assume that you have not added security (password protection) nor changed the default port it listens to.
For this, in the message flow editor, click on “Global Elements” tab on the bottom of the page. Then click on “Create” button on the top right of the tab. In the “Choose Global Element” type dialog box that opens select “Redis” under “Cloud Connectors” and click OK.
In the Redis configuration dialog box that follows, set the name to “Redis”.
You are done with the configuration. Click “OK” to close the dialog box.
Building the web bug service flow
It’s time to start building the flow that will:
- accept HTTP GET requests,
- increment a user-specific hit map, keyed by page ID,
- and return an empty GIF response.
Start by dropping an
HTTP element on the visual editor and configure it as below:We want to asynchronously increment the map in Redis, so first drop an async scope in the flow, right after the HTTP endpoint. Then drop a Redis connector element right inside the async scope. Double-click it and configure it as shown below:
Now let’s return an empty HTTP response, with the GIF content type. Drop an “HTTP Response Builder” in the flow, after the async scope and configure it as shown below:
That’s it! Your flow should look very much like this:
The final flow XML should be similar to this:
Testing the application
Now it’s time to test the application. Run the application in Mule Studio using
Run As > Mule Application.
If you browse
http://localhost:8081/tracking/wb.gif?userId=123456&pageId=ABC you should get an empty page in the browser.
You can also embed the following in an HTML page and load this page:
<img src="http://localhost:8081/tracking/wb.gif?userId=123456&pageId=ABC" alt="" width="0" height="0">
To see what happened behind the scene, connect to the Redis server with its command line client (
redis-cli). Then run:
redis 127.0.0.1:6379> EXISTS 123456
A user-specific hit count map has been created for user ID
123456. Now run:
redis 127.0.0.1:6379> HKEYS 123456
Hits have been recorded for the page ID
ABC. Now run:
redis 127.0.0.1:6379> HGET 123456 ABC
The hit count is currently 1. You should see this count increase as you keep hitting:
Your turn now
This simple tutorial shown you how easy it is to leverage the power of Redis from Mule. Now it’s your turn! Don’t hesitate to share your success stories with Mule and Redis as comments to this post.