package com.muyu.event.redis;

import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * RedisKeyExpirationListener
 *
 * @author Yangle
 * Date 2024/6/21 14:12
 */
@Log4j2
@Configuration
public class RedisKeyExpirationListener  extends KeyExpirationEventMessageListener {

        @Autowired
        private RabbitTemplate rabbitTemplate;

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

    /**
     * 针对redis数据失效事件，进行数据处理
     * @param message 失效的key
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        log.info("过期redis数据：" + message.toString());
        try {
            String key = message.toString();
            log.info("过期redis数据：" + key);
            rabbitTemplate.convertAndSend("disconnect_connect","resolve_time",key);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【修改支付订单过期状态异常】：" + e.getMessage());
        }
    }


}
