diff --git a/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/lock/RedisLock.java b/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/lock/RedisLock.java index daedf56..472dad5 100644 --- a/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/lock/RedisLock.java +++ b/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/lock/RedisLock.java @@ -55,7 +55,7 @@ public class RedisLock { private String identifier; /** 线程锁 */ - private AtomicInteger atomicInteger; + private final AtomicInteger atomicInteger; /** * 构造函数 diff --git a/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/lock/RedisLockImpl.java b/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/lock/RedisLockImpl.java index 9038c47..c630ee8 100644 --- a/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/lock/RedisLockImpl.java +++ b/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/lock/RedisLockImpl.java @@ -15,6 +15,7 @@ */ package org.opsli.plugins.redis.lock; +import cn.hutool.core.thread.ThreadUtil; import lombok.extern.slf4j.Slf4j; import org.opsli.plugins.redis.RedisLockPlugins; import org.opsli.plugins.redis.RedisPlugin; @@ -82,6 +83,9 @@ public class RedisLockImpl implements RedisLockPlugins { */ @Override public boolean unLock(RedisLock redisLock) { + if(redisLock == null){ + return false; + } try { List keys = Collections.singletonList(LOCK_NAME_PREFIX + redisLock.getLockName()); Long ret = (Long) redisPlugin.callScript(RedisScriptsEnum.REDIS_UN_LOCK, keys, @@ -120,11 +124,7 @@ public class RedisLockImpl implements RedisLockPlugins { if (1 == ret){ acquired = true; } else { - try { - Thread.sleep(10); - } catch (InterruptedException ignored) { - log.error(ignored.getMessage(),ignored); - } + ThreadUtil.sleep(10); } } redisLock.setIdentifier(identifier); @@ -141,27 +141,25 @@ public class RedisLockImpl implements RedisLockPlugins { * @return boolean */ private void lockDog(RedisLock redisLock) { + if(redisLock == null){ + return; + } Thread t = new Thread(()->{ try { // 倒计时前续命 long countDownTime = 3000L; // 锁释放时间 long lockTimeOutEnd = System.currentTimeMillis() + redisLock.getLockTimeOut(); - boolean dogFlag = true; // 看门狗检测 当前线程是否还存活 - while (dogFlag) { + while (true) { int lock = redisLock.threadGetLock(); if(lock <= 0){ - dogFlag = false; // 再一次确定 解锁 防止线程差 最后加锁 this.unLock(redisLock); break; } - try { - Thread.sleep(10); - } catch (InterruptedException ignored) { - log.error(ignored.getMessage(),ignored); - } + + ThreadUtil.sleep(10); // 如果 距离倒计时 前 2000 毫秒还没有动作 则执行续命 if((System.currentTimeMillis()+countDownTime) >= lockTimeOutEnd){ @@ -189,4 +187,13 @@ public class RedisLockImpl implements RedisLockPlugins { private String getInfo(String name,RedisLock redisLock){ return name + " 锁名称: "+redisLock.getLockName()+" 锁凭证: "+redisLock.getIdentifier(); } + + + public static void main(String[] args) { + int count = 0; + while (count <= 10) { + System.out.println(count); + count++; + } + } } diff --git a/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/msg/RedisMsg.java b/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/msg/RedisMsg.java index 673f21a..921cfb3 100644 --- a/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/msg/RedisMsg.java +++ b/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/msg/RedisMsg.java @@ -35,8 +35,8 @@ public enum RedisMsg implements BaseMsg { ; - private int code; - private String message; + private final int code; + private final String message; RedisMsg(int code, String message){ this.code = code; diff --git a/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/scripts/enums/RedisScriptsEnum.java b/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/scripts/enums/RedisScriptsEnum.java index b4f1cea..a504fca 100644 --- a/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/scripts/enums/RedisScriptsEnum.java +++ b/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/scripts/enums/RedisScriptsEnum.java @@ -32,7 +32,7 @@ public enum RedisScriptsEnum { ; /** 脚本路径 */ - private String path; + private final String path; RedisScriptsEnum(String path){ this.path = path;