Spring Data Redis Messaging – PubSub example | Spring Boot
Tags: springdata, springboot, subscrier, redis
Published: 2021-04-20 10:54:07

https://grokonez.com/spring-framework/spring-boot/spring-data-redis-messaging-pubsub-spring-boot-spring-data-redis-example

Spring Data Redis Messaging – PubSub example | Spring Boot

The previous <a href="https://grokonez.com/spring-framework/spring-boot/spring-data-redis-example-spring-boot-redis-example">post</a> introduces way to do Redis CRUD Operations. In this tutorial, we're gonna look at how to implement Redis Messaging- PubSub with Spring Boot.

Related Post: <a href="https://grokonez.com/spring-framework/spring-boot/spring-data-redis-example-spring-boot-redis-example">Spring Data Redis CRUD Operations example with Spring Boot</a>

<!--more-->
<h2>I. Technology</h2>
- Java 1.8
- Maven 3.3.9
- Spring Tool Suite – Version 3.9.0.RELEASE
- Spring Boot: 1.5.9.RELEASE
<h2>II. Spring Data Redis Messaging - PubSub</h2>
<h3>1. Overview</h3>
Spring Data provides dedicated messaging integration for Redis that can be roughly divided into two areas of functionality: production (or publication) and consumption (or subscription) of messages.

- Publisher doesn't know specific subscribers to send messages to. Instead, Publisher specifies channel.
- Subscriber receives messages from one or more topics, without knowledge of Publisher.
<h3>2. Maven Dependency</h3>
<pre><code class="language-html">&lt;dependency&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-data-redis&lt;/artifactId&gt;
&lt;/dependency&gt;</code></pre>
<h3>3. Redis Configuration</h3>
We need:
- connection factory -> <code>JedisConnectionFactory</code>
- a channel -> <code>ChannelTopic</code>
- a template for publisher to publish messages -> <code>RedisTemplate</code>
- a message listener for consumer to consume messages -> <code>RedisMessageListenerContainer</code>
<pre><code class="language-java">@Bean
JedisConnectionFactory jedisConnectionFactory() {
	return new JedisConnectionFactory();
}

@Bean
public RedisTemplate&lt;String, Object&gt; redisTemplate() {
	final RedisTemplate&lt;String, Object&gt; template = new RedisTemplate&lt;String, Object&gt;();
	template.setConnectionFactory(jedisConnectionFactory());
	template.setValueSerializer(new GenericToStringSerializer&lt;Object&gt;(Object.class));
	return template;
}

@Bean
MessageListenerAdapter messageListener() {
	return new MessageListenerAdapter(new CustomerInfoSubscriber());
}

@Bean
RedisMessageListenerContainer redisContainer() {
	final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
	container.setConnectionFactory(jedisConnectionFactory());
	container.addMessageListener(messageListener(), topic());
	container.setTaskExecutor(Executors.newFixedThreadPool(4));
	return container;
}

@Bean
CustomerInfoPublisher redisPublisher() {
	return new RedisCustomerInfoPublisher(redisTemplate(), topic());
}

@Bean
ChannelTopic topic() {
	return new ChannelTopic(&quot;pubsub:jsa-channel&quot;);
}</code></pre>
We can also supply an <strong>executor</strong> within <code>RedisMessageListenerContainer</code> for asynch manner of messages.

https://grokonez.com/spring-framework/spring-boot/spring-data-redis-messaging-pubsub-spring-boot-spring-data-redis-example

Spring Data Redis Messaging – PubSub example | Spring Boot