1、
@Configuration
public class RedisExpirationConfig {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}
2、
@Component
@Slf4j
public class RedisMessageListener extends KeyExpirationEventMessageListener {
@Autowired
private RedisTemplate<String, String> redisTemplate;
public RedisMessageListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
@Override
public void onMessage(Message message, byte[] pattern) {
log.info("收到 redis 过期异步通知:{}", message.toString());
String redisKey = (String) redisTemplate.getValueSerializer().deserialize(message.getBody());
log.info("redisKey : {}", redisKey);
if (redisKey.startsWith("rediskey")){
try { //TODO
}catch (Exception exp){
log.info("业务处理失败:" + exp.getMessage());
}
}
}
}