本篇内容主要讲解“用redis做消息队列有用吗”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“用redis做消息队列有用吗”吧!
公司主营业务:网站建设、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出湘潭免费做网站回馈大家。
我觉得redis消息队列不太好,虽然有消息队列的功能,也能做延迟,但是不建议使用redis做消息队列。
org.springframework.boot spring-boot-starter-data-redis
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) { RedisSerializervalueSerializer = redisTemplate.getStringSerializer(); String deserialize = valueSerializer.deserialize(message.getBody()); System.out.println(deserialize); } }
到此,相信大家对“用redis做消息队列有用吗”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!