摘要:环境依赖创建一个新的工程,在其文件加入依赖创建一个消息接收者类,它是一个普通的类,需要注入到中。
这篇文章主要讲述如何在springboot中用reids实现消息队列。
准备阶段</>复制代码
安装redis,可参考我的另一篇文章,5分钟带你入门Redis。
java 1.8
maven 3.0
idea
环境依赖
创建一个新的springboot工程,在其pom文件,加入spring-boot-starter-data-redis依赖:
</>复制代码
org.springframework.boot
spring-boot-starter-data-redis
创建一个消息接收者
REcevier类,它是一个普通的类,需要注入到springboot中。
</>复制代码
public class Receiver {
private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);
private CountDownLatch latch;
@Autowired
public Receiver(CountDownLatch latch) {
this.latch = latch;
}
public void receiveMessage(String message) {
LOGGER.info("Received <" + message + ">");
latch.countDown();
}
}
注入消息接收者
</>复制代码
@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);
}
注入消息监听容器
在spring data redis中,利用redis发送一条消息和接受一条消息,需要三样东西:
</>复制代码
一个连接工厂
一个消息监听容器
Redis template
上述1、3步已经完成,所以只需注入消息监听容器即可:
</>复制代码
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(listenerAdapter, new PatternTopic("chat"));
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
测试
在springboot入口的main方法:
</>复制代码
public static void main(String[] args) throws Exception{
ApplicationContext ctx = SpringApplication.run(SpringbootRedisApplication.class, args);
StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
CountDownLatch latch = ctx.getBean(CountDownLatch.class);
LOGGER.info("Sending message...");
template.convertAndSend("chat", "Hello from Redis!");
latch.await();
System.exit(0);
}
先用redisTemplate发送一条消息,接收者接收到后,打印出来。启动springboot程序,控制台打印:
</>复制代码
2017-04-20 17:25:15.536 INFO 39148 — [ main] com.forezp.SpringbootRedisApplication : Sending message…
2017-04-20 17:25:15.544 INFO 39148 — [ container-2] com.forezp.message.Receiver : 》Received
源码下载:
https://github.com/forezp/Spr...
参考资料messaging-redis
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/67814.html
摘要:是一个开源的应用容器引擎,基于语言并遵从协议开源。准备工作环境环境或不要用对一无所知的看教程。创建一个工程引入的起步依赖,创建一个将工程容器化有一个简单的文件作为指定镜像的图层。说明的工程已部署。停止镜像删除镜像参考资料源码下载 这篇文篇介绍,怎么为 springboot程序构建一个docker镜像。docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议...
摘要:创建消息监听,并发送一条消息在程序中,提供了发送消息和接收消息的所有方法。 这篇文章带你了解怎么整合RabbitMQ服务器,并且通过它怎么去发送和接收消息。我将构建一个springboot工程,通过RabbitTemplate去通过MessageListenerAdapter去订阅一个POJO类型的消息。 准备工作 15min IDEA maven 3.0 在开始构建项目之前,机器需...
摘要:本文介绍如何在中使用默认的声明式缓存定义和接口用来统一不同的缓存技术。在使用集成的时候,我们需要注册实现的的。默认使用在我们不使用其他第三方缓存依赖的时候,自动采用作为缓存管理器。源码下载参考资料揭秘与实战二数据缓存篇快速入门 本文介绍如何在springboot中使用默认的spring cache 声明式缓存 Spring 定义 CacheManager 和 Cache 接口用来统一不...
摘要:用于控制活动人数,将超过此一定阀值的订单直接丢弃。缓解短时间的高流量压垮应用。目前比较推荐的就是我们手动然后将消费错误的消息转移到其它的消息队列中,做补偿处理消费者该方案是默认的方式不太推荐。 SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相...
阅读 1004·2021-10-13 09:48
阅读 4049·2021-09-22 10:53
阅读 3201·2021-08-30 09:41
阅读 2011·2019-08-30 15:55
阅读 2986·2019-08-30 15:55
阅读 1920·2019-08-30 14:11
阅读 2272·2019-08-29 13:44
阅读 842·2019-08-26 12:23