加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

SpringBoot利用redis集成消息队列的方法

发布时间:2020-12-14 19:52:09 所属栏目:Java 来源:网络整理
导读:一、pom文件依赖 dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-redis/artifactId /dependency 二、创建消息接收者 变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get

一、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;
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 {
@Autowired
protected ApplicationContext ctx;
private static final Logger logger = LoggerFactory.getLogger(MsgQueueTest.class);
@Test
public 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("消息发送失败...");
}
}
}

总结

以上所述是小编给大家介绍的SpringBoot利用redis集成消息队列的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

您可能感兴趣的文章:

  • 浅谈SpringBoot集成Redis实现缓存处理(Spring AOP实现)
  • SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存的方法
  • springboot+mybatis+redis 二级缓存问题实例详解
  • SpringBoot项目中使用redis缓存的方法步骤
  • springboot中使用redis由浅入深解析
  • 详解springboot中redis的使用和分布式session共享问题
  • springboot整合redis进行数据操作(推荐)
  • SpringBoot初步连接redis详解

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读