Commit ff2ddd0c authored by yanzg's avatar yanzg

分布式幂等性判断

parent cb0c15c5
...@@ -38,7 +38,24 @@ public class CacheLock implements Runnable { ...@@ -38,7 +38,24 @@ public class CacheLock implements Runnable {
/** /**
* 执行函数 * 执行函数
*/ */
private final Runnable func; private Runnable func;
/**
* 等待次数
*/
private int waitCount;
/**
* 构造函数
*
* @param cache
* @param lockTime
* @param waitUnitTime
* @param key
*/
public CacheLock(Cache cache, int lockTime, int waitUnitTime, String key) {
this(cache, lockTime, waitUnitTime, key, null);
}
/** /**
* 构造函数 * 构造函数
...@@ -57,11 +74,31 @@ public class CacheLock implements Runnable { ...@@ -57,11 +74,31 @@ public class CacheLock implements Runnable {
this.func = func; this.func = func;
} }
/**
* 等待次数
*
* @return
*/
public int getWaitCount() {
return waitCount;
}
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*/
public void run(Runnable func) {
this.func = func;
this.run();
}
/** /**
* 开始执行,每个关键字会等待其他关键字执行完成后执行 * 开始执行,每个关键字会等待其他关键字执行完成后执行
*/ */
@Override @Override
public void run() { public void run() {
if (this.func == null) {
return;
}
// 需要运行的函数 // 需要运行的函数
Runnable runnable = new Runnable() { Runnable runnable = new Runnable() {
@Override @Override
...@@ -76,6 +113,7 @@ public class CacheLock implements Runnable { ...@@ -76,6 +113,7 @@ public class CacheLock implements Runnable {
cache.tryLockAndRun(key, lockTime, TimeUnit.SECONDS, runnable); cache.tryLockAndRun(key, lockTime, TimeUnit.SECONDS, runnable);
// 假如没有运行,则等待50毫秒后继续运行 // 假如没有运行,则等待50毫秒后继续运行
if (!runFlag) { if (!runFlag) {
this.waitCount++;
ThreadHelper.sleep(waitUnitTime); ThreadHelper.sleep(waitUnitTime);
} }
} while (!runFlag); } while (!runFlag);
......
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