Mule and Redis get a web bug

September 10 2013

9 comments 0
motif

The recently upgraded Redis connector for Mule allows you to interact with this data-store in a convenient manner. This blog is a tutorial that you can follow in order to get your feet wet with , if you don’t know it already, or Mule, if you have 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.

Prerequisites

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:

  1. Open Mule Studio and from “Help” menu select “Install New Software…”. The installation dialog – shown below – opens.
  2. From “Work with” drop down, select “MuleStudio Cloud Connectors Update Site”. The list of available connectors will be shown to you.
  3. 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.
  4. 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.
  5. 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:

Flow XML

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&amp;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:

A user-specific hit count map has been created for user ID 123456. Now run:

Hits have been recorded for the page ID ABC. Now run:

The hit count is currently 1. You should see this count increase as you keep hitting: http://localhost:8081/tracking/wb.gif?userId=123456&amp;pageId=ABC

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.

 


We'd love to hear your opinion on this post


9 Responses to “Mule and Redis get a web bug”

  1. I am running on windows, I cann’t install redis in my machine. so sad.

  2. There seems to be two bugs in this blog example. I ran it on version=”EE-3.4.2″.

    1. The ‘dot’ syntax for MEL in the Redis field and key does not seem to work, so
    #[message.inboundProperties[‘pageId’]]
    works for me, but
    #[message.inboundProperties.pageId]
    gets a stack trace
    [Error: could not access: pageId; in class: org.mule.el.context.MessagePropertyMapContext]
    [Near : {… message.inboundProperties.page ….}]

    B. The URL put in the browser address bar seems cannot be url-encoded, so
    http://localhost:8081/tracking/wb.gif?userId=123456&pageId=ABC
    works for me but the above
    http://localhost:8081/tracking/wb.gif?userId=123456&pageId=ABC
    gets a stack trace: redis.clients.jedis.exceptions.JedisDataException: value sent to redis cannot be null

  3. To moderator:
    Looks like any url-encoded entity in a url is not displayed literally in the comment posting for this blog. So, if you’re able to edit my comment above, for the second ‘bug’ reported, the url-encoded url that I had pasted into the browser (which causes the JedisDataException) is what is shown in the last section of the blog post, just above the last section “Your Turn Now”.

  4. Thanks for reporting the issue. Note that this tutorial has been developed and tested with Mule EE-3.4.0.

    1. The “dot” syntax fails if the pageId query parameter is not provided. The assumption is that this query parameter is mandatory and has to be provided in the URL.
    B. Indeed, the URL in the post is wrong. WordPress has replaced the & (ampersand) character with an & entity which is completely wrong.

  5. The pageId query parameter in my test was in the url. I have the same browser left open with same url in address bar, and the example app fails with the exception given above when using the dot syntax, but works ok with the [‘pageId’] syntax.

    I’m thinking that message.inboundProperties is a MEL map, which does not appear to support a dot syntax, per documentation here: http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+MEL#MuleExpressionLanguageMEL-ReferringtoMapData

  6. Access map entries with dot should work: http://mvel.codehaus.org/MVEL+2.0+Property+Navigation#MVEL2.0PropertyNavigation-MapAccess

    Could you try with the same back-end as the example (EE-3.4.0)? Maybe this has changed in EE-3.4.2.

  7. The dot syntax does seem to work, even using spring version=”EE-3.4.2″.
    In fact, both syntax seem to work, e.g.

    (So must have been “operator error” the first time around; sorry about delay in verifying and getting back to you.)

  8. In previous reply, tried to show the “redis:hash-increment” element I have in my flow, but looks like the blogging software used here does not display anything within angle brackets 🙁