package com.loan.msg.config;

import com.loan.msg.service.MessageReceiver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; /**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: RedisMQConfig
* @description:
* @date 2018/11/13:10:12
*/
@Configuration
public class RedisMQConfig {
/**
* 注入消息监听容器
*
* @param connectionFactory 连接工厂
* @param listenerAdapter 监听处理器1
* @param listenerAdapter 监听处理器2 (参数名称需和监听处理器的方法名称一致,因为@Bean注解默认注入的id就是方法名称)
* @return
*/
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter,
MessageListenerAdapter listenerAdapter2) { RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
//订阅一个叫mq_01 的信道
container.addMessageListener(listenerAdapter, new PatternTopic("mq_01"));
//订阅一个叫mq_02 的信道
container.addMessageListener(listenerAdapter2, new PatternTopic("mq_02"));
//这个container 可以添加多个 messageListener
return container;
} /**
* 消息监听处理器1
*
* @param receiver 处理器类
* @return
*/
@Bean
MessageListenerAdapter listenerAdapter(MessageReceiver receiver) {
//给messageListenerAdapter 传入一个消息接收的处理器,利用反射的方法调用“receiveMessage”
return new MessageListenerAdapter(receiver, "receiveMessage"); //receiveMessage:接收消息的方法名称
} /**
* 消息监听处理器2
*
* @param receiver 处理器类
* @return
*/
@Bean
MessageListenerAdapter listenerAdapter2(MessageReceiver receiver) {
//给messageListenerAdapter 传入一个消息接收的处理器,利用反射的方法调用“receiveMessage2”
return new MessageListenerAdapter(receiver, "receiveMessage2"); //receiveMessage:接收消息的方法名称
}
}
package com.loan.msg.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; /**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: SendMessageService
* @description:
* @date 2018/11/13:10:11
*/
@Service
public class SendMessageService {
@Autowired
private StringRedisTemplate stringRedisTemplate; /**
* 发布消息
*
* @param channel 消息信道
* @param message 消息内容
*/
public void sendMessage(String channel, String message) {
stringRedisTemplate.convertAndSend(channel, message);
} /**
* 发布消息的方法
*/
public void setStr01() {
this.sendMessage("mq_01", "发送信息内容01");
this.sendMessage("mq_01", "发送信息内容011");
this.sendMessage("mq_02", "发送信息内容02"); }
}
package com.loan.msg.service;

import org.springframework.stereotype.Component;

/**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: MessageReceiver
* @description:
* @date 2018/11/13:10:14
*/
@Component
public class MessageReceiver {
/**
* 接收消息的方法1
**/
public void receiveMessage(String message) {
System.out.println("receiveMessage接收到的消息:" + message);
} /**
* 接收消息的方法2
**/
public void receiveMessage2(String message) {
System.out.println("receiveMessage2接收到的消息:" + message);
}
}
//向redis里存入数据和设置缓存时间
stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS); //val做-1操作
stringRedisTemplate.boundValueOps("test").increment(-1); //根据key获取缓存中的val
stringRedisTemplate.opsForValue().get("test") //val +1
stringRedisTemplate.boundValueOps("test").increment(1); //根据key获取过期时间
stringRedisTemplate.getExpire("test") //根据key获取过期时间并换算成指定单位
stringRedisTemplate.getExpire("test",TimeUnit.SECONDS) //根据key删除缓存
stringRedisTemplate.delete("test"); //检查key是否存在,返回boolean值
stringRedisTemplate.hasKey("546545"); //向指定key中存放set集合
stringRedisTemplate.opsForSet().add("red_123", "1","2","3"); //设置过期时间
stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS); //根据key查看集合中是否存在指定数据
stringRedisTemplate.opsForSet().isMember("red_123", "1") //根据key获取set集合
stringRedisTemplate.opsForSet().members("red_123")

