您好,登錄后才能下訂單哦!
本篇內容主要講解“用redis做消息隊列有用嗎”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“用redis做消息隊列有用嗎”吧!
我覺得redis消息隊列不太好,雖然有消息隊列的功能,也能做延遲,但是不建議使用redis做消息隊列。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
spring.redis.database=15 spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.timeout=10000ms
/** * @Author:MuJiuTian * @Description: 全局topicname的定義 * @Date: Created in 上午10:54 2019/9/26 */ public class TopicName { public static String topic_name_test = "loving"; }
@Service public class PublishService { @Autowired StringRedisTemplate redisTemplate; public void sendMessage(String channel, Object message){ redisTemplate.convertAndSend(channel, message); } }
/** * @Author:MuJiuTian * @Description: 測試redis消息隊列 * @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"; } }
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 { /** * 綁定消息監聽者和接收監聽的方法 */ @Bean public MessageListenerAdapter listenerAdapter(Receiver receiver){ //如果使用此方法,那么Receiver則 implements MessageListener return new MessageListenerAdapter(receiver); // 如果使用此方法,那么Receiver則 不用implements MessageListener,可以自定義方法處理消息,下面的第二個參數為方法名 //return new MessageListenerAdapter(receiver,"receiveMessage"); } /** * 創建消息監聽容器 */ @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; } }
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: 消息接受然后處理 * @Date: Created in 下午1:57 2019/9/10 */ @Component public 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<String> valueSerializer = redisTemplate.getStringSerializer(); String deserialize = valueSerializer.deserialize(message.getBody()); System.out.println(deserialize); } }
到此,相信大家對“用redis做消息隊列有用嗎”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。