没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
SpringBoot利用利用redis集成消息队列的方法集成消息队列的方法
主要介绍了SpringBoot利用redis集成消息队列的方法,需要的朋友可以参考下
一、一、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消息队列连接工厂
@Bean
Receiver receiver(CountDownLatch latch) {
return new Receiver(latch);
}
//必要的redis消息队列连接工厂
@Bean
CountDownLatch latch() {
return new CountDownLatch(1);
}
//redis模板
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
//注入消息监听器容器
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(listenerAdapter, new PatternTopic("msg"));
return container;
}
//注入消息监听器容器
@Bean
MessageListenerAdapter 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;
资源评论
weixin_38664612
- 粉丝: 6
- 资源: 888
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功