[Java] 纯文本查看 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- redis配置文件 -->
<context:property-placeholder location="classpath:/redis.properties"
ignore-unresolvable="true" />
<!-- 配置连接池参数 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- 配置连接工厂 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<!-- 如果redis没有开启验证,password不需要配置 -->
<property name="password" value="${redis.passwd}"></property>
<property name="timeout" value="${redis.timeout}"></property>
<property name="poolConfig" ref="poolConfig" />
</bean>
<!-- 配置redis消息订阅 -->
<bean id="messageListener"
class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<!-- 自定义消息处理类 -->
<bean class="com.mote.redis.listener.KeyExpiredNotice" />
</constructor-arg>
</bean>
<bean id="redisContainer"
class="org.springframework.data.redis.listener.RedisMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="messageListeners">
<map>
<entry key-ref="messageListener">
<list>
<!-- 通配符:匹配消息通知类型 -->
<bean class="org.springframework.data.redis.listener.PatternTopic">
<constructor-arg value="__key*__:expired" />
</bean>
</list>
</entry>
</map>
</property>
</bean>
</beans>
[Java] 纯文本查看 复制代码
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;
@Component
public class KeyExpiredNotice extends MessageListenerAdapter {
/**
* msg : 通知消息,内含 key
* topic : 管道类型
*/
@Override
public void onMessage(Message msg, byte[] topic) {
String key = new String(msg.getBody());
// TODO 根据业务书写处理逻辑
System.out.println(key);
System.out.println(new String(topic));
}
}