Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Is it useful to use redis as a message queue?

Shulou Source: shulou.com Published: 2022-06-02 10:03:28 09月30日 Update

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!

Tags: Messages queues methods useful monitoring content this method processing learning testing practical deeper no global interest function parameters entities practicality practice Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple NVidia macOS Linux Microsoft