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 ...
随机推荐
- 前端:js
JavaScript: 参考文章: http://www.cnblogs.com/wupeiqi/articles/5369773.html http://javascript.ruanyifeng. ...
- bash 取文件特定行
比如,想要取某文件10-20行 可以用sed sed -n '10,20p' XXX.txt 非常方便!
- Ionic学习笔记三 Gulp在ionic中的使用
简介 Gulp是一个基于流的自动化构建器. 安装 npm config set registry http://registry.npm.taobao.org ---最好用国内源 npm instal ...
- /etc/default/grub 部分配置选项设置
GRUB_HIDDEN_TIMEOUT=0 此配置将影响菜单显示.若设置此选项,将在此时间内隐藏菜单而显示引导画面. 菜单将会被隐藏,除非在此行开头加上一个 # 符号.(# GRUB_HIDDEN_T ...
- 实现TCP、UDP相互通信及应用
实验名称 Socket编程综合实验(1) 一.实验目的: 1.理解进程通信的原理及通信过程 2.掌握基于TCP和UDP的工作原理 3.掌握基本的Socket网络编程原理及方法 二.实验内容 1.掌握 ...
- css伪类选择器及伪元素选择器
1.类选择器 在css中可以使用类选择器把相同的元素定义成不同的样式.比如: 结果如下: 标题背景未变 2.伪类选择器 类选择器和伪类选择器的区别在于,类选择器我们定义的,而伪类选择器是CSS中已经定 ...
- Python-Mac 安装 PyQt4
环境: 系统: OS X 10.11.4 Python: 2.7.10 1.安装 Qt brew install qt 测试安装结果,需要正确找到 qmake 的路径 qmake 2.安装 SIP 下 ...
- XML 增删查改
<?xml version="1.0" encoding="utf-8"?> <users> <person name=" ...
- Windows Redis使用
一.Redis 的安装 1.Redis 下载 Windows 版本下载:https://github.com/dmajkic/redis/downloads 2.解压到 C:\redis-2.4.5- ...
- oracle for循环查找结果
-- Call the procedure begin ' ) loop dbms_output.put_line('v_rlt = '||v_rlt.ID||v_rlt.inspection_no) ...