国产精品午夜免费观看网站_国产精品禁18久久久夂久_晚上开车又疼又叫的视频_尤物麻豆av在线_善良迷人的女教师2中文

針對RedisTemplate分布式鎖實現(xiàn)WatchDog


(相關資料圖)

在此之前,去看了下Redission的實現(xiàn)原理,不過在開發(fā)中,原本的代碼使用RedistTemplate實現(xiàn)的,也不太想換,所以我想了下,不如自己實現(xiàn)要給WatchDog。

我的想法是,在用戶加上鎖的時候開啟個定時任務線程,并且在定時任務中,判斷原線程isAlive狀態(tài)進行“續(xù)命”。

下面是代碼(在這里面為了方便,未使用的是HuTool.CornUtil來實現(xiàn)動態(tài)定時任務):

/** * Title * * @ClassName: LockUtil * @Description:鎖工具類,通過內部枚舉類實現(xiàn)單例,防止反射攻擊 * @author: Karos * @date: 2023/1/4 0:17 * @Blog: https://www.wzl1.top/ */package cn.katool.lock;import cn.hutool.core.util.BooleanUtil;import cn.hutool.core.util.ObjectUtil;import cn.hutool.cron.CronUtil;import cn.hutool.cron.task.Task;import cn.katool.Config.LockConfig;import cn.katool.Exception.ErrorCode;import cn.katool.Exception.KaToolException;import cn.katool.other.MethodIntefaceUtil;import com.qiniu.util.StringUtils;import lombok.SneakyThrows;import lombok.extern.slf4j.Slf4j;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Scope;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;import org.springframework.util.ObjectUtils;import javax.annotation.Resource;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.TimeUnit;@Component@Scope("prototype")@Slf4jpublic class LockUtil {        @Resource        RedisTemplate redisTemplate;        private LockUtil(){        }        private static boolean isOpenCorn=false;        /**         * 帶看門狗機制上鎖         * @param lockObj         * @return         */        public boolean DistributedLock(Object lockObj){                try {                        return DistributedLock(lockObj,null,null);                } catch (KaToolException e) {                        throw new RuntimeException(e);                }        }        @Resource        LockConfig lockConfig;        //加鎖        /**         * 無看門狗機制上鎖         * @param obj         * @param exptime         * @param timeUnit         * @return         * @throws KaToolException         */        public boolean DistributedLock(Object obj,Long exptime,TimeUnit timeUnit) throws KaToolException {                if (ObjectUtil.isEmpty(obj)){                        throw new KaToolException(ErrorCode.PARAMS_ERROR," Lock=> 傳入obj為空");                }                Boolean isDelay=false;                if (ObjectUtil.isAllEmpty(exptime,timeUnit)){                        isDelay=true;                }                if(ObjectUtil.isEmpty(exptime)){                        exptime= lockConfig.getInternalLockLeaseTime();;                }                if (ObjectUtils.isEmpty(timeUnit)){                        timeUnit=lockConfig.getTimeUnit();                }                //線程被鎖住了,就一直等待                DistributedAssert(obj);                Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("Lock:"+obj.toString(), "1", exptime, timeUnit);                log.info("katool=> LockUntil => DistributedLock:{} value:{} extime:{} timeUnit:{}",obj.toString(), "1", exptime, timeUnit);                //實現(xiàn)看門狗                if (isDelay){                        if (LockUtil.isOpenCorn==false){                                //如果同一個項目之前打開過,那么先關閉,避免重復啟動                                CronUtil.stop();                                //支持秒級別定時任務                                CronUtil.setMatchSecond(true);                                //定時服務啟動                                CronUtil.start();                                LockUtil.isOpenCorn=true;                        }                        Thread thread = Thread.currentThread();                        TimeUnit finalTimeUnit = timeUnit;                        Long finalExptime = exptime;                        class TempClass{                                public String scheduleId;                        }                        final TempClass tempClass = new TempClass();                        tempClass.scheduleId=CronUtil.schedule("0/30 * * * * ?", new Task() {                                @SneakyThrows                                @Override                                public void execute() {                                        boolean alive = thread.isAlive();                                        if (alive) {                                                delayDistributedLock(obj, finalExptime>=3?(finalExptime / 3):finalExptime, finalTimeUnit);                                                return;                                        } else {                                                if (tempClass.scheduleId==null||"".equals(tempClass.scheduleId)){                                                        return;                                                }                                                CronUtil.remove(tempClass.scheduleId);                                                DistributedUnLock(obj);                                                return;                                        }                                }                        });                }                return BooleanUtil.isTrue(aBoolean);        }        //檢鎖        public void DistributedAssert(Object obj) throws KaToolException {                if (ObjectUtils.isEmpty(obj)){                        throw new KaToolException(ErrorCode.PARAMS_ERROR," Lock=> 傳入obj為空");                }                while(true){                        Object o = redisTemplate.opsForValue().get("Lock:" + obj.toString());                        if (ObjectUtils.isEmpty(o))return;                }        }        //延期        public boolean delayDistributedLock(Object obj,Long exptime,TimeUnit timeUnit) throws KaToolException {                if (ObjectUtils.isEmpty(obj)){                        throw new KaToolException(ErrorCode.PARAMS_ERROR," Lock=> 傳入obj為空");                }                Boolean aBoolean = redisTemplate.opsForValue().setIfPresent("Lock:"+obj.toString(), "1", exptime, timeUnit);                log.info("katool=> LockUntil => delayDistributedLock:{} value:{} extime:{} timeUnit:{}",obj.toString(), "1", exptime, timeUnit);                return BooleanUtil.isTrue(aBoolean);        }        //釋放鎖        public boolean DistributedUnLock(Object obj) throws KaToolException {                if (ObjectUtils.isEmpty(obj)){                        throw new KaToolException(ErrorCode.PARAMS_ERROR," Lock=> 傳入obj為空");                }                Boolean aBoolean = redisTemplate.delete("Lock:" + obj.toString());                log.info("katool=> LockUntil => unDistributedLock:{} isdelete:{} ",obj.toString(),true);                return BooleanUtil.isTrue(aBoolean);        }        //利用枚舉類實現(xiàn)單例模式,枚舉類屬性為靜態(tài)的        private enum SingletonFactory{                Singleton;                LockUtil lockUtil;                private SingletonFactory(){                        lockUtil=new LockUtil();                }                public LockUtil getInstance(){                        return lockUtil;                }        }        @Bean("LockUtil")        public static LockUtil getInstance(){                return SingletonFactory.Singleton.lockUtil;        }}
關鍵詞:
圖片版權歸原作者所有,如有侵權請聯(lián)系我們,我們立刻刪除。
新化月報網(wǎng)報料熱線:886 2395@qq.com

相關文章

你可能會喜歡

最近更新

推薦閱讀
南澳县| 田东县| 长沙市| 建昌县| 洛隆县| 饶阳县| 兴和县| 雅安市| 汝城县| 兴安县| 芦溪县| 溧阳市| 镇远县| 老河口市| 桐城市| 济源市| 烟台市| 遂昌县| 麻江县| 博湖县| 海原县| 绥滨县| 常山县| 平泉县| 恭城| 英德市| 巫溪县| 潜山县| 苍南县| 饶平县| 丰台区| 万安县| 滦南县| 威海市| 达州市| 田阳县| 马尔康县| 莱州市| 邵武市| 江永县| 陆河县|