spring中订阅redis键值过期消息通知
1、首先启用redis通知功能(ubuntu下操作):
编辑/etc/redis/redis.conf文件,添加或启用以下内容(过期通知):
notify-keyspace-events Ex
或者登陆redis-cli之后,输入以下命令:
config set notify-keyspace-events Ex
更多通知详见:http://redis.io/topics/notifications#configuration
2、Java Spring中配置监听
接口类:
import java.io.Serializable;
import java.util.Map; public interface IMessageDelegate {
void handleMessage(String message);
void handleMessage(Map message);
void handleMessage(byte[] message);
void handleMessage(Serializable message);
void handleMessage(Serializable message, String channel);
}
实现类:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import rhxtune.smarthome.api.interfaces.IMessageDelegate;
import java.io.Serializable;
import java.util.Map; @Service
public class DefaultMessageDelegate implements IMessageDelegate {
public static Logger logger = LogManager.getLogger(DefaultMessageDelegate.class.getName()); @Override
public void handleMessage(String message) {
logger.info("handleMessage1:" + message);
} @Override
public void handleMessage(Map message) {
logger.info("handleMessage2:" + message);
} @Override
public void handleMessage(byte[] message) {
logger.info("handleMessage3:" + message);
} @Override
public void handleMessage(Serializable message) {
logger.info("handleMessage4:" + message);
} @Override
public void handleMessage(Serializable message, String channel) {
logger.info("handleMessage5:" + message + channel);
}
}
spring-redis.xml中配置:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:redis="http://www.springframework.org/schema/redis"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis.xsd">
<!--<context:component-scan base-package="rhxtune.smarthome.api.repositorys" />-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"></property>
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="minIdle" value="${redis.minIdle}"></property>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
</bean>
<bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
<property name="hostName" value="${redis.hostname}" />
<property name="port" value="${redis.port}" />
<property name="timeout" value="${redis.timeout}" />
<property name="database" value="${redis.database}" />
<property name="password" value="${redis.password}" />
<property name="usePool" value="true" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<!-- 序列化方式 建议key/hashKey采用StringRedisSerializer。 -->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
<property name="connectionFactory" ref="jedisConnFactory" />
</bean>
<!-- 对string操作的封装 -->
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnFactory" />
</bean>
<!-- 设置redis消息订阅(方式1) -->
<!--<bean id="listener" class="rhxtune.smarthome.api.services.DefaultMessageDelegate" />
<redis:listener-container connection-factory="jedisConnFactory">
<redis:listener ref="listener" method="handleMessage" topic="__keyevent@1__:expired" />
</redis:listener-container>-->
<!-- 设置redis消息订阅(方式2) -->
<bean id="messageListener"
class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean class="rhxtune.smarthome.api.services.DefaultMessageDelegate" />
</constructor-arg>
</bean>
<bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer">
<property name="connectionFactory" ref="jedisConnFactory" />
<property name="messageListeners">
<map>
<entry key-ref="messageListener">
<list>
<bean class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="__keyevent@1__:expired" />
</bean>
<bean class="org.springframework.data.redis.listener.PatternTopic">
<constructor-arg value="*" />
</bean>
<bean class="org.springframework.data.redis.listener.PatternTopic">
<constructor-arg value="'__key*__:*" />
</bean>
</list>
</entry>
</map>
</property>
</bean>
</beans>
更多详情参见:http://docs.spring.io/spring-data/redis/docs/1.7.1.RELEASE/reference/html/#redis:pubsub:subscribe
spring中订阅redis键值过期消息通知的更多相关文章
- python中的Redis键空间通知(过期回调)
介绍 Redis是一个内存数据结构存储库,用于缓存,高速数据摄取,处理消息队列,分布式锁定等等. 使用Redis优于其他内存存储的优点是Redis提供持久性和数据结构,如列表,集合,有序集和散列. 在 ...
- JAVA整合Redis使用redisTemplate清除库中的所有键值对数据
JAVA整合Redis使用redisTemplate清除库中的所有键值对数据,清除所有缓存数据 Set<String> keys = redisTemplate.keys("*& ...
- 7.Redis键值对数据库
1.Redis的安装以及客户端连接 安装:apt-get install redis-server 卸载:apt-get purge --auto-remove redis-server 启动:red ...
- redis键值操作
1.1. redis键值操作 1.1.1. keys patten 查询相应的key 可以精确的查,也可以模糊的查 1.1.1.1. 通配符:* ? [] 在redis里,模糊查询key的时候有3个通 ...
- redis 键值对 有效期设置
redis 键值对 有效期设置redis中可以使用expire命令设置一个键的生存时间, 到时间后redis会自动删除它<-----> 类比于javaweb系统临时数据 过期删除功能 ex ...
- Redis 键的过期删除策略及缓存淘汰策略
前言 Redis缓存淘汰策略与Redis键的过期删除策略并不完全相同,前者是在Redis内存使用超过一定值的时候(一般这个值可以配置)使用的淘汰策略:而后者是通过定期删除+惰性删除两者结合的方式进行内 ...
- 记自己在spring中使用redis遇到的两个坑
本人在spring中使用redis作为缓存时,遇到两个坑,现在记录如下,算是作为自己的备忘吧,文笔不好,望大家见谅: 一.配置文件 <!-- 加载Properties文件 --> < ...
- php usort 按照数组中的某个键值排序
//php usort 按照数组中的某个键值排序 如果第一个参数小于第二个参数 -> 返回小于0的整数如果第一个参数等于于第二个参数 -> 返回等于0的整数如果第一个参数大于于第二个参数 ...
- .NET 获取Get方式URL中的参数键值
在Web开发中,我们常常会涉及到需要获取Get方式URL中的参数键值的情况,这里简单介绍三种方法: 第一种:常用的做法有使用JavaScript获取location.href后用正则表达式匹配获取此U ...
随机推荐
- http中get和post的区别
HTTP定义了与服务器交互的不同方法,最基本的方法是 GET 和 POST. HTTP-GET和HTTP-POST是使用HTTP的标准协议动词,用于编码和传送变量名/变量值对参数,并且使用相关的请求语 ...
- oracle远程连接配置
今日需要对站内所有的空间数据做入库处理,所以在服务器上安装了oracle,在本地需要对其进行连接,在网上查阅了相关资料,整理如下: 远程服务端配置:1. 数据库配置 因为要为外界客户端提供数据服 ...
- 转载:ORA-01438: 值大于为此列指定的允许精度
Number的数据声明如下: 表示 作用 说明Number(p, s) 声明一个定点数 p(precision)为精度,s(scale)表示小数 ...
- jquery插件的引用和扩展应用
引入插件,引用的jquery吆喝开发插件的jquery版本一致,引用插件里面所用的图像img图片等,常用的jquery可以通过对其进行函数描述,直接调用,下例子就是改变特定的颜色,插件的使用是在仿网页 ...
- Linux虚拟机安装(CentOS 6.5,图文详解,需要自查)
Linux虚拟机的安装(图文详解) 下篇会接续Hadoop集群安装(以此为基础) 一.安装准备 VMWorkstation.linux系统镜像(以下以CentOS6.5为例) 二.安装过程详解 关闭防 ...
- PHP实例开发(3)PHP中MVC学习之ThinkPHP
PHP中MVC学习之ThinkPHP 1.什么是MVC MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的是将M和V的实现代码分离 MVC是一个设 ...
- 计算机网络自学之路------IP协议(2)
之前忘记说了,这些内容都是边看视频边写的,视频出自西安交通大学. 本期说的是IP分组转发的内容,里面详细介绍了IP协议跟ARP协议,最后再给出一个例子,说明这两个协议在网络中传输的具体过程. 1)Ip ...
- 汇编语言基础 debug的使用
-r 查看,改变CPU寄存器的内容 -r 加上寄存器名 在:后输入要写入的数据后 完成更改 debug 随着CS IP的改变 对应的汇编指令也不同 -r ip -r cs修改 ip cs 的值 d 段 ...
- mysql之替换字符串
update `wp_posts` set `post_content`=REPLACE(`post_content`,'localhost/linkcp','www.linkcp.cn') wher ...
- html框架
1.框架的概念 框架:将一个浏览器窗口划分成若干个小窗口 2.框架集合框架页 框架集<frameset>:主要用来划分窗口的. 框架页<frame>:主要用来指定窗口默认显示的 ...