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

 更新时间:2017年08月28日 11:59:25   作者:子轩、破译者  
这篇文章主要介绍了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;
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集成消息队列的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • Java模板引擎生成代码的几种常见方式及其使用方法

    Java模板引擎生成代码的几种常见方式及其使用方法

    在Java中,有多种模板引擎可以用于代码生成,包括FreeMarker、Velocity、Thymeleaf等,下面我将详细介绍每种方式的优缺点,并提供完整的代码示例,需要的朋友可以参考下
    2025-08-08
  • 在启动后台 jar包时,使用指定的 application.yml操作

    在启动后台 jar包时,使用指定的 application.yml操作

    这篇文章主要介绍了在启动后台 jar包时,使用指定的 application.yml操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • SpringBoot日志配置全过程

    SpringBoot日志配置全过程

    Spring Boot默认使用Logback作为日志框架,可以配置多种日志系统,包括JavaUtilLogging、CommonsLogging、Log4J及SLF4J,默认日志输出在控制台,可以通过配置文件将日志保存到文件中,日志级别包括TRACE、DEBUG、INFO、WARN、ERROR和FATAL
    2025-01-01
  • Java读取properties配置文件时,出现中文乱码的解决方法

    Java读取properties配置文件时,出现中文乱码的解决方法

    下面小编就为大家带来一篇Java读取properties配置文件时,出现中文乱码的解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • Java 17 更快的 LTS 节奏

    Java 17 更快的 LTS 节奏

    这篇文章主要介绍的是Java 17 更新后的LTS,现在 Java 17 的发布,让 Java 11 成了 LTS 系列的次新版本,下面我们就来看看Java 17 的更新 LTS有什么变化吧
    2021-09-09
  • java线程池ExecutorService超时处理小结

    java线程池ExecutorService超时处理小结

    使用ExecutorService时,设置子线程执行超时是一个常见需求,本文就来详细的介绍一下ExecutorService超时的三种方法,感兴趣的可以了解一下
    2024-09-09
  • Springboot配置文件加密方式

    Springboot配置文件加密方式

    文章介绍了使用Jasypt对服务器配置文件进行加密,包括引入依赖、配置密钥、生成密钥、IDEA和Linux启动配置等步骤,解决了版本和JCE安全插件的问题
    2025-10-10
  • elasticsearch head的安装及使用过程解析

    elasticsearch head的安装及使用过程解析

    这篇文章主要介绍了elasticsearch head的安装及使用过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • Spring Cloud Feign实现动态URL

    Spring Cloud Feign实现动态URL

    本文主要介绍了Spring Cloud Feign实现动态URL,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • SpringBoot排除不需要的自动配置类DataSourceAutoConfiguration问题

    SpringBoot排除不需要的自动配置类DataSourceAutoConfiguration问题

    这篇文章主要介绍了SpringBoot排除不需要的自动配置类DataSourceAutoConfiguration问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07

最新评论