Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Connect Redis and Mule ESB Using Spring Data Redis Module

DZone's Guide to

How to Connect Redis and Mule ESB Using Spring Data Redis Module

We can integrate Redis and Mule with either the Spring Data Redis framework or the Mule Redis connector.

Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

In this article, I am going to show you to connect the Redis key-value data store and Mule ESB using Spring Data Redis Module.

Prerequisites

  • Anypoint Studio 6 or above.
  • Mule ESB 3.8 or above.
  • Redis 3.2.
  • Apache Maven 3.3.9.
  • 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 the REDIS data store. Apart from storing objects 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

  • Create a Maven-based Mule project with Anypoint Studio. In the article, it was named integrating_with_redis.
  • 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.

Define the global elements for Spring Redis configuration and HTTP listener.

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.JedisConnectionFactory">
</spring:bean>

RedisTemplate is required to be configured so as to read and 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.JacksonJsonRedisSerializer">
<spring:constructor-arg type="java.lang.Class"
value="java.lang.Object" />
</spring:bean>
</spring:property>
</spring:bean>

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" />

Define Java classes and interfaces in the src/main/java folder. 

Model: com.example.redis.model.User 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() {

    }

}

Interface: com.example.redis.repository.IHashRepository 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();
}

Implementation Class: com.example.redis.repository.impl.UserRepository implements the Redis hash operations defined by IHashRepository 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);
    }
}

Define the Global Mule Configuration element for com.example.redis.repository.impl.UserRepository.

<spring:bean id="userRepo" name="userRepo"
class="com.example.redis.repository.impl.UserRepository" />

In this step, we will be creating Mule flows. We need to represent each CRUD operation with a mule flow.  

userFormRedisFlow provides the user registration HTML form to the end user when invoked through the URL http://localhost:8081/.

userFormRedisFlow

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 the 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>

userRegistrationRedisFlow processes the HTML form data posted by the end user via http://localhost:8081/ and creates a new user registration object into Redis data store.

userRegisterationRedisFlow

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(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: Insert\n&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: Insert\n&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: Insert\n&quot;] #[&quot;Payload: &quot; + payload + &quot;\n&quot;] #[&quot;Operation Status: User Already Exist&quot;]"
doc:name="Set Payload" />
</when>
</choice>
</flow>

userQueryRedisFlow queries 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 the key for the object stored in the Redis store.

userQueryRedisFlow

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("userRepo").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: Query\n&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: Query\n&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>

userQueryAllRedisFlow queries 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.

userQueryAllRedisFlow

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: QueryAll\n&quot;] #[&quot;Result: &quot; + payload + &quot;\n&quot;]"
doc:name="Set Payload" />
</when>
<otherwise>
<set-payload
value="#[&quot;\n Operation: Query\n&quot;]  #[&quot;Result: Record Not Available&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</flow>

userDeleteRedisFlow deletes 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.

userDeleteRedisFlow

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(message.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").delete(message.inboundProperties.'http.query.params'.email);
payload=app.registry.get("userRepo").get(message.inboundProperties.'http.query.params'.email);]]></expression-component>
<choice doc:name="Choice">
<when expression="#[payload == null]">
<set-payload
value="#[&quot;\n Operation: Delete\n&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: Delete\n&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: Delete\n&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>

userDeleteAllRedisFlow deletes 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.

userDeleteAllRedisFlow

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").delete();
payload=app.registry.get("userRepo").getAll();]]></expression-component>
<choice doc:name="Choice">
<when expression="#[payload == null]">
<set-payload
value="#[&quot;\n Operation: DeleteAll\n&quot;]  #[&quot;Result: All Records Deleted&quot;]"
doc:name="Set Payload" />
</when>
<otherwise>
<set-payload
value="#[&quot;\n Operation: DeleteAll\n&quot;]  #[&quot;Result: Unsuccessful&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</when>
<otherwise>
<set-payload
value="#[&quot;\n Operation: DeleteAll\n&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 represents the respected Redis Hash operation.

  • Select Mule Application With Maven option from Run As context menu to run the Mule application.

  • Open any browser and type the URL http://localhost:8081/ to obtain the User Registration HTML form. Fill the details and submit the form.

User Registration Form

On submission, the user registration details are processed by userRegistrationRedisFlow flow and stored in the Redis data store as a POJO. The following acknowledgment is sent back to the end user after completion of the registration process indicating successful insertion of a record in Redis data store.

Insert Operation Acknowledgement

Now, let’s query the record which was inserted in the earlier step. Type the URL http://localhost:8081/find?.yadav@abc.com in the address bar. The query result is shown in the screen shot below.

Query Operation

  

Now, let’s delete the record. Type the URL http://localhost:8081/delete?.yadav@abc.com in the address bar. The following acknowledgment screen must be displayed if the record is deleted successfully.

Delete Operation

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?.yadav@abc.com again. The following acknowledgment screen indicates the record has been deleted.

Query After Delete Operation

Type the delete operation URL http://localhost:8081/delete?.yadav@abc.com again. The following acknowledgment screen indicates that the record has been deleted.

Delete After Delete Operation

Now, let’s insert few more records into Redis data store and test to retrieve all the records.

Insert 1

 Insert 2

Insert 3

Type the URL http://localhost:8081/findAll in the address bar to retrieve all the records from the Redis data store, as shown below.

QueryAll Operation

Now, type the URL http://localhost:8081/deleteAll in the address bar to delete all the record.  

DeleteAll Operation

Let’s assure ourselves about whether all the records are deleted by retrieving the records by the URL http://localhost:8081/findAll. The acknowledgment shown in the below screenshot assures that the records were deleted.

QueryAll After DeleteAll Operation

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.JedisConnectionFactory">
</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.JacksonJsonRedisSerializer">
<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: Insert\n&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: Insert\n&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: Insert\n&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(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: Query\n&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: Query\n&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.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").delete(message.inboundProperties.'http.query.params'.email);
payload=app.registry.get("userRepo").get(message.inboundProperties.'http.query.params'.email);]]></expression-component>
<choice doc:name="Choice">
<when expression="#[payload == null]">
<set-payload
value="#[&quot;\n Operation: Delete\n&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: Delete\n&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: Delete\n&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: QueryAll\n&quot;] #[&quot;Result: &quot; + payload + &quot;\n&quot;]"
doc:name="Set Payload" />
</when>
<otherwise>
<set-payload
value="#[&quot;\n Operation: Query\n&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: DeleteAll\n&quot;]  #[&quot;Result: All Records Deleted&quot;]"
doc:name="Set Payload" />
</when>
<otherwise>
<set-payload
value="#[&quot;\n Operation: DeleteAll\n&quot;]  #[&quot;Result: Unsuccessful&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</when>
<otherwise>
<set-payload
value="#[&quot;\n Operation: DeleteAll\n&quot;]  #[&quot;Result: Records Not Available&quot;]"
doc:name="Set Payload" />
</otherwise>
</choice>
</flow>
</mule>

Conclusion

In this article, I have shown 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.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
tutorial ,spring data ,redis ,mule ,integration

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}