Spring Data Redis Messaging – PubSub example | Spring Boot

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

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

451 bài viết.
79 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 5 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/android/firebasecloudmessagingsendupstreammessagefirebaseandroid Firebase Cloud Messaging – How to Send Upstream Messages | A...
loveprogramming viết 16 ngày trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
451 bài viết.
79 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!