DEV Community


Spring Data Redis Messaging – PubSub example | Spring Boot

Software Engineer - Founder at
・2 min read

Spring Data Redis Messaging – PubSub example | Spring Boot

The previous post 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: Spring Data Redis CRUD Operations example with Spring Boot

I. Technology

  • Java 1.8
  • Maven 3.3.9
  • Spring Tool Suite – Version 3.9.0.RELEASE
  • Spring Boot: 1.5.9.RELEASE

    II. Spring Data Redis Messaging - PubSub

    1. Overview

    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.

    2. Maven Dependency


    3. Redis Configuration

    We need:

  • connection factory -> JedisConnectionFactory

  • a channel -> ChannelTopic

  • a template for publisher to publish messages -> RedisTemplate

  • a message listener for consumer to consume messages -> RedisMessageListenerContainer

    JedisConnectionFactory jedisConnectionFactory() {
    return new JedisConnectionFactory();

public RedisTemplate<String, Object> redisTemplate() {
final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
return template;

MessageListenerAdapter messageListener() {
return new MessageListenerAdapter(new CustomerInfoSubscriber());

RedisMessageListenerContainer redisContainer() {
final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.addMessageListener(messageListener(), topic());
return container;

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

ChannelTopic topic() {
return new ChannelTopic("pubsub:jsa-channel");
We can also supply an executor within RedisMessageListenerContainer for asynch manner of messages.

Spring Data Redis Messaging – PubSub example | Spring Boot

Discussion (0)

Forem Open with the Forem app