Spring boot 使用Redis 消息队列的更多相关文章

  1. Spring Boot (25) RabbitMQ消息队列

    MQ全程(Message Queue)又名消息队列,是一种异步通讯的中间件.可以理解为邮局,发送者将消息投递到邮局,然后邮局帮我们发送给具体的接收者,具体发送过程和时间与我们无关,常见的MQ又kafk ...

  2. Spring Boot 之 RabbitMQ 消息队列中间件的三种模式

    开门见山(文末附有消息队列的几个基本概念) 1.直接模式( Direct)模式 直白的说就是一对一,生产者对应唯一的消费者(当然同一个消费者可以开启多个服务). 虽然使用了自带的交换器(Exchang ...

  3. Spring Boot使用Redis进行消息的发布订阅

    今天来学习如何利用Spring Data对Redis的支持来实现消息的发布订阅机制.发布订阅是一种典型的异步通信模型,可以让消息的发布者和订阅者充分解耦.在我们的例子中,我们将使用StringRedi ...

  4. 【redis】spring boot利用redis的Keyspace Notifications实现消息通知

    前言 需求:当redis中的某个key失效的时候,把失效时的value写入数据库. github: https://github.com/vergilyn/RedisSamples 1.修改redis ...

  5. 15套java架构师、集群、高可用、高可扩展、高性能、高并发、性能优化、Spring boot、Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分布式项目实战视频教程

    * { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩展. ...

  6. 几种常见的微服务架构方案简述——ZeroC IceGrid、Spring Cloud、基于消息队列

    微服务架构是当前很热门的一个概念,它不是凭空产生的,是技术发展的必然结果.虽然微服务架构没有公认的技术标准和规范草案,但业界已经有一些很有影响力的开源微服务架构平台,架构师可以根据公司的技术实力并结合 ...

  7. 几种常见的微服务架构方案——ZeroC IceGrid、Spring Cloud、基于消息队列、Docker Swarm

    微服务架构是当前很热门的一个概念,它不是凭空产生的,是技术发展的必然结果.虽然微服务架构没有公认的技术标准和规范草案,但业界已经有一些很有影响力的开源微服务架构平台,架构师可以根据公司的技术实力并结合 ...

  8. java-spring基于redis单机版(redisTemplate)实现的分布式锁+redis消息队列,可用于秒杀,定时器,高并发,抢购

    此教程不涉及整合spring整合redis,可另行查阅资料教程. 代码: RedisLock package com.cashloan.analytics.utils; import org.slf4 ...

  9. Spring Boot与Redis的集成

    Redis是一个完全开源免费的.遵守BSD协议的.内存中的数据结构存储,它既可以作为数据库,也可以作为缓存和消息代理.因其性能优异等优势,目前已被很多企业所使用,但通常在企业中我们会将其作为缓存来使用 ...

随机推荐

  1. .NET 云原生架构师训练营(模块二 基础巩固 MongoDB 写入和查询)--学习笔记

    2.5.3 MongoDB -- 写入和查询 写入 查询 查找操作符 逻辑操作符 其他 嵌套对象 数组 游标方法 写入 https://docs.mongodb.com/manual/tutorial ...

  2. 单细胞分析实录(8): 展示marker基因的4种图形(一)

    今天的内容讲讲单细胞文章中经常出现的展示细胞marker的图:tsne/umap图.热图.堆叠小提琴图.气泡图,每个图我都会用两种方法绘制. 使用的数据来自文献:Single-cell transcr ...

  3. DEDECMS自动编号(序号)autoindex属性(转)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 让织梦dedecms autoindex,itemindex 从0到1开始的办法! 1 2 3 [field:global name=autoin ...

  4. C#扫盲篇(四):.NET Core 的异步编程-只讲干货(async,await,Task)

    关于async,await,task的用法和解释这里就不要说明了,网上一查一大堆.至于为啥还要写这篇文章,主要是其他文章水分太多,不适合新手学习和理解.以下内容纯属个人理解,如果有误,请高手指正.本文 ...

  5. 分贝单位的本质(下半篇),dBm、dBFS、dBV的妙处你想象不到

    上半篇讲到了声音分贝的概念, 对于声音的单位:dB SPL和dB SIL,有兴趣了解并推算的朋友,可以点击以下链接(PC端效果更佳) http://www.sengpielaudio.com/calc ...

  6. Jenkins上实现Python + Jenkins + Allure Report 接口自动化测试持续集成,最终测试报告用allure-report进行展示

    项目介绍 接口功能测试应用:http://www.weather.com.cn/data/cityinfo/<city_code>.html 测试功能:获取对应城市的天气预报 源码:Pyt ...

  7. sa-token 之权限验证

    权限验证 核心思想 所谓权限验证,验证的核心就是当前账号是否拥有一个权限码 有:就让你通过.没有:那么禁止访问 再往底了说,就是每个账号都会拥有一个权限码集合,我来验证这个集合中是否包括我需要检测的那 ...

  8. win10/windows 安装Pytorch

    https://pytorch.org/get-started/locally/ 去官网,选择你需要的版本. 把 pip install torch==1.5.0+cu101 torchvision= ...

  9. kubernets之从应用访问pod元数据以及其他资源

    一  downwardAPI的应用 1.1  前面我们介绍了如何通过configmap以及secret将配置传入到pod的容器中,但是传递的这些都是预先能够安排和只晓得,对于那些只有当pod创建起来之 ...

  10. VB基础总结

    前段时间用VB写了一个简单窗口小应用,久了不碰VB,都忘了,下面用思维导图简单总结了一些基础的东西,方便以后快速查阅.