Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

Redis Connector

Select

Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

The Redis Connector offers complete support for its CRUD API.

Prerequisites

This document assumes that you are familiar with Mule, Anypoint Connectors, Anypoint Studio essentials, elements in a Mule flow, and global elements.

Namespace and Schema

When designing your application in Studio, the act of dragging the connector from the palette onto the Anypoint Studio canvas should automatically populate the XML code with the connector namespace and schema location.

If you manually code the Mule application in Studio’s XML editor or other text editor, paste these into the header of your Configuration XML, inside the <mule> tag.


         
      
1
2
3
4
5
6
7
8
<mule xmlns:redis="http://www.mulesoft.org/schema/mule/redis"
  ...
  xsi:schemaLocation="http://www.mulesoft.org/schema/mule/redis http://www.mulesoft.org/schema/mule/connector/current/mule-redis.xsd">
  ...
  <flow name="yourFlow">
  ...
  </flow>
</mule>

Using the Connector in a Mavenized Mule App

If you are coding a Mavenized Mule application, this XML snippet must be included in your pom.xml file.


         
      
1
2
3
4
5
<dependency>
  <groupId>org.mule.modules</groupId>
  <artifactId>mule-redis-connector</artifactId>
  <version>RELEASE</version>
</dependency>

Inside the <version> tags, put the desired version number, the word RELEASE for the latest release, or SNAPSHOT for the latest available version.

Compatibility

Software Version

Mule Runtime

4.0.0 or higher

Redis server

2.8.x, 3.0.x, and 3.2.x

Installing

  1. In Anypoint Studio, click the Exchange icon in the Studio taskbar.

  2. Click Login in Anypoint Exchange.

  3. Search for the connector and click Install.

  4. Follow the prompts to install the connector.

When Studio has an update, a message displays in the lower right corner, which you can click to install the update.

Configuring

To use Redis connector in your Mule application, configure a global Redis element that can be used by all the connectors in the application.

The sections that follow provide the properties to configure the global element.

Non-Clustered Configuration

Configuration

In the image above, the placeholder values refer to a configuration file placed in the src folder of your project. You can either hardcode your credentials into the global configuration properties, or reference a configuration file that contains these values. For simpler maintenance and better re-usability of your project, Mule recommends that you use a configuration file. Keeping these values in a separate file is useful if you need to deploy to different environments, such as production, development, and QA, where your access credentials differ.

Field Description

Name

Enter a name for this configuration to reference it later.

Host

Redis host. Use this field when you want to access you Redis server that is not part of a cluster through the connector.

Port

Redis port. The port on which your non cluster server is running.

Connection Timeout

Connection timeout in milliseconds.

Entry Ttl

The default time to live to be set on keys stored through the connector.

Password

Redis password. Set this in case that the server requires authentication.

Clustered Configuration

Configuration

Field Description

Name

Enter a name for this configuration to reference it later.

Cluster Hosts

Redis cluster hosts, separated by comma and with the format host:port. Use this field when you want to access you Redis cluster through the connector.

Connection Timeout

Connection timeout in milliseconds.

Entry Ttl

The default time to live to be set on keys stored through the connector.

Upgrading from an Older Version

If you’re currently using an older version of the connector, a small popup appears in the bottom right corner of Anypoint Studio with an "Updates Available" message.

  1. Click the popup and check for available updates.

  2. Click the Connector version checkbox and click Next and follow the instructions provided by the user interface.

  3. Restart Studio when prompted.

  4. After restarting, when creating a flow and using the Object Store Connector, if you have several versions of the connector installed, you may be asked which version you would like to use. Pick one of these.

We recommend that you keep Studio up to date with the connector’s latest version.

Using This Connector

You can use this connector as an inbound endpoint for consuming messages from a channel. You can subscribe to one of these channels through the connector or as an outbound connector for sending commands to the Redis server.

Example Use Case

Save a value for a key into Redis server

