How To Connect Redis and Mule ESB Using Spring Data Redis
Module
In this article I am going to show you to connect Redis key-value data store and Mule ESB using
Spring Data Redis Module.
Pre-requisite
 Anypoint Studio 6 or above
 Mule ESB 3.8 or above
 Redis 3.2 - https://github.com/MSOpenTech/redis/releases/download/win-3.2.100/Redis-
x64-3.2.100.msi
 Apache Maven 3.3.9 - http://mirror.fibergrid.in/apache/maven/maven-
3/3.3.9/binaries/apache-maven-3.3.9-bin.zip
 Spring Data Redis 1.7.3
 Jedis 2.9.0 – Java driver for Redis.
Use Case
Let’s take a use case where the end user is provided with a user registration HTML form. The
submitted user registration details are then transformed into a POJO and subsequently stored in
REDIS data store. Apart from storing object in Redis, the use case deals with query and delete
operations as well. The scope of this use case is limited to Redis Hash operations only.
Solution
1. Create a Maven based Mule project with Anypoint Studio. In the article it was named
integrating_with_redis.
2. Add the following dependencies in pom.xml:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl
-->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
Note: The above dependencies must be added at the top of the
dependency list to avoid version conflict with regards to Spring
framework.
3. Define Global Elements for Spring Redis Configuration and HTTP Listener.
a. JedisConnectionFactory – is required to create and configure
JedisConnectionFactory by which we should be able to connect to Redis.
<spring:bean id="jedisConnectionFactory"
name="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectio
nFactory">
</spring:bean>
b. RedisTemplate – is required to be configured so as to read/write data to and from
Redis.
<spring:bean id="redisTemplate" name="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate">
<spring:property name="connectionFactory"
ref="jedisConnectionFactory" />
<spring:property name="valueSerializer">
<spring:bean
class="org.springframework.data.redis.serializer.JacksonJsonRedisSeri
alizer">
<spring:constructor-arg
type="java.lang.Class"
value="java.lang.Object" />
</spring:bean>
</spring:property>
</spring:bean>
c. HTTP Listener – is required to be configured for listening to HTTP requests.
<http:listener-config name="HTTP_Listener_Configuration"
host="0.0.0.0" port="8081" doc:name="HTTP Listener
Configuration" />
4. Define Java classes and interfaces in src/main/java folder
a. Model – com.example.redis.model.User – This is a POJO that maps the user
registration HTML form submitted by the end user.
package com.example.redis.model;
import java.io.Serializable;
@SuppressWarnings("serial")
public class User implements Serializable {
private String fname;
private String lname;
private String email;
private String comments;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public User() {
}
}
b. Interface – com.example.redis.repository.IHashRepository<V> – This is
required to expose the CRUD operations with regards to Redis.
package com.example.redis.repository;
import java.util.List;
public interface IHashRepository<V> {
void put(V obj);
V get(String key);
void delete(String key);
List<V> getAll();
void delete();
}
c. Implementation Class –
com.example.redis.repository.impl.UserRepository – Implements the
Redis hash operations defined by IHashRepository<V> interface.
package com.example.redis.repository.impl;
import com.example.redis.model.User;
import com.example.redis.repository.IHashRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.*;
public class UserRepository implements IHashRepository<User> {
private static final String OBJECT_KEY = "USER:";
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Override
public void put(User user) {
redisTemplate.opsForHash().put(OBJECT_KEY,
user.getEmail(), user);
}
@Override
public User get(String key) {
Object user = redisTemplate.opsForHash().get(OBJECT_KEY,
key);
if (user == null)
return null;
return (User)user;
}
@Override
public void delete(String key) {
redisTemplate.opsForHash().delete(OBJECT_KEY,key);
}
@Override
public List<User> getAll() {
List<User> users = new ArrayList<User>();
Map<Object, Object> entries =
redisTemplate.opsForHash().entries(OBJECT_KEY);
if (entries.isEmpty())
return null;
Set<Object> keys = entries.keySet();
Iterator<Object> iterator = keys.iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
User user = (User) entries.get(key);
users.add(user);
}
return users;
}
@Override
public void delete() {
redisTemplate.delete(OBJECT_KEY);
}
}
5. Define Global Mule Configuration element for
com.example.redis.repository.impl.UserRepository.
<spring:bean id="userRepo" name="userRepo"
class="com.example.redis.repository.impl.UserRepository"
/>
6. In this step we will be creating mule flows. We need to represent each CRUD operation with
a mule flow.
a. userFormRedisFlow – The purpose of this flow is to provide the user registration HTML form
to the end user when invoked through the URL http://localhost:8081/.
The parse template transformer loads the following static HTML contents into the mule flow. In
this article it was named user-registration-form.html. This file needs to be kept in
src/main/resources folder.
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body style="margin: 30px;">
<form method="post" action="/store" onSubmit="return
validateForm();">
<table>
<tr>
<td>
<div style="padding-bottom: 18px;
font-size: 21px;">User
Registration</div>
<div style="display: flex;
padding-bottom: 18px; width: 450px;">
<div style="margin-left: %;
margin-right: 1%; width: 49%;">
First name<span
style="color: red;"> *</span><br> <input
type="text"
id="fname" name="fname" style="width: 100%;" />
</div>
<div style="margin-left:
1%; margin-right: 0; width: 49%;">
Last name<span
style="color: red;"> *</span><br> <input
type="text"
id="lname" name="lname" style="width: 100%;" />
</div>
</div>
<div style="padding-bottom:
18px;">
Email<span style="color:
red;"> *</span><br> <input
type="text"
id="email" name="email" style="width: 450px;" />
</div>
<div style="padding-bottom:
18px;">
Comments<br>
<textarea id="comments"
${readonly} name="comments"
style="width: 450px;"
rows="6" cols=""></textarea>
</div>
<div style="padding-bottom:
18px;">
<input name="skip_Submit"
value="Subscribe" type="submit" />
</div>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
function validateForm() {
if
(isEmpty(document.getElementById('fname').value.trim())) {
alert('First name is required!');
return false;
}
if
(isEmpty(document.getElementById('lname').value.trim())) {
alert('Last name is required!');
return false;
}
if
(isEmpty(document.getElementById('email').value.trim())) {
alert('Email is required!');
return false;
}
if
(!validateEmail(document.getElementById('email').value.trim()))
{
alert('Email must be a valid email
address!');
return false;
}
return true;
}
function isEmpty(str) {
return (str.length === 0 || !str.trim());
}
function validateEmail(email) {
var re = /^([w-]+(?:.[w-]+)*)@((?:[w-
]+.)*w[w-]{0,66}).([a-z]{2,15}(?:.[a-z]{2})?)$/i;
return isEmpty(email) || re.test(email);
}
</script>
</body>
</html>
Flow XML code:
<flow name="userFormRedisFlow">
<http:listener config-
ref="HTTP_Listener_Configuration"
path="/" allowedMethods="GET" doc:name="HTTP"
/>
<parse-template location="src/main/resources/user-
registration-form.html"
doc:name="Parse Template" />
</flow>
b. userRegistrationRedisFlow – The purpose of this flow is to process the HTML form data
posted by the end user via http://localhost:8081/ and creates a new user registration object
into Redis data store.
Flow XML code:
<flow name="userRegistrationRedisFlow">
<http:listener config-
ref="HTTP_Listener_Configuration"
path="/store" allowedMethods="POST"
doc:name="HTTP" />
<dw:transform-message metadata:id="f9f1be99-7322-
41e3-9a22-0e0b753768d5"
doc:name="Transform Message">
<dw:input-payload mimeType="application/java"
/>
<dw:input-inbound-property
propertyName="http.query.params" />
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
comments: payload.comments,
email: payload.email,
fname: payload.fname,
lname: payload.lname
} as :object {class:
"com.example.redis.model.User"}]]></dw:set-payload>
</dw:transform-message>
<set-variable variableName="flag"
value="#[app.registry.get(&quot;userRepo&quot;).get(paylo
ad.getEmail())]"
doc:name="Variable" />
<choice doc:name="Choice">
<when expression="#[flowVars.flag == null]">
<expression-component
doc:name="Expression"><![CDATA[app.registry.get("userRepo").put
(payload);
flowVars.flag =
app.registry.get("userRepo").get(payload.getEmail());]]></expre
ssion-component>
<choice doc:name="Choice">
<when expression="#[flowVars.flag
!= null]">
<json:object-to-json-
transformer
doc:name="Object to
JSON" />
<set-payload
value="#[&quot;n
Operation: Insertn&quot;] #[&quot;Payload: &quot; + payload +
&quot;n&quot;] #[&quot;Operation Status: Success! Record has
been added.&quot;]"
doc:name="Set
Payload" />
</when>
<otherwise>
<json:object-to-json-
transformer
doc:name="Object to
JSON" />
<set-payload
value="#[&quot;n
Operation: Insertn&quot;] #[&quot;Payload: &quot; + payload +
&quot;n&quot;] #[&quot;Operation Status: Fail! Record has not
been added.&quot;]"
doc:name="Set
Payload" />
</otherwise>
</choice>
</when>
<when expression="#[flowVars.flag != null]">
<set-payload value="#[flowVars.flag]"
doc:name="Set Payload" />
<json:object-to-json-transformer
doc:name="Object to JSON" />
<set-payload
value="#[&quot;n Operation:
Insertn&quot;] #[&quot;Payload: &quot; + payload +
&quot;n&quot;] #[&quot;Operation Status: User Already
Exist&quot;]"
doc:name="Set Payload" />
</when>
</choice>
</flow>
c. userQueryRedisFlow – The purpose of this flow is to query a single user registration object
from Redis data store. Use the URL http://localhost:8081/find?email=<email-id> to
invoke the query. Here, email is considered as the key for the object stored in the Redis store.
Flow XML code:
<flow name="userQueryRedisFlow">
<http:listener config-
ref="HTTP_Listener_Configuration"
path="/find" allowedMethods="GET"
doc:name="HTTP" />
<expression-component
doc:name="Expression"><![CDATA[payload=app.registry.get("userRe
po").get(message.inboundProperties.'http.query.params'.email);]
]></expression-component>
<choice doc:name="Choice">
<when expression="#[payload != null]">
<json:object-to-json-transformer
doc:name="Object to JSON" />
<set-payload
value="#[&quot;n Operation:
Queryn&quot;] #[&quot;Key: &quot; +
message.inboundProperties.'http.query.params'.email +
&quot;n&quot;] #[&quot;Result: &quot; + payload +
&quot;n&quot;]"
doc:name="Set Payload" />
</when>
<otherwise>
<set-payload
value="#[&quot;n Operation:
Queryn&quot;] #[&quot;Key: &quot; +
message.inboundProperties.'http.query.params'.email +
&quot;n&quot;] #[&quot;Result: Record Not Available&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</flow>
d. userQueryAllRedisFlow – The purpose of this flow is to query all user registration objects
stored in Redis data store. Use the URL http://localhost:8081/findAll to invoke the query to
retrieve all user registration objects.
Flow XML code:
<flow name="userQueryAllRedisFlow">
<http:listener config-
ref="HTTP_Listener_Configuration"
path="/findAll" allowedMethods="GET"
doc:name="HTTP" />
<expression-component
doc:name="Expression"><![CDATA[payload=app.registry
.get("userRepo").getAll();]]></expression-
component>
<choice doc:name="Choice">
<when expression="#[payload != null]">
<json:object-to-json-transformer
doc:name="Object to JSON" />
<set-payload
value="#[&quot;n Operation:
QueryAlln&quot;] #[&quot;Result: &quot; + payload + &quot;n&quot;]"
doc:name="Set Payload" />
</when>
<otherwise>
<set-payload
value="#[&quot;n Operation:
Queryn&quot;] #[&quot;Result: Record Not Available&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</flow>
e. userDeleteRedisFlow – The purpose of this flow is to delete a single user registration object
from the Redis data store. Use the URL http://localhost:8081/delete?email=<email-id> to
invoke the delete operation. Here, email is considered as the key for the object stored in the
Redis store.
Flow XML code:
<flow name="userDeleteRedisFlow">
<http:listener config-
ref="HTTP_Listener_Configuration"
path="/delete" allowedMethods="GET"
doc:name="HTTP" />
<set-variable variableName="flag"
value="#[app.registry.get(&quot;userRepo&quot;).get(messa
ge.inboundProperties.'http.query.params'.email)]"
doc:name="Variable" />
<choice doc:name="Choice">
<when expression="#[flowVars.flag != null]">
<expression-component
doc:name="Expression"><![CDATA[app.registry.get("userRepo").del
ete(message.inboundProperties.'http.query.params'.email);
payload=app.registry.get("userRepo").get(message.inboundPropert
ies.'http.query.params'.email);]]></expression-component>
<choice doc:name="Choice">
<when expression="#[payload ==
null]">
<set-payload
value="#[&quot;n
Operation: Deleten&quot;] #[&quot;Key: &quot; +
message.inboundProperties.'http.query.params'.email +
&quot;n&quot;] #[&quot;Result: Record Deleted&quot;]"
doc:name="Set
Payload" />
</when>
<otherwise>
<set-payload
value="#[&quot;n
Operation: Deleten&quot;] #[&quot;Key: &quot; +
message.inboundProperties.'http.query.params'.email +
&quot;n&quot;] #[&quot;Result: Opertion Unsuccessful&quot;]"
doc:name="Set
Payload" />
</otherwise>
</choice>
</when>
<otherwise>
<set-payload
value="#[&quot;n Operation:
Deleten&quot;] #[&quot;Key: &quot; +
message.inboundProperties.'http.query.params'.email +
&quot;n&quot;] #[&quot;Result: Record Not Available&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</flow>
f. userDeleteAllRedisFlow – The purpose of this flow is to delete all the user registration
objects from the Redis data store. Use the URL http://localhost:8081/deleteAll to invoke the
operation to delete all the objects from the Redis data store.
Flow XML code:
<flow name="userDeleteAllRedisFlow">
<http:listener config-
ref="HTTP_Listener_Configuration"
path="/deleteAll" allowedMethods="GET"
doc:name="HTTP" />
<set-variable variableName="flag"
value="#[app.registry.get(&quot;userRepo&quot;).getAll()]
" doc:name="Variable" />
<choice doc:name="Choice">
<when expression="#[flowVars.flag != null]">
<expression-component
doc:name="Expression"><![CDATA[app.registry.get("userRepo").del
ete();
payload=app.registry.get("userRepo").getAll();]]></expression-
component>
<choice doc:name="Choice">
<when expression="#[payload ==
null]">
<set-payload
value="#[&quot;n
Operation: DeleteAlln&quot;] #[&quot;Result: All Records
Deleted&quot;]"
doc:name="Set
Payload" />
</when>
<otherwise>
<set-payload
value="#[&quot;n
Operation: DeleteAlln&quot;] #[&quot;Result:
Unsuccessful&quot;]"
doc:name="Set
Payload" />
</otherwise>
</choice>
</when>
<otherwise>
<set-payload
value="#[&quot;n Operation:
DeleteAlln&quot;] #[&quot;Result: Records Not
Available&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</flow>
Run and Test the application
In this section we will be testing each flow that represent the respected Redis Hash operation.
1. Start Redis service if not started.
2. Select Mule Application with Maven option from Run As context menu to run the mule
application.
3. Open any browser and type the URL http://localhost:8081/ to obtain the User Registration
HTML form. Fill the details and submit the form.
On submission, the user registration details are processed by userRegistrationRedisFlow flow
and stored in the Redis data store as a POJO. The following acknowledgement is sent back to
the end user after completion of the registration process indicating successful insertion of a
record in Redis data store.
4. Now, let’s query the record which was inserted in the earlier step. Type the URL
http://localhost:8081/find?email=pankaj.yadav@abc.com in the address bar. The query
result is shown in the screen shot below.
5. Now, let’s delete the record. Type the URL
http://localhost:8081/delete?email=pankaj.yadav@abc.com in the address bar. The following
acknowledgement screen must be displayed if the record is deleted successfully.
Now, let’s try to double check whether the record is deleted. This can be done in two ways:
query the deleted record again and execute the delete operation again.
Type the query operation URL http://localhost:8081/find?email=pankaj.yadav@abc.com
again. The following acknowledgement screen indicates the record has been deleted.
Type the delete operation URL http://localhost:8081/delete?email=pankaj.yadav@abc.com
again. The following acknowledgement screen indicates that the record has been deleted.
6. Now, let’s insert few more records into Redis data store and test to retrieve all the records.
Type the URL http://localhost:8081/findAll in the address bar to retrieve all the records from
the Redis data store, as shown below.
7. Now, type the URL http://localhost:8081/deleteAll in the address bar to delete all the
record.
Let’s assure ourselves whether all the records are deleted by retrieving the records by the
URL http://localhost:8081/findAll. The acknowledgement shown in the below screen shot
assures that the records were deleted.
Full Application XML Code
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:redis="http://www.mulesoft.org/schema/mule/redis"
xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
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/ee/dw
http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/json
http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/redis
http://www.mulesoft.org/schema/mule/redis/current/mule-redis.xsd">
<http:listener-config name="HTTP_Listener_Configuration"
host="0.0.0.0" port="8081" doc:name="HTTP Listener
Configuration" />
<spring:beans>
<spring:bean id="jedisConnectionFactory"
name="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectio
nFactory">
</spring:bean>
<spring:bean id="redisTemplate" name="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate">
<spring:property name="connectionFactory"
ref="jedisConnectionFactory" />
<spring:property name="valueSerializer">
<spring:bean
class="org.springframework.data.redis.serializer.JacksonJsonRedisSeri
alizer">
<spring:constructor-arg
type="java.lang.Class"
value="java.lang.Object" />
</spring:bean>
</spring:property>
</spring:bean>
<spring:bean id="userRepo" name="userRepo"
class="com.example.redis.repository.impl.UserRepository"
/>
</spring:beans>
<flow name="userFormRedisFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/" allowedMethods="GET" doc:name="HTTP" />
<parse-template location="src/main/resources/user-registration-
form.html"
doc:name="Parse Template" />
</flow>
<flow name="userRegistrationRedisFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/store" allowedMethods="POST" doc:name="HTTP" />
<dw:transform-message metadata:id="f9f1be99-7322-41e3-9a22-
0e0b753768d5"
doc:name="Transform Message">
<dw:input-payload mimeType="application/java" />
<dw:input-inbound-property
propertyName="http.query.params" />
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
comments: payload.comments,
email: payload.email,
fname: payload.fname,
lname: payload.lname
} as :object {class: "com.example.redis.model.User"}]]></dw:set-payload>
</dw:transform-message>
<set-variable variableName="flag"
value="#[app.registry.get(&quot;userRepo&quot;).get(payload.getEmail(
))]"
doc:name="Variable" />
<choice doc:name="Choice">
<when expression="#[flowVars.flag == null]">
<expression-component
doc:name="Expression"><![CDATA[app.registry.get("userRepo").put(payload);
flowVars.flag =
app.registry.get("userRepo").get(payload.getEmail());]]></expression-
component>
<choice doc:name="Choice">
<when expression="#[flowVars.flag != null]">
<json:object-to-json-transformer
doc:name="Object to JSON" />
<set-payload
value="#[&quot;n Operation:
Insertn&quot;] #[&quot;Payload: &quot; + payload + &quot;n&quot;]
#[&quot;Operation Status: Success! Record has been added.&quot;]"
doc:name="Set Payload" />
</when>
<otherwise>
<json:object-to-json-transformer
doc:name="Object to JSON" />
<set-payload
value="#[&quot;n Operation:
Insertn&quot;] #[&quot;Payload: &quot; + payload + &quot;n&quot;]
#[&quot;Operation Status: Fail! Record has not been added.&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</when>
<when expression="#[flowVars.flag != null]">
<set-payload value="#[flowVars.flag]" doc:name="Set
Payload" />
<json:object-to-json-transformer
doc:name="Object to JSON" />
<set-payload
value="#[&quot;n Operation: Insertn&quot;]
#[&quot;Payload: &quot; + payload + &quot;n&quot;] #[&quot;Operation
Status: User Already Exist&quot;]"
doc:name="Set Payload" />
</when>
</choice>
</flow>
<flow name="userQueryRedisFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/find" allowedMethods="GET" doc:name="HTTP" />
<expression-component
doc:name="Expression"><![CDATA[payload=app.registry.get("userRepo").get(mes
sage.inboundProperties.'http.query.params'.email);]]></expression-
component>
<choice doc:name="Choice">
<when expression="#[payload != null]">
<json:object-to-json-transformer
doc:name="Object to JSON" />
<set-payload
value="#[&quot;n Operation: Queryn&quot;]
#[&quot;Key: &quot; + message.inboundProperties.'http.query.params'.email +
&quot;n&quot;] #[&quot;Result: &quot; + payload + &quot;n&quot;]"
doc:name="Set Payload" />
</when>
<otherwise>
<set-payload
value="#[&quot;n Operation: Queryn&quot;]
#[&quot;Key: &quot; + message.inboundProperties.'http.query.params'.email +
&quot;n&quot;] #[&quot;Result: Record Not Available&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</flow>
<flow name="userDeleteRedisFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/delete" allowedMethods="GET" doc:name="HTTP" />
<set-variable variableName="flag"
value="#[app.registry.get(&quot;userRepo&quot;).get(message.inboundPr
operties.'http.query.params'.email)]"
doc:name="Variable" />
<choice doc:name="Choice">
<when expression="#[flowVars.flag != null]">
<expression-component
doc:name="Expression"><![CDATA[app.registry.get("userRepo").delete(message.
inboundProperties.'http.query.params'.email);
payload=app.registry.get("userRepo").get(message.inboundProperties.'http.qu
ery.params'.email);]]></expression-component>
<choice doc:name="Choice">
<when expression="#[payload == null]">
<set-payload
value="#[&quot;n Operation:
Deleten&quot;] #[&quot;Key: &quot; +
message.inboundProperties.'http.query.params'.email + &quot;n&quot;]
#[&quot;Result: Record Deleted&quot;]"
doc:name="Set Payload" />
</when>
<otherwise>
<set-payload
value="#[&quot;n Operation:
Deleten&quot;] #[&quot;Key: &quot; +
message.inboundProperties.'http.query.params'.email + &quot;n&quot;]
#[&quot;Result: Opertion Unsuccessful&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</when>
<otherwise>
<set-payload
value="#[&quot;n Operation: Deleten&quot;]
#[&quot;Key: &quot; + message.inboundProperties.'http.query.params'.email +
&quot;n&quot;] #[&quot;Result: Record Not Available&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</flow>
<flow name="userQueryAllRedisFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/findAll" allowedMethods="GET" doc:name="HTTP" />
<expression-component
doc:name="Expression"><![CDATA[payload=app.registry.get("userRepo").getAll(
);]]></expression-component>
<choice doc:name="Choice">
<when expression="#[payload != null]">
<json:object-to-json-transformer
doc:name="Object to JSON" />
<set-payload
value="#[&quot;n Operation:
QueryAlln&quot;] #[&quot;Result: &quot; + payload + &quot;n&quot;]"
doc:name="Set Payload" />
</when>
<otherwise>
<set-payload
value="#[&quot;n Operation: Queryn&quot;]
#[&quot;Result: Record Not Available&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</flow>
<flow name="userDeleteAllRedisFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/deleteAll" allowedMethods="GET" doc:name="HTTP" />
<set-variable variableName="flag"
value="#[app.registry.get(&quot;userRepo&quot;).getAll()]"
doc:name="Variable" />
<choice doc:name="Choice">
<when expression="#[flowVars.flag != null]">
<expression-component
doc:name="Expression"><![CDATA[app.registry.get("userRepo").delete();
payload=app.registry.get("userRepo").getAll();]]></expression-component>
<choice doc:name="Choice">
<when expression="#[payload == null]">
<set-payload
value="#[&quot;n Operation:
DeleteAlln&quot;] #[&quot;Result: All Records Deleted&quot;]"
doc:name="Set Payload" />
</when>
<otherwise>
<set-payload
value="#[&quot;n Operation:
DeleteAlln&quot;] #[&quot;Result: Unsuccessful&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</when>
<otherwise>
<set-payload
value="#[&quot;n Operation:
DeleteAlln&quot;] #[&quot;Result: Records Not Available&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</flow>
</mule>
Conclusion
In this article I have tried to show you how we can integrate Redis and Mule with Spring Data Redis
framework and explored few Redis Hash operations. Alternatively, we can integrate Mule and Redis
using Mule Redis connector, as well.

