环境依赖

创建一个新的springboot工程,在其pom文件,加入spring-boot-starter-data-redis依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

创建一个消息接收者

REcevier类,它是一个普通的类,需要注入到springboot中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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();
    }
}

  

注入消息接收者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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步已经完成,所以只需注入消息监听容器即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
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程序,控制台打印:

1
2
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

  

在springboot中用redis实现消息队列的更多相关文章

  1. SpringBoot非官方教程 | 第十四篇:在springboot中用redis实现消息队列

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot14-redis-mq/ 本文出自方志朋的博客 这 ...

  2. Spring Boot教程(一)在springboot中用redis实现消息队列

    环境依赖 创建一个新的springboot工程,在其pom文件,加入spring-boot-starter-data-redis依赖: <dependency> <groupId&g ...

  3. 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能

    springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...

  4. Redis 做消息队列

    一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式.利用redis这两种场景的消息队列都能够实现.定义: 生产者消费者模式:生产者生产消息放到队列里,多个消费者同时监听队列, ...

  5. Redis作为消息队列服务场景应用案例

    NoSQL初探之人人都爱Redis:(3)使用Redis作为消息队列服务场景应用案例   一.消息队列场景简介 “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更 ...

  6. redis resque消息队列

    Resque 目前正在学习使用resque .resque-scheduler来发布异步任务和定时任务,为了方便以后查阅,所以记录一下. resque和resque-scheduler其优点在于功能比 ...

  7. springboot整合mq接收消息队列

    继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...

  8. 【Redis】php+redis实现消息队列

    在项目中使用消息队列一般是有如下几个原因: 把瞬间服务器的请求处理换成异步处理,缓解服务器的压力 实现数据顺序排列获取 redis实现消息队列步骤如下: 1).redis函数rpush,lpop 2) ...

  9. Lumen开发:结合Redis实现消息队列(1)

    1.简介 Lumen队列服务为各种不同的后台队列提供了统一的API.队列允许你推迟耗时任务(例如发送邮件)的执行,从而大幅提高web请求速度. 1.1 配置 .env文件的QUEUE_DRIVER选项 ...

随机推荐

  1. 网页中Div的打印

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  2. rostopic demo

    发布者 #!/usr/bin/env python2. # -*- coding: utf- -*- import rospy from hdw_driver.msg import update_fi ...

  3. idea使用教程(2)

    目录: 1. open和import的区别 2.修改虚拟机配置信息 3.安装目录和设置目录 1. open和import的区别 open:如果本身就是idea项目,则可以直接open打开; impor ...

  4. 常见字符集&乱码问题

    字符集 常用字符集分类 ASCII及其扩展字符集 作用:表语英语及西欧语言. 位数:ASCII是用7位表示的,能表示128个字符:其扩展使用8位表示,表示256个字符. 范围:ASCII从00到7F, ...

  5. idataway_前端

    一半架构,一半标准.---纯属个人意见 ----------------------------------------- 基本的插件:(尽量使用iviews) 1.传输图片. 2.下拉框.日期控件. ...

  6. 第 6 章 存储 - 038 - Docker 的两类存储资源

    存储资源 Docker 为容器提供了两种存放数据的资源: 由 storage driver 管理的镜像层和容器层 Data Volume 1.storage driver 容器由最上面一个可写的容器层 ...

  7. PHP中如何命令行

    PHP中如何命令行 一.总结 一句话总结:配置php系统环境,然后命令行中运行 php -f 文件名即可 配置php系统环境 php_-f_文件名 例如: 1.三种运行php的方式? 运行文件_-f ...

  8. SQL中的where条件,在数据库中提取与应用浅析

    1. 问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当然,要完整描述一条SQL在数据库中的生命周期,这是一个非常巨大的问题,涵盖了SQL的词法解析.语法解析.权限 ...

  9. Windows 7 Update Settings Disabled (Important Updates Grayed Out)

    This worked for me: 1) Hold WindowsKey + R     (is hold Start & press R on your keyboard) 2) Typ ...

  10. RepBaseRepeatMaskerEdition下载 | RepeatMasker

    开源的生物信息世界居然有这么个需要注册才能下载的工具,开源世界不是怎么方便怎么来吗? 这个注册真的麻烦,这里上传了一个可以使用的版本. RepBaseRepeatMaskerEdition-20170 ...