當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot项目redis的消息队列
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot项目redis的消息队列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一步導入redis依賴即可
第二步修改配置文件(這里我用的是mvn項目)
spring.application.name=springbootdemospring.rabbitmq.host=192.168.0.1spring.rabbitmq.port=5672spring.rabbitmq.username=rootspring.rabbitmq.password=123456第三步(注冊redisTemplate)
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.StringRedisTemplate;/*** 注冊redisTemplate,作為消息隊列的發布者*/@Configurationpublic class PublisherConfig {@Beanpublic StringRedisTemplate getRedisTemplate(RedisConnectionFactory redisConnectionFactory) {return new StringRedisTemplate(redisConnectionFactory);}}第四步(創建一個接收消息的類,繼承MessageListener,也可以不繼承)
繼承
import org.slf4j.Logger;import org.slf4j.LoggerFactory;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;@Componentpublic class Receiver implements MessageListener {private static Logger logger = LoggerFactory.getLogger(Receiver.class);@Autowiredprivate StringRedisTemplate redisTemplate;@Overridepublic void onMessage(Message message, byte[] pattern) {RedisSerializer<String> valueSerializer = redisTemplate.getStringSerializer();String deserialize = valueSerializer.deserialize(message.getBody());logger.info("收到的mq消息" + deserialize);}}不繼承
import org.slf4j.Logger;import org.slf4j.LoggerFactory;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;@Componentpublic class Receiver {private static Logger logger = LoggerFactory.getLogger(Receiver.class); @Autowiredprivate StringRedisTemplate redisTemplate;public void receiveMessage(String message) {System.out.println("收到的mq消息" + message);}}第五步(消息訂閱者配置類)
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 {public static final String MSG_CONFIG="TOPIC_NAME";/*** 消息監聽適配器,注入接受消息方法,輸入方法名字 反射方法*/@Beanpublic MessageListenerAdapter getMessageListenerAdapter(Receiver receiver) {return new MessageListenerAdapter(receiver,"receiveMessage"); //當沒有繼承MessageListener時需要寫方法名字}/*** 創建消息監聽容器*/@Beanpublic RedisMessageListenerContainer getRedisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter messageListenerAdapter) {RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);redisMessageListenerContainer.addMessageListener(messageListenerAdapter, new PatternTopic(SubscriberConfig.MSG_CONFIG));return redisMessageListenerContainer;}}第六步(消息生產者發送消息)
StringRedisTemplate redisTemplate= SpringUtil.getBean(StringRedisTemplate.class);redisTemplate.convertAndSend(SubscriberConfig.MSG_CONFIG, result.toString()); //使用消息隊列這里用到的SpringUtil類是 spring獲取bean工具類spring獲取bean工具類參考
最后大工告成
總結
以上是生活随笔為你收集整理的SpringBoot项目redis的消息队列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工信部正式公布小米汽车“证件照”
- 下一篇: js的异步请求出现同步效果