In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is to explain "is it useful to use redis as a message queue", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "is it useful to use redis as a message queue?"
I don't think redis message queuing is very good. Although it has the function of message queuing and can do delay, it is not recommended to use redis as message queuing.
1. Pom files org.springframework.boot spring-boot-starter-data-redis2, application.propertiesspring.redis.database=15spring.redis.host=localhostspring.redis.port=6379spring.redis.password=spring.redis.timeout=10000ms3, entity classes / * * @ Author:MuJiuTian * @ Description: definition of global topicname * @ Date: Created in 10:54 2019-9-26 * / public class TopicName {public static String topic_name_test = "loving" 4. Service layer (producer) @ Servicepublic class PublishService {@ Autowired StringRedisTemplate redisTemplate; public void sendMessage (String channel, Object message) {redisTemplate.convertAndSend (channel, message);}} 5, Controller layer / * * @ Author:MuJiuTian * @ Description: test redis message queue * @ Date: Created in 1:49 2019-9-10 * / @ RestController@RequestMapping (value = "/ mq") public class RedisMqController {@ Autowired PublishService publishService @ GetMapping (value = "/ sendMessage") public String sendMessage () {publishService.sendMessage (TopicName.topic_name_test, "i love you aoxin"); return "SUCCESS";} 6, configuration class import org.springframework.boot.autoconfigure.AutoConfigureAfter;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.listener.PatternTopic Import org.springframework.data.redis.listener.RedisMessageListenerContainer;import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;@Configuration@AutoConfigureAfter ({Receiver.class}) public class SubscriberConfig {/ * bind message listeners and methods to receive listeners * / @ Bean public MessageListenerAdapter listenerAdapter (Receiver receiver) {/ / if you use this method, then Receiver implements MessageListener return new MessageListenerAdapter (receiver) / / if you use this method, Receiver does not use implements MessageListener and can customize the method to handle messages. The second parameter below is the method name / / return new MessageListenerAdapter (receiver, "receiveMessage");} / * create a message listening container * / @ Bean public RedisMessageListenerContainer getRedisMessageListenerContainer (RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter messageListenerAdapter) {RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer () RedisMessageListenerContainer.setConnectionFactory (redisConnectionFactory); redisMessageListenerContainer.addMessageListener (messageListenerAdapter, new PatternTopic (TopicName.topic_name_test)); return redisMessageListenerContainer;}} 7, consumer import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.connection.Message;import org.springframework.data.redis.connection.MessageListener;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.stereotype.Component / * * @ Author:MuJiuTian * @ Description: message acceptance and processing * @ Date: Created in 1:57 2019-9-10 * / @ Componentpublic class Receiver implements MessageListener {@ Autowired StringRedisTemplate redisTemplate; / * public void receiveMessage (String message) {System.out.println (message);} * / @ Override public void onMessage (Message message, byte [] bytes) {RedisSerializer valueSerializer = redisTemplate.getStringSerializer () String deserialize = valueSerializer.deserialize (message.getBody ()); System.out.println (deserialize);}} 8, Test
At this point, I believe you have a deeper understanding of "is it useful to use redis as a message queue?" 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.