利用redis的键空间失效通知处理超时案件

利用redis的键空间失效通知处理超时案件

package com.yymt.modules.controller.accountmanage;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yymt.common.exception.RRException;
import com.yymt.common.exception.ResultEnum;
import com.yymt.common.utils.Constant;
import com.yymt.entity.api.accountmanage.AccountManageEntity;
import com.yymt.service.api.accountmanage.AccountManageService;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * 利用redis的键空间失效通知处理超时案件
 * 找到 notify-keyspace-events Ex 这一行 取消行前边的注释 开启redis的键空间失效通知
 * https://blog.csdn.net/huangbaoling66/article/details/102776551?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~first_rank_v2~rank_v25-1-102776551.nonecase&utm_term=redis%20%E7%9B%91%E5%90%AC%E5%88%A0%E9%99%A4%E4%BA%8B%E4%BB%B6
 * <p>
 * 
 */
//@Component
public class AccountOverTimeListener extends KeyExpirationEventMessageListener {

    @Autowired
    private AccountManageService accountManageService;

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

    // 这里对超时的案件进行处理,在AccountManageService处理案件后要删除redis中的key
    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 根据 message.toString() 可以获取失效的key
        String expiredKey = message.toString();
        if (expiredKey.startsWith("account:overtime")) {
            System.err.println("------------------ 台账管理案件超时未处理; key = " + expiredKey);
            // 获取key:account:overtime:主要问题ID:数据记录ID
            String[] expiredKeys = expiredKey.split(":");
            if (4 == expiredKeys.length) {
                // 获取案件的记录ID
                String accountId = expiredKeys[3];
                // 案件状态:(0-待派发,1-未立案,2-立案待处理,3-待核实,4-已结案,5-未整改,6-超时案件)
                AccountManageEntity accountManageEntity = accountManageService.selectOne(
                        new EntityWrapper<AccountManageEntity>()
                                .eq("account_id", accountId)
                                .eq("case_status", Constant.AccountStatus.ON_Handle.getValue())
                                .eq("is_delete", 0)
                );
                if (null != accountManageEntity) {
                    // 将案件的状态改为 超时案件 状态
                    accountManageEntity.setCaseStatus(Constant.AccountStatus.TAKING_OVER_TIME.getValue());
                    // 修改案件 是否超时 为 超时(0-否,1-是)
                    accountManageEntity.setCaseOverTime(1);
                    boolean updateById = accountManageService.updateById(accountManageEntity);
                    if (!updateById) {
                        System.err.println("AccountOverTimeListener类中修改台账管理记录ID为 " + accountId + " 的案件状态为超时案件失败");
                        throw new RRException("修改台账管理记录ID为 " + accountId + " 的案件状态为超时案件失败");
                    }
                } else {
                    throw new RRException("数据库中台账管理不存在ID为 " + accountId + " 的数据记录");
                }
            } else {
                throw new RRException("------------------ 台账管理中 " + expiredKey + " 的格式不正确");
            }
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值