Commit 2ebffc67 authored by yanzg's avatar yanzg

分布式幂等性判断

parent 267ee8d8
...@@ -35,6 +35,10 @@ public class CacheLock implements Runnable { ...@@ -35,6 +35,10 @@ public class CacheLock implements Runnable {
* 关键字 * 关键字
*/ */
private String key; private String key;
/**
* 执行函数
*/
private Runnable funcWait;
/** /**
* 执行函数 * 执行函数
*/ */
...@@ -54,7 +58,7 @@ public class CacheLock implements Runnable { ...@@ -54,7 +58,7 @@ public class CacheLock implements Runnable {
* @param key * @param key
*/ */
public CacheLock(Cache cache, int lockTime, int waitUnitTime, String key) { public CacheLock(Cache cache, int lockTime, int waitUnitTime, String key) {
this(cache, lockTime, waitUnitTime, key, null); this(cache, lockTime, waitUnitTime, key, null, null);
} }
/** /**
...@@ -67,10 +71,25 @@ public class CacheLock implements Runnable { ...@@ -67,10 +71,25 @@ public class CacheLock implements Runnable {
* @param func * @param func
*/ */
public CacheLock(Cache cache, int lockTime, int waitUnitTime, String key, Runnable func) { public CacheLock(Cache cache, int lockTime, int waitUnitTime, String key, Runnable func) {
this(cache, lockTime, waitUnitTime, key, null, func);
}
/**
* 构造函数
*
* @param cache
* @param lockTime
* @param waitUnitTime
* @param key
* @param funcWait  等待执行
* @param func
*/
public CacheLock(Cache cache, int lockTime, int waitUnitTime, String key, Runnable funcWait, Runnable func) {
this.cache = cache; this.cache = cache;
this.lockTime = lockTime; this.lockTime = lockTime;
this.waitUnitTime = waitUnitTime; this.waitUnitTime = waitUnitTime;
this.key = key; this.key = key;
this.funcWait = funcWait;
this.func = func; this.func = func;
} }
...@@ -103,11 +122,9 @@ public class CacheLock implements Runnable { ...@@ -103,11 +122,9 @@ public class CacheLock implements Runnable {
Runnable runnable = new Runnable() { Runnable runnable = new Runnable() {
@Override @Override
public void run() { public void run() {
CacheLock.this.func.run(); funcRun();
runFlag = true;
} }
}; };
do { do {
// 开启唯一性锁,防止多人运行同一关键字的函数 // 开启唯一性锁,防止多人运行同一关键字的函数
cache.tryLockAndRun(key, lockTime, TimeUnit.SECONDS, runnable); cache.tryLockAndRun(key, lockTime, TimeUnit.SECONDS, runnable);
...@@ -119,6 +136,16 @@ public class CacheLock implements Runnable { ...@@ -119,6 +136,16 @@ public class CacheLock implements Runnable {
} while (!runFlag); } while (!runFlag);
} }
private void funcRun() {
if (this.waitCount > 0 && this.funcWait != null) {
funcWait.run();
}
if (this.func != null) {
func.run();
}
runFlag = true;
}
/** /**
* 开始执行,每个关键字会等待其他关键字执行完成后执行 * 开始执行,每个关键字会等待其他关键字执行完成后执行
* *
...@@ -129,7 +156,20 @@ public class CacheLock implements Runnable { ...@@ -129,7 +156,20 @@ public class CacheLock implements Runnable {
* @param func * @param func
*/ */
public static void run(Cache cache, int lockTime, int waitUnitTime, String key, Runnable func) { public static void run(Cache cache, int lockTime, int waitUnitTime, String key, Runnable func) {
CacheLock lock = new CacheLock(cache, lockTime, waitUnitTime, key, func); run(cache, lockTime, waitUnitTime, key, null, func);
}
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*
* @param cache
* @param lockTime
* @param waitUnitTime
* @param key
* @param func
*/
public static void run(Cache cache, int lockTime, int waitUnitTime, String key, Runnable funcWait, Runnable func) {
CacheLock lock = new CacheLock(cache, lockTime, waitUnitTime, key, funcWait, func);
lock.run(); lock.run();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment