使用redis监听key做延迟处理事件

先给redis开启key失效监听模式,在客户端中执行:
config set notify-keyspace-events Ex

配置监听器

@Slf4j
@Service
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
    /**
     * Creates new {@link MessageListener} for {@code __keyevent@*__:expired} messages.
     *
     * @param listenerContainer must not be {@literal null}.
     */
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }
    @Override
    public void onMessage(Message message, byte[] pattern) {
        if (StringUtils.startsWith(message.toString(), RedisKeyCommon.PLATFORM_ORDER_EXPIRATION)) {
            
        }
        super.onMessage(message, pattern);
    }
}

将监听器注入到spring容器中

@Configuration
public class RedisListenerConfiguration {
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory) {
        RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
        listenerContainer.setConnectionFactory(redisConnectionFactory);
        return listenerContainer;
    }
    @Bean
    public KeyExpirationEventMessageListener keyExpirationEventMessageListener(RedisMessageListenerContainer redisMessageListenerContainer) {
        return new RedisKeyExpirationListener(redisMessageListenerContainer);
    }
}

你可能感兴趣的:(使用redis监听key做延迟处理事件)