您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容主要讲解“用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。