环境依赖

创建一个新的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. node.js模块本地代理模块(将自己本机/局域网)服务 代理到外网可以访问

    npm 模块 localtunnel模块可以. lt --port 7000  其中7000是本地服务端口 会分配一个动态的url. 局域网中查看本地ip用ifconfg 或者在系统偏好设置里面查看网 ...

  2. _event_spawn

    EventId 事件ID Entry 刷新生物或物体的entry,正数为生物,负数为物体 Phase 刷新生物或物体的阶段ID SpawnTime 刷新生物或物体的时间(单位:秒),从阶段开始时开始计 ...

  3. 初识unittest

    unittest是python自带的一个标准木块,单元测试框架 unittest基本使用方法: 我们需要先导入unittest (import unittest) import unittest 定义 ...

  4. 异常处理.VC++

    ZC:个人这样 理解 C++的异常处理: ZC: (1).C++标准异常处理,try{}catch{} 抛异常:throw() [ 据说是包装的Windows函数RaiseException() ] ...

  5. 力扣(LeetCode)500. 键盘行

    给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例: 输入: ["Hello", "Alaska", "Dad& ...

  6. 牛客网NOIP赛前集训营-普及组(第一场)C 括号

    括号 思路: dp 状态:dp[i][j]表示到i位置为止未匹配的 '(' 个数为j的方案数 状态转移: 如果s[i] == '(' dp[i][j] = dp[i-1][j] + dp[i-1][j ...

  7. echarts的学习

    博客1.:https://zrysmt.github.io/ 博客2:http://blog.csdn.net/future_todo/article/details/60956942 工作中一个需求 ...

  8. Appium的工作原理

    把我们写的python语言代码,看做客户端 通过客户端向appium服务器发送请求 appium服务器把我们的代码转换成手机可以识别的指令 然后把指令发给手机,手机根据指令做出相应的操作 最后手机把操 ...

  9. 输出图片格式BARTENDER

    try {                BarTender.Application btApp = new BarTender.Application();                BarTe ...

  10. c# zfc

    1.根据单个分隔字符用split截取 例如 复制代码代码如下: string st="GT123_1"; string[] sArray=st.split("_" ...