惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Troy Hunt's Blog
GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
Engineering at Meta
Engineering at Meta
The Register - Security
The Register - Security
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Fortinet All Blogs
博客园 - 司徒正美
博客园 - 聂微东
T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
Microsoft Security Blog
Microsoft Security Blog
Jina AI
Jina AI
A
About on SuperTechFans
Y
Y Combinator Blog
N
Netflix TechBlog - Medium
V
V2EX
I
InfoQ
WordPress大学
WordPress大学
小众软件
小众软件
The Cloudflare Blog
Recent Announcements
Recent Announcements
U
Unit 42
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
Vercel News
Vercel News
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
M
MIT News - Artificial intelligence
Project Zero
Project Zero
美团技术团队
L
LangChain Blog
S
Security @ Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
W
WeLiveSecurity
S
Securelist
H
Hacker News: Front Page
K
Kaspersky official blog
Martin Fowler
Martin Fowler
Know Your Adversary
Know Your Adversary
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
J
Java Code Geeks
P
Proofpoint News Feed
有赞技术团队
有赞技术团队
Google Online Security Blog
Google Online Security Blog
D
DataBreaches.Net

博客园 - 一路前行

RestTemplate之GET和POST调用和异步回调 ConditionalOnProperty fastjson序列化乱序问题 IE中的console.log spring boot 中添加mongodb支持 javacript onclick事件中传递对象参数 Java Lambda 表达式 对 Map 对象排序 比较两个list对象是否相同 ubuntu redis 自启动配置文件(关机有密码) 网站架构之性能优化(转) Json转Java Bean spring mvc 4 校验 java @ResponseBody返回值中去掉NULL字段 合并两个java bean对象非空属性(泛型) spring mvc 删除返回字符串中值为null的字段 ubuntu下postgreSQL安装配置 十大Intellij IDEA快捷键(转) C#封装好的Win32API IntelliJ Idea 常用快捷键列表
spring中订阅redis键值过期消息通知
一路前行 · 2017-01-04 · via 博客园 - 一路前行

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