SpringBoot + Redis order to achieve non-payment automatically canceled 30 minutes

Redis modify the file redis.conf

Found notify-keyspace-events Ex comment this line to cancel the line in front of the open space bond failure notification

Import dependence in pom.xml SpringBoot project

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

New Listener in the project and inherit from KeyExpirationEventMessageListener

@Component 
@ SLF4J 
public  class RedisKeyExpirationListener the extends KeyExpirationEventMessageListener { 

    @Autowired 
    Private OrderService orderService; 

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

    @Override 
    public  void the onMessage (the Message Message, byte [] pattern) {
         // the user to do his own business processes can pay attention to message.toString () may fail to obtain the Key 
        String expiredKey = message.toString (); 
        log.info ( "------------------ Redis Key failure; key = "+expiredKey);
         IF (expiredKey.startsWith (GlobalConstant.RedisPrefixKey.ORDER_PREFIX)) {
             // Get Order orderNo 
            String orderNo = expiredKey.substring (expiredKey.lastIndexOf ( ":") +. 1 );
             // will be paid to the order canceled (unpaid overtime) 
            orderService.orderPaidTimeout (orderNo); 
        } 
    } 
}

New configuration class

@Configuration
public class RedisListenerConfig {

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }

}

To order when generating a key into orderNo redis, when setting invalid length of 30 minutes

Redis delete the key after a successful payment orders, otherwise the payment is successful candidate will be canceled

Guess you like

Origin www.cnblogs.com/yangwanhao/p/12638410.html