How to connect redis and mule esb using spring data redis module

  • 1.
    How To ConnectRedis and Mule ESB Using Spring Data Redis Module In this article I am going to show you to connect Redis key-value data store and Mule ESB using Spring Data Redis Module. Pre-requisite  Anypoint Studio 6 or above  Mule ESB 3.8 or above  Redis 3.2 - https://github.com/MSOpenTech/redis/releases/download/win-3.2.100/Redis- x64-3.2.100.msi  Apache Maven 3.3.9 - http://mirror.fibergrid.in/apache/maven/maven- 3/3.3.9/binaries/apache-maven-3.3.9-bin.zip  Spring Data Redis 1.7.3  Jedis 2.9.0 – Java driver for Redis. Use Case Let’s take a use case where the end user is provided with a user registration HTML form. The submitted user registration details are then transformed into a POJO and subsequently stored in REDIS data store. Apart from storing object in Redis, the use case deals with query and delete operations as well. The scope of this use case is limited to Redis Hash operations only. Solution 1. Create a Maven based Mule project with Anypoint Studio. In the article it was named integrating_with_redis. 2. Add the following dependencies in pom.xml: <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.3.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> Note: The above dependencies must be added at the top of the dependency list to avoid version conflict with regards to Spring framework. 3. Define Global Elements for Spring Redis Configuration and HTTP Listener.
  • 2.
    a. JedisConnectionFactory –is required to create and configure JedisConnectionFactory by which we should be able to connect to Redis. <spring:bean id="jedisConnectionFactory" name="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectio nFactory"> </spring:bean> b. RedisTemplate – is required to be configured so as to read/write data to and from Redis. <spring:bean id="redisTemplate" name="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <spring:property name="connectionFactory" ref="jedisConnectionFactory" /> <spring:property name="valueSerializer"> <spring:bean class="org.springframework.data.redis.serializer.JacksonJsonRedisSeri alizer"> <spring:constructor-arg type="java.lang.Class" value="java.lang.Object" /> </spring:bean> </spring:property> </spring:bean> c. HTTP Listener – is required to be configured for listening to HTTP requests. <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration" /> 4. Define Java classes and interfaces in src/main/java folder a. Model – com.example.redis.model.User – This is a POJO that maps the user registration HTML form submitted by the end user. package com.example.redis.model; import java.io.Serializable; @SuppressWarnings("serial") public class User implements Serializable { private String fname; private String lname; private String email; private String comments; public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; }
  • 3.
    public String getLname(){ return lname; } public void setLname(String lname) { this.lname = lname; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getComments() { return comments; } public void setComments(String comments) { this.comments = comments; } public User() { } } b. Interface – com.example.redis.repository.IHashRepository<V> – This is required to expose the CRUD operations with regards to Redis. package com.example.redis.repository; import java.util.List; public interface IHashRepository<V> { void put(V obj); V get(String key); void delete(String key); List<V> getAll(); void delete(); } c. Implementation Class – com.example.redis.repository.impl.UserRepository – Implements the Redis hash operations defined by IHashRepository<V> interface. package com.example.redis.repository.impl; import com.example.redis.model.User; import com.example.redis.repository.IHashRepository;
  • 4.
    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; importjava.util.*; public class UserRepository implements IHashRepository<User> { private static final String OBJECT_KEY = "USER:"; @Autowired private RedisTemplate<String,Object> redisTemplate; @Override public void put(User user) { redisTemplate.opsForHash().put(OBJECT_KEY, user.getEmail(), user); } @Override public User get(String key) { Object user = redisTemplate.opsForHash().get(OBJECT_KEY, key); if (user == null) return null; return (User)user; } @Override public void delete(String key) { redisTemplate.opsForHash().delete(OBJECT_KEY,key); } @Override public List<User> getAll() { List<User> users = new ArrayList<User>(); Map<Object, Object> entries = redisTemplate.opsForHash().entries(OBJECT_KEY); if (entries.isEmpty()) return null; Set<Object> keys = entries.keySet(); Iterator<Object> iterator = keys.iterator(); while (iterator.hasNext()) { String key = iterator.next().toString(); User user = (User) entries.get(key); users.add(user); } return users; } @Override public void delete() { redisTemplate.delete(OBJECT_KEY); } }
  • 5.
    5. Define GlobalMule Configuration element for com.example.redis.repository.impl.UserRepository. <spring:bean id="userRepo" name="userRepo" class="com.example.redis.repository.impl.UserRepository" /> 6. In this step we will be creating mule flows. We need to represent each CRUD operation with a mule flow. a. userFormRedisFlow – The purpose of this flow is to provide the user registration HTML form to the end user when invoked through the URL http://localhost:8081/. The parse template transformer loads the following static HTML contents into the mule flow. In this article it was named user-registration-form.html. This file needs to be kept in src/main/resources folder. <!DOCTYPE html> <html lang="en"> <head> </head> <body style="margin: 30px;"> <form method="post" action="/store" onSubmit="return validateForm();"> <table> <tr> <td> <div style="padding-bottom: 18px; font-size: 21px;">User Registration</div> <div style="display: flex; padding-bottom: 18px; width: 450px;"> <div style="margin-left: %; margin-right: 1%; width: 49%;"> First name<span style="color: red;"> *</span><br> <input type="text" id="fname" name="fname" style="width: 100%;" /> </div> <div style="margin-left: 1%; margin-right: 0; width: 49%;"> Last name<span style="color: red;"> *</span><br> <input type="text" id="lname" name="lname" style="width: 100%;" /> </div>
  • 6.
    </div> <div style="padding-bottom: 18px;"> Email<span style="color: red;">*</span><br> <input type="text" id="email" name="email" style="width: 450px;" /> </div> <div style="padding-bottom: 18px;"> Comments<br> <textarea id="comments" ${readonly} name="comments" style="width: 450px;" rows="6" cols=""></textarea> </div> <div style="padding-bottom: 18px;"> <input name="skip_Submit" value="Subscribe" type="submit" /> </div> </td> </tr> </table> </form> <script type="text/javascript"> function validateForm() { if (isEmpty(document.getElementById('fname').value.trim())) { alert('First name is required!'); return false; } if (isEmpty(document.getElementById('lname').value.trim())) { alert('Last name is required!'); return false; } if (isEmpty(document.getElementById('email').value.trim())) { alert('Email is required!'); return false; } if (!validateEmail(document.getElementById('email').value.trim())) { alert('Email must be a valid email address!'); return false; } return true; } function isEmpty(str) { return (str.length === 0 || !str.trim()); } function validateEmail(email) { var re = /^([w-]+(?:.[w-]+)*)@((?:[w- ]+.)*w[w-]{0,66}).([a-z]{2,15}(?:.[a-z]{2})?)$/i; return isEmpty(email) || re.test(email); } </script>
  • 7.
    </body> </html> Flow XML code: <flowname="userFormRedisFlow"> <http:listener config- ref="HTTP_Listener_Configuration" path="/" allowedMethods="GET" doc:name="HTTP" /> <parse-template location="src/main/resources/user- registration-form.html" doc:name="Parse Template" /> </flow> b. userRegistrationRedisFlow – The purpose of this flow is to process the HTML form data posted by the end user via http://localhost:8081/ and creates a new user registration object into Redis data store. Flow XML code: <flow name="userRegistrationRedisFlow"> <http:listener config- ref="HTTP_Listener_Configuration" path="/store" allowedMethods="POST" doc:name="HTTP" /> <dw:transform-message metadata:id="f9f1be99-7322- 41e3-9a22-0e0b753768d5" doc:name="Transform Message"> <dw:input-payload mimeType="application/java" /> <dw:input-inbound-property propertyName="http.query.params" /> <dw:set-payload><![CDATA[%dw 1.0 %output application/java ---
  • 8.
    { comments: payload.comments, email: payload.email, fname:payload.fname, lname: payload.lname } as :object {class: "com.example.redis.model.User"}]]></dw:set-payload> </dw:transform-message> <set-variable variableName="flag" value="#[app.registry.get(&quot;userRepo&quot;).get(paylo ad.getEmail())]" doc:name="Variable" /> <choice doc:name="Choice"> <when expression="#[flowVars.flag == null]"> <expression-component doc:name="Expression"><![CDATA[app.registry.get("userRepo").put (payload); flowVars.flag = app.registry.get("userRepo").get(payload.getEmail());]]></expre ssion-component> <choice doc:name="Choice"> <when expression="#[flowVars.flag != null]"> <json:object-to-json- transformer doc:name="Object to JSON" /> <set-payload value="#[&quot;n Operation: Insertn&quot;] #[&quot;Payload: &quot; + payload + &quot;n&quot;] #[&quot;Operation Status: Success! Record has been added.&quot;]" doc:name="Set Payload" /> </when> <otherwise> <json:object-to-json- transformer doc:name="Object to JSON" /> <set-payload value="#[&quot;n Operation: Insertn&quot;] #[&quot;Payload: &quot; + payload + &quot;n&quot;] #[&quot;Operation Status: Fail! Record has not been added.&quot;]" doc:name="Set Payload" /> </otherwise> </choice> </when> <when expression="#[flowVars.flag != null]"> <set-payload value="#[flowVars.flag]" doc:name="Set Payload" /> <json:object-to-json-transformer doc:name="Object to JSON" /> <set-payload value="#[&quot;n Operation: Insertn&quot;] #[&quot;Payload: &quot; + payload + &quot;n&quot;] #[&quot;Operation Status: User Already Exist&quot;]"
  • 9.
    doc:name="Set Payload" /> </when> </choice> </flow> c.userQueryRedisFlow – The purpose of this flow is to query a single user registration object from Redis data store. Use the URL http://localhost:8081/find?email=<email-id> to invoke the query. Here, email is considered as the key for the object stored in the Redis store. Flow XML code: <flow name="userQueryRedisFlow"> <http:listener config- ref="HTTP_Listener_Configuration" path="/find" allowedMethods="GET" doc:name="HTTP" /> <expression-component doc:name="Expression"><![CDATA[payload=app.registry.get("userRe po").get(message.inboundProperties.'http.query.params'.email);] ]></expression-component> <choice doc:name="Choice"> <when expression="#[payload != null]"> <json:object-to-json-transformer doc:name="Object to JSON" /> <set-payload value="#[&quot;n Operation: Queryn&quot;] #[&quot;Key: &quot; + message.inboundProperties.'http.query.params'.email + &quot;n&quot;] #[&quot;Result: &quot; + payload + &quot;n&quot;]" doc:name="Set Payload" /> </when> <otherwise> <set-payload value="#[&quot;n Operation: Queryn&quot;] #[&quot;Key: &quot; + message.inboundProperties.'http.query.params'.email + &quot;n&quot;] #[&quot;Result: Record Not Available&quot;]" doc:name="Set Payload" /> </otherwise> </choice> </flow>
  • 10.
    d. userQueryAllRedisFlow –The purpose of this flow is to query all user registration objects stored in Redis data store. Use the URL http://localhost:8081/findAll to invoke the query to retrieve all user registration objects. Flow XML code: <flow name="userQueryAllRedisFlow"> <http:listener config- ref="HTTP_Listener_Configuration" path="/findAll" allowedMethods="GET" doc:name="HTTP" /> <expression-component doc:name="Expression"><![CDATA[payload=app.registry .get("userRepo").getAll();]]></expression- component> <choice doc:name="Choice"> <when expression="#[payload != null]"> <json:object-to-json-transformer doc:name="Object to JSON" /> <set-payload value="#[&quot;n Operation: QueryAlln&quot;] #[&quot;Result: &quot; + payload + &quot;n&quot;]" doc:name="Set Payload" /> </when> <otherwise> <set-payload value="#[&quot;n Operation: Queryn&quot;] #[&quot;Result: Record Not Available&quot;]" doc:name="Set Payload" /> </otherwise> </choice> </flow> e. userDeleteRedisFlow – The purpose of this flow is to delete a single user registration object from the Redis data store. Use the URL http://localhost:8081/delete?email=<email-id> to invoke the delete operation. Here, email is considered as the key for the object stored in the Redis store.
  • 11.
    Flow XML code: <flowname="userDeleteRedisFlow"> <http:listener config- ref="HTTP_Listener_Configuration" path="/delete" allowedMethods="GET" doc:name="HTTP" /> <set-variable variableName="flag" value="#[app.registry.get(&quot;userRepo&quot;).get(messa ge.inboundProperties.'http.query.params'.email)]" doc:name="Variable" /> <choice doc:name="Choice"> <when expression="#[flowVars.flag != null]"> <expression-component doc:name="Expression"><![CDATA[app.registry.get("userRepo").del ete(message.inboundProperties.'http.query.params'.email); payload=app.registry.get("userRepo").get(message.inboundPropert ies.'http.query.params'.email);]]></expression-component> <choice doc:name="Choice"> <when expression="#[payload == null]"> <set-payload value="#[&quot;n Operation: Deleten&quot;] #[&quot;Key: &quot; + message.inboundProperties.'http.query.params'.email + &quot;n&quot;] #[&quot;Result: Record Deleted&quot;]" doc:name="Set Payload" /> </when> <otherwise> <set-payload value="#[&quot;n Operation: Deleten&quot;] #[&quot;Key: &quot; + message.inboundProperties.'http.query.params'.email + &quot;n&quot;] #[&quot;Result: Opertion Unsuccessful&quot;]" doc:name="Set Payload" /> </otherwise> </choice>
  • 12.
    </when> <otherwise> <set-payload value="#[&quot;n Operation: Deleten&quot;] #[&quot;Key:&quot; + message.inboundProperties.'http.query.params'.email + &quot;n&quot;] #[&quot;Result: Record Not Available&quot;]" doc:name="Set Payload" /> </otherwise> </choice> </flow> f. userDeleteAllRedisFlow – The purpose of this flow is to delete all the user registration objects from the Redis data store. Use the URL http://localhost:8081/deleteAll to invoke the operation to delete all the objects from the Redis data store. Flow XML code: <flow name="userDeleteAllRedisFlow"> <http:listener config- ref="HTTP_Listener_Configuration" path="/deleteAll" allowedMethods="GET" doc:name="HTTP" /> <set-variable variableName="flag" value="#[app.registry.get(&quot;userRepo&quot;).getAll()] " doc:name="Variable" /> <choice doc:name="Choice"> <when expression="#[flowVars.flag != null]"> <expression-component doc:name="Expression"><![CDATA[app.registry.get("userRepo").del ete(); payload=app.registry.get("userRepo").getAll();]]></expression- component> <choice doc:name="Choice"> <when expression="#[payload == null]"> <set-payload
  • 13.
    value="#[&quot;n Operation: DeleteAlln&quot;] #[&quot;Result:All Records Deleted&quot;]" doc:name="Set Payload" /> </when> <otherwise> <set-payload value="#[&quot;n Operation: DeleteAlln&quot;] #[&quot;Result: Unsuccessful&quot;]" doc:name="Set Payload" /> </otherwise> </choice> </when> <otherwise> <set-payload value="#[&quot;n Operation: DeleteAlln&quot;] #[&quot;Result: Records Not Available&quot;]" doc:name="Set Payload" /> </otherwise> </choice> </flow> Run and Test the application In this section we will be testing each flow that represent the respected Redis Hash operation. 1. Start Redis service if not started. 2. Select Mule Application with Maven option from Run As context menu to run the mule application. 3. Open any browser and type the URL http://localhost:8081/ to obtain the User Registration HTML form. Fill the details and submit the form.
  • 14.
    On submission, theuser registration details are processed by userRegistrationRedisFlow flow and stored in the Redis data store as a POJO. The following acknowledgement is sent back to the end user after completion of the registration process indicating successful insertion of a record in Redis data store. 4. Now, let’s query the record which was inserted in the earlier step. Type the URL http://localhost:8081/find?email=pankaj.yadav@abc.com in the address bar. The query result is shown in the screen shot below.
  • 15.
    5. Now, let’sdelete the record. Type the URL http://localhost:8081/delete?email=pankaj.yadav@abc.com in the address bar. The following acknowledgement screen must be displayed if the record is deleted successfully. Now, let’s try to double check whether the record is deleted. This can be done in two ways: query the deleted record again and execute the delete operation again. Type the query operation URL http://localhost:8081/find?email=pankaj.yadav@abc.com again. The following acknowledgement screen indicates the record has been deleted. Type the delete operation URL http://localhost:8081/delete?email=pankaj.yadav@abc.com again. The following acknowledgement screen indicates that the record has been deleted.
  • 16.
    6. Now, let’sinsert few more records into Redis data store and test to retrieve all the records.
  • 19.
    Type the URLhttp://localhost:8081/findAll in the address bar to retrieve all the records from the Redis data store, as shown below. 7. Now, type the URL http://localhost:8081/deleteAll in the address bar to delete all the record.
  • 20.
    Let’s assure ourselveswhether all the records are deleted by retrieving the records by the URL http://localhost:8081/findAll. The acknowledgement shown in the below screen shot assures that the records were deleted. Full Application XML Code <?xml version="1.0" encoding="UTF-8"?> <mule xmlns:redis="http://www.mulesoft.org/schema/mule/redis" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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
  • 21.
    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/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd http://www.mulesoft.org/schema/mule/redis http://www.mulesoft.org/schema/mule/redis/current/mule-redis.xsd"> <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081"doc:name="HTTP Listener Configuration" /> <spring:beans> <spring:bean id="jedisConnectionFactory" name="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectio nFactory"> </spring:bean> <spring:bean id="redisTemplate" name="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <spring:property name="connectionFactory" ref="jedisConnectionFactory" /> <spring:property name="valueSerializer"> <spring:bean class="org.springframework.data.redis.serializer.JacksonJsonRedisSeri alizer"> <spring:constructor-arg type="java.lang.Class" value="java.lang.Object" /> </spring:bean> </spring:property> </spring:bean> <spring:bean id="userRepo" name="userRepo" class="com.example.redis.repository.impl.UserRepository" /> </spring:beans> <flow name="userFormRedisFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/" allowedMethods="GET" doc:name="HTTP" /> <parse-template location="src/main/resources/user-registration- form.html" doc:name="Parse Template" /> </flow> <flow name="userRegistrationRedisFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/store" allowedMethods="POST" doc:name="HTTP" /> <dw:transform-message metadata:id="f9f1be99-7322-41e3-9a22- 0e0b753768d5" doc:name="Transform Message"> <dw:input-payload mimeType="application/java" /> <dw:input-inbound-property propertyName="http.query.params" /> <dw:set-payload><![CDATA[%dw 1.0 %output application/java
  • 22.
    --- { comments: payload.comments, email: payload.email, fname:payload.fname, lname: payload.lname } as :object {class: "com.example.redis.model.User"}]]></dw:set-payload> </dw:transform-message> <set-variable variableName="flag" value="#[app.registry.get(&quot;userRepo&quot;).get(payload.getEmail( ))]" doc:name="Variable" /> <choice doc:name="Choice"> <when expression="#[flowVars.flag == null]"> <expression-component doc:name="Expression"><![CDATA[app.registry.get("userRepo").put(payload); flowVars.flag = app.registry.get("userRepo").get(payload.getEmail());]]></expression- component> <choice doc:name="Choice"> <when expression="#[flowVars.flag != null]"> <json:object-to-json-transformer doc:name="Object to JSON" /> <set-payload value="#[&quot;n Operation: Insertn&quot;] #[&quot;Payload: &quot; + payload + &quot;n&quot;] #[&quot;Operation Status: Success! Record has been added.&quot;]" doc:name="Set Payload" /> </when> <otherwise> <json:object-to-json-transformer doc:name="Object to JSON" /> <set-payload value="#[&quot;n Operation: Insertn&quot;] #[&quot;Payload: &quot; + payload + &quot;n&quot;] #[&quot;Operation Status: Fail! Record has not been added.&quot;]" doc:name="Set Payload" /> </otherwise> </choice> </when> <when expression="#[flowVars.flag != null]"> <set-payload value="#[flowVars.flag]" doc:name="Set Payload" /> <json:object-to-json-transformer doc:name="Object to JSON" /> <set-payload value="#[&quot;n Operation: Insertn&quot;] #[&quot;Payload: &quot; + payload + &quot;n&quot;] #[&quot;Operation Status: User Already Exist&quot;]" doc:name="Set Payload" /> </when> </choice> </flow> <flow name="userQueryRedisFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/find" allowedMethods="GET" doc:name="HTTP" /> <expression-component doc:name="Expression"><![CDATA[payload=app.registry.get("userRepo").get(mes
  • 23.
    sage.inboundProperties.'http.query.params'.email);]]></expression- component> <choice doc:name="Choice"> <when expression="#[payload!= null]"> <json:object-to-json-transformer doc:name="Object to JSON" /> <set-payload value="#[&quot;n Operation: Queryn&quot;] #[&quot;Key: &quot; + message.inboundProperties.'http.query.params'.email + &quot;n&quot;] #[&quot;Result: &quot; + payload + &quot;n&quot;]" doc:name="Set Payload" /> </when> <otherwise> <set-payload value="#[&quot;n Operation: Queryn&quot;] #[&quot;Key: &quot; + message.inboundProperties.'http.query.params'.email + &quot;n&quot;] #[&quot;Result: Record Not Available&quot;]" doc:name="Set Payload" /> </otherwise> </choice> </flow> <flow name="userDeleteRedisFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/delete" allowedMethods="GET" doc:name="HTTP" /> <set-variable variableName="flag" value="#[app.registry.get(&quot;userRepo&quot;).get(message.inboundPr operties.'http.query.params'.email)]" doc:name="Variable" /> <choice doc:name="Choice"> <when expression="#[flowVars.flag != null]"> <expression-component doc:name="Expression"><![CDATA[app.registry.get("userRepo").delete(message. inboundProperties.'http.query.params'.email); payload=app.registry.get("userRepo").get(message.inboundProperties.'http.qu ery.params'.email);]]></expression-component> <choice doc:name="Choice"> <when expression="#[payload == null]"> <set-payload value="#[&quot;n Operation: Deleten&quot;] #[&quot;Key: &quot; + message.inboundProperties.'http.query.params'.email + &quot;n&quot;] #[&quot;Result: Record Deleted&quot;]" doc:name="Set Payload" /> </when> <otherwise> <set-payload value="#[&quot;n Operation: Deleten&quot;] #[&quot;Key: &quot; + message.inboundProperties.'http.query.params'.email + &quot;n&quot;] #[&quot;Result: Opertion Unsuccessful&quot;]" doc:name="Set Payload" /> </otherwise> </choice> </when> <otherwise> <set-payload value="#[&quot;n Operation: Deleten&quot;] #[&quot;Key: &quot; + message.inboundProperties.'http.query.params'.email + &quot;n&quot;] #[&quot;Result: Record Not Available&quot;]"
  • 24.
    doc:name="Set Payload" /> </otherwise> </choice> </flow> <flowname="userQueryAllRedisFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/findAll" allowedMethods="GET" doc:name="HTTP" /> <expression-component doc:name="Expression"><![CDATA[payload=app.registry.get("userRepo").getAll( );]]></expression-component> <choice doc:name="Choice"> <when expression="#[payload != null]"> <json:object-to-json-transformer doc:name="Object to JSON" /> <set-payload value="#[&quot;n Operation: QueryAlln&quot;] #[&quot;Result: &quot; + payload + &quot;n&quot;]" doc:name="Set Payload" /> </when> <otherwise> <set-payload value="#[&quot;n Operation: Queryn&quot;] #[&quot;Result: Record Not Available&quot;]" doc:name="Set Payload" /> </otherwise> </choice> </flow> <flow name="userDeleteAllRedisFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/deleteAll" allowedMethods="GET" doc:name="HTTP" /> <set-variable variableName="flag" value="#[app.registry.get(&quot;userRepo&quot;).getAll()]" doc:name="Variable" /> <choice doc:name="Choice"> <when expression="#[flowVars.flag != null]"> <expression-component doc:name="Expression"><![CDATA[app.registry.get("userRepo").delete(); payload=app.registry.get("userRepo").getAll();]]></expression-component> <choice doc:name="Choice"> <when expression="#[payload == null]"> <set-payload value="#[&quot;n Operation: DeleteAlln&quot;] #[&quot;Result: All Records Deleted&quot;]" doc:name="Set Payload" /> </when> <otherwise> <set-payload value="#[&quot;n Operation: DeleteAlln&quot;] #[&quot;Result: Unsuccessful&quot;]" doc:name="Set Payload" /> </otherwise> </choice> </when> <otherwise> <set-payload value="#[&quot;n Operation: DeleteAlln&quot;] #[&quot;Result: Records Not Available&quot;]" doc:name="Set Payload" /> </otherwise> </choice>
  • 25.
    </flow> </mule> Conclusion In this articleI have tried to show you how we can integrate Redis and Mule with Spring Data Redis framework and explored few Redis Hash operations. Alternatively, we can integrate Mule and Redis using Mule Redis connector, as well.