DEV Community

loading...

Spring Data Redis Messaging – PubSub example | Spring Boot

loizenai
Software Engineer - Founder at https://loizenai.com
・2 min read

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

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </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

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
    return new JedisConnectionFactory();
    }

@Bean
public RedisTemplate<String, Object> redisTemplate() {
final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(jedisConnectionFactory());
template.setValueSerializer(new GenericToStringSerializer<Object>(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("pubsub:jsa-channel");
}
We can also supply an executor within RedisMessageListenerContainer 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

Discussion (0)

Forem Open with the Forem app