浏览代码

单纯加锁释放锁

xiatian 5 年之前
父节点
当前提交
18bb3cdb6e
共有 1 个文件被更改,包括 36 次插入0 次删除
  1. 36 0
      src/main/java/cn/com/qmth/examcloud/web/helpers/SequenceLockHelper.java

+ 36 - 0
src/main/java/cn/com/qmth/examcloud/web/helpers/SequenceLockHelper.java

@@ -76,6 +76,7 @@ public class SequenceLockHelper {
 			throw new SequenceLockException("请求等待,请稍后重试!");
 		}
 	}
+	
 
 	/**
 	 * 释放锁<br>
@@ -96,5 +97,40 @@ public class SequenceLockHelper {
 			}
 		}
 	}
+	
+	   /**
+     * 获取顺序请求锁<br>
+     * 
+     * @param args
+     * @throws SequenceLockException
+     *             获取锁失败时抛出异常
+     */
+    public static void getLockSimple(Object... args) throws SequenceLockException {
+
+        String key = LOCK_PREFIX + StringUtils.join(Arrays.asList(args), "_");
+
+        int timeout = 60 * 2;
+
+        if (!getRedisClient().setIfAbsent(key, ThreadLocalUtil.getTraceId(), timeout)) {
+            Long expire = getRedisClient().getExpire(key, TimeUnit.SECONDS);
+            if (null == expire || expire > timeout) {
+                getRedisClient().delete(key);
+            }
+
+            throw new SequenceLockException("请求等待,请稍后重试!");
+        }
+    }
+    
+    /**
+     * 释放锁<br>
+     * 接口调用不要调用此方法释放锁,此锁会在拦截器<{@link SeqlockInterceptor}中释放.
+     *
+     * @author WANGWEI
+     * @param args
+     */
+    public static void releaseLockSimple(Object... args) {
+        String key = LOCK_PREFIX + StringUtils.join(Arrays.asList(args), "_");
+        getRedisClient().expire((String) key, 100, TimeUnit.MILLISECONDS);
+    }
 
 }