In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to use the publish and subscribe model of redis in Spring Boot". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the publish and subscribe model of redis in Spring Boot.
Redis is not only a very powerful non-relational database, but also has the pub/sub function of message middleware. You can use the pub/sub function of redis by setting the following settings in spring boot:
1. Create the setting class @ Configurationpublic class RedisSubListenerConfig {/ / initialize the listener [@ Bean] (https://my.oschina.net/bean) RedisMessageListenerContainer container (RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {RedisMessageListenerContainer container = new RedisMessageListenerContainer (); container.setConnectionFactory (connectionFactory); container.addMessageListener (listenerAdapter, new PatternTopic ("here is the name of the listening channel"); return container } / / use reflection to create an execution method [@ Bean] (https://my.oschina.net/bean) MessageListenerAdapter listenerAdapter (RedisReceiver redisReceiver) {return new MessageListenerAdapter (redisReceiver, "receiveMessage") after listening to a message } / / initialize the redis operation template [@ Bean] (https://my.oschina.net/bean) StringRedisTemplate template (RedisConnectionFactory connectionFactory) {return new StringRedisTemplate (connectionFactory);}} 2. Create the receiver method class [@ Service] (https://my.oschina.net/service)public class RedisReceiver {@ Autowired RedisService redisService; public void receiveMessage (String message) {/ / here is the method executed after receiving the message from the channel}} 3. Use reidsTemplate to send messages to channels @ Servicepublic class RedisService {@ Autowired private StringRedisTemplate stringRedisTemplate; / / method of sending messages to channels public void sendChannelMess (String channel, String message) {stringRedisTemplate.convertAndSend (channel, message);}}
Attachment: some settings in the RedisReceiver accept class constructor passed CountDownLatch to control the thread, if you do not need to control the thread can not be used.
Give an example
@ Component@Slf4jpublic class MessageReceiver {@ Autowired ISocketService socketService; / * * method to receive messages * @ throws Exception * / public void receiveMessage (String message) throws Exception {log.info ("receive a message From Redis:" + message); Message msg = JSON.parseObject (message, Message.class) If (null = = msg.getAction ()) {throw new Exception ("must set the action that the client needs to take after receiving the message");} if (null = = msg.getTableId () | | ".equals (msg.getTableId () {throw new Exception (" must set the table code to receive the message! ") If (msg.getAction (). Equals (Event.NOTICEOTHER)) {NoticeOtherDto noticeOtherDto = JSON.parseObject (msg.getData (). ToString (), NoticeOtherDto.class); msg.setData (noticeOtherDto.getData ()); socketService.noticeOther (msg.getTenantId () + "-" + msg.getTableId (), noticeOtherDto.getEventName (), msg) } else {/ / send group messages via WebSocket socketService.sendMessageToTable (msg.getTenantId () + "-" + msg.getTableId (), msg.getAction (), msg);} @ Componentpublic class MessageSender {@ Autowired private StringRedisTemplate stringRedisTemplate; public void sendMessage (Message message) {stringRedisTemplate.convertAndSend (RedisConstant.TOPIC_KEY, JSON.toJSONString (message)) }} at this point, I believe you have a deeper understanding of "how to use the publish and subscribe model of redis in Spring Boot". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.