This article is mainly an extension of the previous article, which stated that there are three ways to deal with a time-out order, and this article documents the second solution

Address of previous article

Log the use of Rabbit MQ to process time-out orders

So let’s get started

  1. Modified the configurations of Redis-related events, and added notify-keyspace-events Ex

    1. Modify the redis.conf file directly
    2. Or use the RedisDesktopManager remote connection tool Open Console button, using the command lineCONFIG SET notify-keyspace-events ExTo solveHowever, you must restart the Redis service after both methods are set. Otherwise, the Settings do not take effect
  2. Joining maven dependencies

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <scope>compile</scope> The < version > 2.9.7 < / version > < / dependency >Copy the code
  1. Define the configuration RedisListenerConfig
@Configuration
public class RedisListenerConfig {

    @Autowired
    private RedisTemplate redisTemplate;

    /** * handle garbled characters *@return* /
    @Bean
    public RedisTemplate redisTemplateInit(a) {
        // Key serialization
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        / / val instantiation
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        return redisTemplate;
    }

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        returncontainer; }}Copy the code
  1. Define listener, realize KeyExpirationEventMessageListener interface
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /** * Data processing for redis data failure event *@param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        // The user can do their own business. Note that message.tostring () can fetch invalid keys
        String expiredKey = message.toString();
        if(expiredKey.startsWith("test:")) {// If the key starts with test:, process it
            System.out.println(expiredKey);
            String substring = expiredKey.split(":") [1]; / / remove orderNo
            System.out.println("-- -- -- -- -- -!!!!!!!!!!" + substring + "!!! -- -- -- -- --"); }}}Copy the code
  1. Test if it works
public class Test(a){ &emsp; &emsp; &emsp; &emsp;@Resource&emsp; &emsp; &emsp; &emsp;privateRedisTemplate redisTemplate; &emsp; &emsp; &emsp; &emsp; redisTemplate.opsForValue().set("test:123"."dadada" ,25, TimeUnit.SECONDS );
}
Copy the code

After enabling the debug function, the system can perform the callback 30 seconds later

This article refer to

Why not apply this kind of method, explained in the previous article, so it is no longer explain it again, the purpose of writing this article is to prove that the method is feasible, just because don’t fit so there is no choice as the solution of our project, interested friends went to see my previous article, article links at the start of this article have oh