Mule and Redis Get a Web Bug
Mule and Redis Get a Web Bug
Join the DZone community and get the full member experience.Join For Free
SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.
Originally authored by David Dossot
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 the 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 the “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. 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 the “Next” button again and accept the terms of the license agreement.
- Click on the “Finish” button. The Redis module is downloaded and installed onto Studio. You’ll need to restart Mule 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 the “Global Elements” tab on the bottom of the page. Then click the “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 like you see 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 much like this:
The final flow XML should be similar to this:
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:redis="http://www.mulesoft.org/schema/mule/redis" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/redis http://www.mulesoft.org/schema/mule/redis/3.4/mule-redis.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd"> <redis:config name="Redis" doc:name="Redis" /> <flow name="redis-exampleFlow1" doc:name="redis-exampleFlow1"> <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="tracking/wb.gif" doc:name="HTTP" /> <async doc:name="Async"> <redis:hash-increment config-ref="Redis" field="#[message.inboundProperties.pageId]" key="#[message.inboundProperties.userId]" doc:name="Redis" /> </async> <http:response-builder status="204" contentType="image/gif" doc:name="HTTP Response Builder" /> </flow> </mule>
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 scenes, connect to the Redis server with its command line client (
redis-cli). Then run:
redis 127.0.0.1:6379> EXISTS 123456 (integer) 1
A user-specific hit count map has been created for user ID
123456. Now run:
redis 127.0.0.1:6379> HKEYS 123456 1) "ABC"
Hits have been recorded for the page ID
ABC. Now run:
redis 127.0.0.1:6379> HGET 123456 ABC "1"
The hit count is currently 1. You should see this count increase as you keep hitting:
Your Turn Now
This simple tutorial shows 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.
Published at DZone with permission of Ross Mason , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.