SpringBoot利用redis集成消息队列
来源:互联网 发布:linux凝思重启网卡命令 编辑:程序博客网 时间:2024/05/18 05:20
一、pom文件依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、创建消息接收者
变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。@Autowired public Receiver(CountDownLatch latch) { this.latch = latch; } public void receiveMessage(String message) { LOGGER.info("收到的消息: <" + message + ">"); latch.countDown(); } }
以上基本条件达成后,以下是实现的三要素:
- 一个连接工厂
- 一个消息监听容器
- Redis template
三、在application.java注入消息接收者
@Bean Receiver receiver(CountDownLatch latch) { return new Receiver(latch); } @Bean CountDownLatch latch() { return new CountDownLatch(1); } @Bean StringRedisTemplate template(RedisConnectionFactory connectionFactory) { return new StringRedisTemplate(connectionFactory); }
四、注入消息监听容器
//必要的redis消息队列连接工厂@BeanReceiver receiver(CountDownLatch latch) {return new Receiver(latch);} //必要的redis消息队列连接工厂@BeanCountDownLatch latch() {return new CountDownLatch(1);} //redis模板@BeanStringRedisTemplate template(RedisConnectionFactory connectionFactory) {return new StringRedisTemplate(connectionFactory);} //注入消息监听器容器@BeanRedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.addMessageListener(listenerAdapter, new PatternTopic("msg"));return container;} //注入消息监听器容器@BeanMessageListenerAdapter listenerAdapter(Receiver receiver) {return new MessageListenerAdapter(receiver, "receiveMessage");}
五、单元测试
import java.util.concurrent.CountDownLatch;import org.junit.Test;import org.junit.runner.RunWith;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.context.ApplicationContext;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.test.context.junit4.SpringRunner; import com.Application; @RunWith(SpringRunner.class)@SpringBootTest(classes = Application.class)public class MsgQueueTest {@Autowiredprotected ApplicationContext ctx;private static final Logger logger = LoggerFactory.getLogger(MsgQueueTest.class);@Testpublic void SendMsg() {StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);CountDownLatch latch = ctx.getBean(CountDownLatch.class);logger.info("我要发送消息咯...");template.convertAndSend("msg", "欢迎使用redis的消息队列!");try {//发送消息连接等待中logger.info("消息正在发送...");latch.await();} catch (InterruptedException e) {logger.info("消息发送失败...");}} }
阅读全文
0 0
- SpringBoot利用redis集成消息队列
- SpringBoot集成Redis消息订阅发布
- SpringBoot 基于Redis快速实现消息队列
- 利用Redis 实现消息队列
- 利用Redis 实现消息队列
- 利用Redis 实现消息队列
- 利用Redis 实现消息队列
- 利用Redis 实现消息队列
- 利用Redis 实现消息队列
- 12.springboot使用redis的订阅功能实现消息队列
- SpringBoot -- 集成Redis/CacheManager
- SpringBoot -- 集成Redis/CacheManager
- springboot集成redis
- springboot集成Redis
- springboot 集成redis 哨兵
- springboot集成Redis
- SpringBoot集成Redis
- springboot集成redis详解
- 51 NOD 1417 天堂里的游戏(博弈)
- Ubuntu 网卡配置
- 32:行程长度编码
- MySql的Blob字段报错:Data truncation: Data too long for column 'CONTENT' at row 1
- [编程题] 涂棋盘
- SpringBoot利用redis集成消息队列
- 卷积神经网络的一些改进操作
- mac下安装git后提示安装xcode
- 后台返回json,前台接收有问题
- org.hibernate.exception.SQLGrammarException:could not insert:问题解决
- 8-29 DAIRY
- VS软件的日常
- Python 踩坑记录
- CCF考试练习——201612-1