This use case describes how to use the connector for assigning a value to a key into the Redis server.

  1. Create a new Mule Project by clicking File > New > Mule Project.

  2. In the new project dialog box, the only thing you are required to enter is a name for your new project. Click Finish.

    New project dialog

  3. Navigate through the project’s structure and double click src/main/mule/project-name.xml to open it. The steps below are all performed on this file.

  4. Go to the palette and search for HTTP, then drag and drop a new HTTP Connector Listener on canvas. This element is the entry point for the flow and provides the key and value to be set for that key.

  5. Go to the palette and search for Redis, then drag and drop a new Redis Set operation after HTTP connector. This element is going to send data to the Redis server.

  6. Double click the Redis Set operation and set its properties as follows:

    1. Set Display Name to Set Value For Key Into Redis.

    2. Choose from the Extension Configuration drop down Redis__Configuration, which is the default name of a configuration, or any other configuration that you configured.

    3. Choose from Operation drop down "Set".

    4. Set Key to #[payload.key].

    5. Set Value to #[payload.value].

      Redis set operation configuration

  7. Go to the palette and search for Set Payload, then drag and drop a new Set Payload element after the Redis connector. This element creates the response for the incoming HTTP request.

    Unconfigured producer flow

  8. Double click the flow’s top margin to open its properties, and change the name of the flow to "set-flow".

    Set flow configuration

  9. Double click the HTTP Connector [Listener] to open its properties.

    1. Click the green plus sign beside the Connector Configuration drop down menu.

    2. A pop-up appears, leave the default configurations and click ok.

    3. Set Path to /.

    4. Set Display Name to Listener.

      Set HTTP configuration

  10. Double click Set Payload and set its properties as below.

    1. Set Display Name to Set Value Response.

    2. Set Value to Value Successfully Set.

      Set HTTP response configuration

  11. If you configured Redis global element with placeholder values you must now provide values for these placeholders. Open /src/main/resources/mule-app.properties and provide values for following properties: config.host, config.port and config.connectionTimeout

  12. Deploy the app.

  13. Once the app is running, send an HTTP request to it to trigger it’s flow. To do this, use the CURL command line utility or an HTTP client app (such as Postman) to send a POST request with content-type application/x-www-form-urlencoded and a body in urlencoded format to localhost:8081/. The request’s body should contain a key and a value. For this you can use the following CURL command:

    curl -X POST -d "key=test-key" -d "value=test-value" localhost:8081/

    Congratulations! You have just set a value for a key into the redis server.

Save a value for a key into Redis server code

  1. Add the Redis namespace to the Mule element:

    xmlns:redis="http://www.mulesoft.org/schema/mule/redis"
  2. Add the location of the Redis schema referred to by the Redis namespace:

    
                 
              
    1
    2
    
    http://www.mulesoft.org/schema/mule/redis
    http://www.mulesoft.org/schema/mule/sfdc-composite/current/mule-redis.xsd
  3. Add the HTTP namespace to the Mule element:

    xmlns:http="http://www.mulesoft.org/schema/mule/http"
  4. Add the location of the HTTP schema referred to by the HTTP namespace:

    
                 
              
    1
    2
    
    http://www.mulesoft.org/schema/mule/http
    http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
  5. Add a redis:config element to your project, then configure its attributes:

    
                 
              
    1
    2
    3
    
    <redis:config name="Redis__Configuration" host="${config.host}"
     connectionTimeout="${config.connectionTimeout}" port="${config.port}"
     doc:name="Redis: Configuration"/>
  6. Add a http:listener-config element to your project, and configure its attributes:

    
                 
              
    1
    2
    
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0"
     port="8081" doc:name="HTTP Listener Configuration"/>
  7. Add an empty flow element to your project:

    
                 
              
    1
    2
    
    <flow name="set-flow">
    </flow>
  8. Within the flow element, add an http:listener element:

    
                 
              
    1
    
    <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="Set value HTTP endpoint"/>
  9. Within the flow element, add a redis:set after the http:listener:

    
                 
              
    1
    
    <redis:set config-ref="Redis__Configuration" key="#[payload.key]" value="#[payload.value]" doc:name="Set value for key into Redis"/>
  10. Within the flow element, add a set-payload element after redis:set:

    
                 
              
    1
    
    <set-payload value="Value successfully set" doc:name="Set value response"/>
  11. When you’re done, the XML file should look like this:

    
                 
              
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <mule xmlns:redis="http://www.mulesoft.org/schema/mule/redis"
    xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
    xmlns:http="http://www.mulesoft.org/schema/mule/http"
    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"
            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/http
    http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
    http://www.mulesoft.org/schema/mule/ee/tracking
    http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
    http://www.mulesoft.org/schema/mule/redis
    http://www.mulesoft.org/schema/mule/redis/current/mule-redis.xsd">
        <redis:config name="Redis__Configuration" host="${config.host}" connectionTimeout="${config.connectionTimeout}" port="${config.port}"
        doc:name="Redis: Configuration"/>
        <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0"
        port="8081" doc:name="HTTP Listener Configuration"/>
        <flow name="set-flow">
            <http:listener config-ref="HTTP_Listener_Configuration" path="/"
             doc:name="Set value HTTP endpoint"/>
            <redis:set config-ref="Redis__Configuration" key="#[payload.key]"
             value="#[payload.value]" doc:name="Set value for key into Redis"/>
            <set-payload
             value="Successfully set value: #[payload.value] to key: #[payload.key]"
             doc:name="Set value response"/>
        </flow>
    </mule>