diff --git a/CHANGELOG.md b/CHANGELOG.md index f847127..50453c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,3 +26,9 @@ | 序号 | 变更类型 | 说明 | 时间 | 备注 | |:---|:---|:---|:---|:--| | 1 | A | redis 锁独立,便于使用 | 2021-12-08 14:45:40 | | + +# release_0.0.4 + +| 序号 | 变更类型 | 说明 | 时间 | 备注 | +|:---|:---|:---|:---|:--| +| 1 | A | 简单锁的实现,优化 redisLock 实现策略 | 2022-04-17 14:45:40 | | diff --git a/README.md b/README.md index b31b542..a34217c 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ # 变更日志 -> [变更日志](doc/CHANGELOG.md) +> [变更日志](CHANGELOG.md) # 快速开始 @@ -32,7 +32,7 @@ maven 3.x+ com.github.houbb lock-core - ${最新版本} + 0.0.4 ``` @@ -45,7 +45,7 @@ Jedis jedis = new Jedis("127.0.0.1", 6379); IOperator operator = new JedisOperator(jedis); // 获取锁 -ILock lock = LockRedisBs.newInstance().operator(operator).lock(); +ILock lock = LockBs.newInstance(operator).lock(); try { boolean lockResult = lock.tryLock(); diff --git a/lock-api/pom.xml b/lock-api/pom.xml index 796cdd6..6a45c4f 100644 --- a/lock-api/pom.xml +++ b/lock-api/pom.xml @@ -5,11 +5,11 @@ lock com.github.houbb - 0.0.3 + 0.0.4 4.0.0 lock-api - \ No newline at end of file + diff --git a/lock-api/src/main/java/com/github/houbb/lock/api/support/IOperator.java b/lock-api/src/main/java/com/github/houbb/lock/api/support/IOperator.java index 293bda2..7e13f7d 100644 --- a/lock-api/src/main/java/com/github/houbb/lock/api/support/IOperator.java +++ b/lock-api/src/main/java/com/github/houbb/lock/api/support/IOperator.java @@ -30,4 +30,12 @@ public interface IOperator { */ boolean unlock(String lockKey, String requestId); + /** + * 清空过期的锁 + * + * 避免单个线程 unlock 失败,定时移除过期的锁。 + * @since 0.0.4 + */ + void clearExpireLock(); + } diff --git a/lock-core/pom.xml b/lock-core/pom.xml index 981938c..fdc06ea 100644 --- a/lock-core/pom.xml +++ b/lock-core/pom.xml @@ -5,7 +5,7 @@ lock com.github.houbb - 0.0.3 + 0.0.4 4.0.0 diff --git a/lock-core/src/main/java/com/github/houbb/lock/core/bs/LockBs.java b/lock-core/src/main/java/com/github/houbb/lock/core/bs/LockBs.java new file mode 100644 index 0000000..e439fdc --- /dev/null +++ b/lock-core/src/main/java/com/github/houbb/lock/core/bs/LockBs.java @@ -0,0 +1,111 @@ +package com.github.houbb.lock.core.bs; + +import com.github.houbb.heaven.util.common.ArgUtil; +import com.github.houbb.id.api.Id; +import com.github.houbb.id.core.core.Ids; +import com.github.houbb.lock.api.core.ILock; +import com.github.houbb.lock.api.support.IOperator; +import com.github.houbb.lock.core.support.simple.SimpleLock; +import com.github.houbb.wait.api.IWait; +import com.github.houbb.wait.core.Waits; + +/** + * 锁引导类 + * + * @author binbin.hou + * @since 0.0.4 + */ +public final class LockBs { + + private LockBs(){} + + /** + * 清空初始化延迟时间 + * @since 0.0.4 + */ + private long clearInitDelaySeconds = 60; + + /** + * 清空初始化周期 + * @since 0.0.4 + */ + private long clearPeriodSeconds = 60; + + /** + * 是否启用清空任务 + * @since 0.0.4 + */ + private boolean enableClearTask = true; + + /** + * 锁等待 + * @since 0.0.1 + */ + protected IWait waits = Waits.threadSleep(); + + /** + * 标识策略 + * @since 0.0.4 + */ + protected Id id = Ids.uuid32(); + + /** + * 操作策略 + * @since 0.0.4 + */ + protected IOperator operator; + + public static LockBs newInstance(final IOperator operator) { + return new LockBs().operator(operator); + } + + public LockBs clearInitDelaySeconds(long clearInitDelaySeconds) { + this.clearInitDelaySeconds = clearInitDelaySeconds; + return this; + } + + public LockBs clearPeriodSeconds(long clearPeriodSeconds) { + this.clearPeriodSeconds = clearPeriodSeconds; + return this; + } + + public LockBs enableClearTask(boolean enableClearTask) { + this.enableClearTask = enableClearTask; + return this; + } + + public LockBs waits(IWait waits) { + ArgUtil.notNull(waits, "waits"); + + this.waits = waits; + return this; + } + + public LockBs id(Id id) { + ArgUtil.notNull(id, "id"); + + this.id = id; + return this; + } + + public LockBs operator(IOperator operator) { + ArgUtil.notNull(operator, "operator"); + + this.operator = operator; + return this; + } + + public ILock lock() { + ArgUtil.notNull(operator, "operator"); + + return SimpleLock.newInstance() + .waits(waits) + .id(id) + .operator(operator) + .enableClearTask(enableClearTask) + .clearInitDelaySeconds(clearInitDelaySeconds) + .clearPeriodSeconds(clearPeriodSeconds) + .init(); + } + +} diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/constant/LockConst.java b/lock-core/src/main/java/com/github/houbb/lock/core/constant/LockConst.java similarity index 70% rename from lock-core/src/main/java/com/github/houbb/lock/redis/constant/LockConst.java rename to lock-core/src/main/java/com/github/houbb/lock/core/constant/LockConst.java index 42e88c2..dbbc8c4 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/constant/LockConst.java +++ b/lock-core/src/main/java/com/github/houbb/lock/core/constant/LockConst.java @@ -1,4 +1,4 @@ -package com.github.houbb.lock.redis.constant; +package com.github.houbb.lock.core.constant; /** * 通用锁常量 @@ -14,10 +14,10 @@ public final class LockConst { /** * 默认的失效时间 * - * 暂时定为 30min + * 暂时定为 1min * @since 0.0.1 */ - public static final int DEFAULT_EXPIRE_MILLS = 1000 * 60 * 30; + public static final int DEFAULT_EXPIRE_MILLS = 1000 * 60; /** * 默认锁为全局锁 diff --git a/lock-core/src/main/java/com/github/houbb/lock/core/core/AbstractLock.java b/lock-core/src/main/java/com/github/houbb/lock/core/core/AbstractLock.java new file mode 100644 index 0000000..ffcc455 --- /dev/null +++ b/lock-core/src/main/java/com/github/houbb/lock/core/core/AbstractLock.java @@ -0,0 +1,184 @@ +package com.github.houbb.lock.core.core; + +import com.github.houbb.heaven.util.common.ArgUtil; +import com.github.houbb.id.api.Id; +import com.github.houbb.id.core.core.Ids; +import com.github.houbb.lock.api.core.ILock; +import com.github.houbb.lock.api.support.IOperator; +import com.github.houbb.lock.core.constant.LockConst; +import com.github.houbb.wait.api.IWait; +import com.github.houbb.wait.core.Waits; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Condition; + +/** + * 抽象实现 + * @author binbin.hou + * @since 0.0.1 + */ +public abstract class AbstractLock implements ILock { + + /** + * 锁等待 + * @since 0.0.1 + */ + protected IWait waits = Waits.threadSleep(); + + /** + * 标识策略 + * @since 0.0.4 + */ + protected Id id = Ids.uuid32(); + + /** + * 操作策略 + * @since 0.0.4 + */ + protected IOperator operator; + + /** + * 清空初始化延迟时间 + * @since 0.0.4 + */ + private long clearInitDelaySeconds = 5; + + /** + * 清空初始化周期 + * @since 0.0.4 + */ + private long clearPeriodSeconds = 5; + + /** + * 是否启用清空任务 + * @since 0.0.4 + */ + private boolean enableClearTask = true; + + public AbstractLock waits(IWait waits) { + this.waits = waits; + return this; + } + + public AbstractLock id(Id id) { + this.id = id; + return this; + } + + public AbstractLock operator(IOperator operator) { + this.operator = operator; + return this; + } + + public AbstractLock clearInitDelaySeconds(long clearInitDelaySeconds) { + this.clearInitDelaySeconds = clearInitDelaySeconds; + return this; + } + + public AbstractLock clearPeriodSeconds(long clearPeriodSeconds) { + this.clearPeriodSeconds = clearPeriodSeconds; + return this; + } + + public AbstractLock enableClearTask(boolean enableClearTask) { + this.enableClearTask = enableClearTask; + return this; + } + + /** + * 初始化 + * @since 0.0.4 + */ + public AbstractLock init() { + // 参数校验 + ArgUtil.notNull(operator, "operator"); + + // 初始化任务 + initClearExpireKey(); + + return this; + } + + /** + * 初始化清空任务 + * @since 0.0.6 + */ + private void initClearExpireKey() { + if(!enableClearTask) { + return; + } + + ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); + //5S 清理一次 + scheduledExecutorService.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + operator.clearExpireLock(); + } + }, clearInitDelaySeconds, clearPeriodSeconds, TimeUnit.SECONDS); + } + + + @Override + public void lock() { + throw new UnsupportedOperationException(); + } + + @Override + public void lockInterruptibly() throws InterruptedException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean tryLock() { + return tryLock(LockConst.DEFAULT_KEY); + } + + @Override + public void unlock() { + unlock(LockConst.DEFAULT_KEY); + } + + @Override + public boolean tryLock(long time, TimeUnit unit, String key) throws InterruptedException { + long startTimeMills = System.currentTimeMillis(); + + // 一次获取,直接成功 + boolean result = this.tryLock(key); + if(result) { + return true; + } + + // 时间判断 + if(time <= 0) { + return false; + } + long durationMills = unit.toMillis(time); + long endMills = startTimeMills + durationMills; + + // 循环等待 + while (System.currentTimeMillis() < endMills) { + result = tryLock(key); + if(result) { + return true; + } + + // 等待 1ms + waits.wait(TimeUnit.MILLISECONDS, 1); + } + return false; + } + + @Override + public synchronized boolean tryLock(long time, TimeUnit unit) throws InterruptedException { + return tryLock(time, unit, LockConst.DEFAULT_KEY); + } + + @Override + public Condition newCondition() { + throw new UnsupportedOperationException(); + } + +} diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockNone.java b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockNone.java similarity index 88% rename from lock-core/src/main/java/com/github/houbb/lock/redis/core/LockNone.java rename to lock-core/src/main/java/com/github/houbb/lock/core/core/LockNone.java index 9e34863..9d459af 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockNone.java +++ b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockNone.java @@ -1,4 +1,4 @@ -package com.github.houbb.lock.redis.core; +package com.github.houbb.lock.core.core; import com.github.houbb.heaven.annotation.ThreadSafe; diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWrite.java b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockReadWrite.java similarity index 98% rename from lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWrite.java rename to lock-core/src/main/java/com/github/houbb/lock/core/core/LockReadWrite.java index dcbee46..8b63ace 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWrite.java +++ b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockReadWrite.java @@ -1,4 +1,4 @@ -package com.github.houbb.lock.redis.core; +package com.github.houbb.lock.core.core; import com.github.houbb.lock.api.core.IReadWriteLock; import com.github.houbb.log.integration.core.Log; diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteOwner.java b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockReadWriteOwner.java similarity index 98% rename from lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteOwner.java rename to lock-core/src/main/java/com/github/houbb/lock/core/core/LockReadWriteOwner.java index fdaeff9..8a9f89f 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteOwner.java +++ b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockReadWriteOwner.java @@ -1,4 +1,4 @@ -package com.github.houbb.lock.redis.core; +package com.github.houbb.lock.core.core; import com.github.houbb.lock.api.core.IReadWriteLock; import com.github.houbb.log.integration.core.Log; diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteRe.java b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockReadWriteRe.java similarity index 99% rename from lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteRe.java rename to lock-core/src/main/java/com/github/houbb/lock/core/core/LockReadWriteRe.java index e7373d5..5cb764b 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteRe.java +++ b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockReadWriteRe.java @@ -1,4 +1,4 @@ -package com.github.houbb.lock.redis.core; +package com.github.houbb.lock.core.core; import com.github.houbb.lock.api.core.IReadWriteLock; import com.github.houbb.log.integration.core.Log; diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockSpin.java b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockSpin.java similarity index 89% rename from lock-core/src/main/java/com/github/houbb/lock/redis/core/LockSpin.java rename to lock-core/src/main/java/com/github/houbb/lock/core/core/LockSpin.java index 2d2f3e8..de252dc 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockSpin.java +++ b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockSpin.java @@ -1,6 +1,6 @@ -package com.github.houbb.lock.redis.core; +package com.github.houbb.lock.core.core; -import com.github.houbb.lock.redis.exception.LockRuntimeException; +import com.github.houbb.lock.core.exception.LockRuntimeException; import java.util.concurrent.atomic.AtomicReference; diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockSpinRe.java b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockSpinRe.java similarity index 93% rename from lock-core/src/main/java/com/github/houbb/lock/redis/core/LockSpinRe.java rename to lock-core/src/main/java/com/github/houbb/lock/core/core/LockSpinRe.java index 8dd908b..bc86311 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockSpinRe.java +++ b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockSpinRe.java @@ -1,7 +1,7 @@ -package com.github.houbb.lock.redis.core; +package com.github.houbb.lock.core.core; import com.github.houbb.heaven.util.util.DateUtil; -import com.github.houbb.lock.redis.exception.LockRuntimeException; +import com.github.houbb.lock.core.exception.LockRuntimeException; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockWaitNotify.java b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockWaitNotify.java similarity index 93% rename from lock-core/src/main/java/com/github/houbb/lock/redis/core/LockWaitNotify.java rename to lock-core/src/main/java/com/github/houbb/lock/core/core/LockWaitNotify.java index bb5b595..278eccb 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockWaitNotify.java +++ b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockWaitNotify.java @@ -1,6 +1,6 @@ -package com.github.houbb.lock.redis.core; +package com.github.houbb.lock.core.core; -import com.github.houbb.lock.redis.exception.LockRuntimeException; +import com.github.houbb.lock.core.exception.LockRuntimeException; import com.github.houbb.log.integration.core.Log; import com.github.houbb.log.integration.core.LogFactory; diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockWaitNotifyRe.java b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockWaitNotifyRe.java similarity index 93% rename from lock-core/src/main/java/com/github/houbb/lock/redis/core/LockWaitNotifyRe.java rename to lock-core/src/main/java/com/github/houbb/lock/core/core/LockWaitNotifyRe.java index 7032f76..0e02de8 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockWaitNotifyRe.java +++ b/lock-core/src/main/java/com/github/houbb/lock/core/core/LockWaitNotifyRe.java @@ -1,10 +1,9 @@ -package com.github.houbb.lock.redis.core; +package com.github.houbb.lock.core.core; -import com.github.houbb.lock.redis.exception.LockRuntimeException; +import com.github.houbb.lock.core.exception.LockRuntimeException; import com.github.houbb.log.integration.core.Log; import com.github.houbb.log.integration.core.LogFactory; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/core/Locks.java b/lock-core/src/main/java/com/github/houbb/lock/core/core/Locks.java similarity index 97% rename from lock-core/src/main/java/com/github/houbb/lock/redis/core/Locks.java rename to lock-core/src/main/java/com/github/houbb/lock/core/core/Locks.java index d90c726..7317679 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/core/Locks.java +++ b/lock-core/src/main/java/com/github/houbb/lock/core/core/Locks.java @@ -1,4 +1,4 @@ -package com.github.houbb.lock.redis.core; +package com.github.houbb.lock.core.core; import com.github.houbb.lock.api.core.ILock; import com.github.houbb.lock.api.core.IReadWriteLock; diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/exception/LockRuntimeException.java b/lock-core/src/main/java/com/github/houbb/lock/core/exception/LockRuntimeException.java similarity index 92% rename from lock-core/src/main/java/com/github/houbb/lock/redis/exception/LockRuntimeException.java rename to lock-core/src/main/java/com/github/houbb/lock/core/exception/LockRuntimeException.java index 68e485f..8bcdd06 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/exception/LockRuntimeException.java +++ b/lock-core/src/main/java/com/github/houbb/lock/core/exception/LockRuntimeException.java @@ -1,4 +1,4 @@ -package com.github.houbb.lock.redis.exception; +package com.github.houbb.lock.core.exception; /** * @author binbin.hou diff --git a/lock-core/src/main/java/com/github/houbb/lock/core/package-info.java b/lock-core/src/main/java/com/github/houbb/lock/core/package-info.java new file mode 100644 index 0000000..9f59f6b --- /dev/null +++ b/lock-core/src/main/java/com/github/houbb/lock/core/package-info.java @@ -0,0 +1 @@ +package com.github.houbb.lock.core; diff --git a/lock-core/src/main/java/com/github/houbb/lock/core/support/package-info.java b/lock-core/src/main/java/com/github/houbb/lock/core/support/package-info.java new file mode 100644 index 0000000..31c0af8 --- /dev/null +++ b/lock-core/src/main/java/com/github/houbb/lock/core/support/package-info.java @@ -0,0 +1,5 @@ +/** + * @author binbin.hou + * @since 1.0.0 + */ +package com.github.houbb.lock.core.support; diff --git a/lock-core/src/main/java/com/github/houbb/lock/core/support/simple/SimpleLock.java b/lock-core/src/main/java/com/github/houbb/lock/core/support/simple/SimpleLock.java new file mode 100644 index 0000000..3682093 --- /dev/null +++ b/lock-core/src/main/java/com/github/houbb/lock/core/support/simple/SimpleLock.java @@ -0,0 +1,43 @@ +package com.github.houbb.lock.core.support.simple; + +import com.github.houbb.heaven.util.lang.StringUtil; +import com.github.houbb.id.core.util.IdThreadLocalHelper; +import com.github.houbb.lock.core.constant.LockConst; +import com.github.houbb.lock.core.core.AbstractLock; +import com.github.houbb.lock.core.exception.LockRuntimeException; + +/** + * 简单锁实现策略 + * + * @author binbin.hou + * @since 0.0.4 + */ +public class SimpleLock extends AbstractLock { + + public static SimpleLock newInstance() { + return new SimpleLock(); + } + + @Override + public boolean tryLock(String key) { + final String requestId = id.id(); + IdThreadLocalHelper.put(requestId); + + return operator.lock(key, requestId, LockConst.DEFAULT_EXPIRE_MILLS); + } + + @Override + public void unlock(String key) { + final String requestId = IdThreadLocalHelper.get(); + if(StringUtil.isEmpty(requestId)) { + String threadName = Thread.currentThread().getName(); + throw new LockRuntimeException("Thread " + threadName +" not contains requestId"); + } + + boolean unlock = operator.unlock(key, requestId); + if(!unlock) { + throw new LockRuntimeException("Unlock key " + key + " result is failed!"); + } + } + +} diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/core/AbstractLock.java b/lock-core/src/main/java/com/github/houbb/lock/redis/core/AbstractLock.java deleted file mode 100644 index 8d8c3a0..0000000 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/core/AbstractLock.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.github.houbb.lock.redis.core; - -import com.github.houbb.lock.api.core.ILock; -import com.github.houbb.lock.redis.constant.LockConst; -import com.github.houbb.wait.api.IWait; -import com.github.houbb.wait.core.Waits; - -import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.Condition; - -/** - * 抽象实现 - * @author binbin.hou - * @since 0.0.1 - */ -public abstract class AbstractLock implements ILock { - - /** - * 锁等待 - * @since 0.0.1 - */ - private final IWait wait; - - public AbstractLock() { - this.wait = Waits.threadSleep(); - } - - protected AbstractLock(IWait wait) { - this.wait = wait; - } - - @Override - public void lock() { - throw new UnsupportedOperationException(); - } - - @Override - public void lockInterruptibly() throws InterruptedException { - throw new UnsupportedOperationException(); - } - - @Override - public boolean tryLock() { - return tryLock(LockConst.DEFAULT_KEY); - } - - @Override - public void unlock() { - unlock(LockConst.DEFAULT_KEY); - } - - @Override - public boolean tryLock(long time, TimeUnit unit, String key) throws InterruptedException { - long startTimeMills = System.currentTimeMillis(); - - // 一次获取,直接成功 - boolean result = this.tryLock(key); - if(result) { - return true; - } - - // 时间判断 - if(time <= 0) { - return false; - } - long durationMills = unit.toMillis(time); - long endMills = startTimeMills + durationMills; - - // 循环等待 - while (System.currentTimeMillis() < endMills) { - result = tryLock(key); - if(result) { - return true; - } - - // 等待 1ms - wait.wait(TimeUnit.MILLISECONDS, 1); - } - return false; - } - - @Override - public synchronized boolean tryLock(long time, TimeUnit unit) throws InterruptedException { - return tryLock(time, unit, LockConst.DEFAULT_KEY); - } - - @Override - public Condition newCondition() { - throw new UnsupportedOperationException(); - } - -} diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/package-info.java b/lock-core/src/main/java/com/github/houbb/lock/redis/package-info.java deleted file mode 100644 index f7c4873..0000000 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package com.github.houbb.lock.redis; \ No newline at end of file diff --git a/lock-redis/pom.xml b/lock-redis/pom.xml index 826a7b2..d5f63ae 100644 --- a/lock-redis/pom.xml +++ b/lock-redis/pom.xml @@ -5,7 +5,7 @@ lock com.github.houbb - 0.0.3 + 0.0.4 4.0.0 diff --git a/lock-redis/src/main/java/com/github/houbb/lock/redis/bs/LockRedisBs.java b/lock-redis/src/main/java/com/github/houbb/lock/redis/bs/LockRedisBs.java deleted file mode 100644 index c9d4e00..0000000 --- a/lock-redis/src/main/java/com/github/houbb/lock/redis/bs/LockRedisBs.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.github.houbb.lock.redis.bs; - -import com.github.houbb.id.api.Id; -import com.github.houbb.id.core.core.Ids; -import com.github.houbb.lock.api.core.ILock; -import com.github.houbb.lock.api.support.IOperator; -import com.github.houbb.lock.redis.lock.LockRedis; -import com.github.houbb.wait.api.IWait; -import com.github.houbb.wait.core.Waits; - -/** - * 引导类 - * @author binbin.hou - * @since 0.0.1 - */ -public final class LockRedisBs { - - private LockRedisBs(){} - - /** - * 等待实现 - * @since 0.0.1 - */ - private IWait wait = Waits.threadSleep(); - - /** - * ID 实现 - * @since 0.0.1 - */ - private Id id = Ids.uuid32(); - - /** - * 操作类 - * @since 0.0.1 - */ - private IOperator operator; - - /** - * 新建对象实例 - * @return 对象实例 - * @since 0.0.1 - */ - public static LockRedisBs newInstance() { - return new LockRedisBs(); - } - - public LockRedisBs wait(IWait wait) { - this.wait = wait; - return this; - } - - public LockRedisBs id(Id id) { - this.id = id; - return this; - } - - public LockRedisBs operator(IOperator operator) { - this.operator = operator; - return this; - } - - /** - * 创建锁 - * @return 锁 - * @since 0.0.1 - */ - public ILock lock() { - return new LockRedis(wait, operator, id); - } - -} diff --git a/lock-redis/src/main/java/com/github/houbb/lock/redis/exception/LockRedisException.java b/lock-redis/src/main/java/com/github/houbb/lock/redis/exception/LockRedisException.java index e54580b..1d5e374 100644 --- a/lock-redis/src/main/java/com/github/houbb/lock/redis/exception/LockRedisException.java +++ b/lock-redis/src/main/java/com/github/houbb/lock/redis/exception/LockRedisException.java @@ -1,5 +1,7 @@ package com.github.houbb.lock.redis.exception; +import com.github.houbb.lock.core.exception.LockRuntimeException; + /** * @author binbin.hou * @since 0.0.3 diff --git a/lock-redis/src/main/java/com/github/houbb/lock/redis/lock/LockRedis.java b/lock-redis/src/main/java/com/github/houbb/lock/redis/lock/LockRedis.java deleted file mode 100644 index 04453c9..0000000 --- a/lock-redis/src/main/java/com/github/houbb/lock/redis/lock/LockRedis.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.github.houbb.lock.redis.lock; - -import com.github.houbb.heaven.util.lang.StringUtil; -import com.github.houbb.id.api.Id; -import com.github.houbb.id.core.util.IdThreadLocalHelper; -import com.github.houbb.lock.api.support.IOperator; -import com.github.houbb.lock.redis.constant.LockRedisConst; -import com.github.houbb.lock.redis.core.AbstractLock; -import com.github.houbb.lock.redis.exception.LockRedisException; -import com.github.houbb.wait.api.IWait; - -/** - * 这里是基于 redis 实现 - * - * 实际上也可以基于 zk/数据库等实现。 - * - * @author binbin.hou - * @since 0.0.1 - */ -public class LockRedis extends AbstractLock { - - /** - * redis 操作实现 - * @since 0.0.1 - */ - private final IOperator redisOperator; - - /** - * 主键标识 - * @since 0.0.1 - */ - private final Id id; - - public LockRedis(IWait wait, IOperator redisOperator, Id id) { - super(wait); - this.redisOperator = redisOperator; - this.id = id; - } - - @Override - public boolean tryLock(String key) { - final String requestId = id.id(); - IdThreadLocalHelper.put(requestId); - - return redisOperator.lock(key, requestId, LockRedisConst.DEFAULT_EXPIRE_MILLS); - } - - @Override - public void unlock(String key) { - final String requestId = IdThreadLocalHelper.get(); - if(StringUtil.isEmpty(requestId)) { - String threadName = Thread.currentThread().getName(); - throw new LockRedisException("Thread " + threadName +" not contains requestId"); - } - - boolean unlock = redisOperator.unlock(key, requestId); - if(!unlock) { - throw new LockRedisException("Unlock key " + key + " result is failed!"); - } - } -} diff --git a/lock-redis/src/main/java/com/github/houbb/lock/redis/package-info.java b/lock-redis/src/main/java/com/github/houbb/lock/redis/package-info.java index fe1eb76..0870c0c 100644 --- a/lock-redis/src/main/java/com/github/houbb/lock/redis/package-info.java +++ b/lock-redis/src/main/java/com/github/houbb/lock/redis/package-info.java @@ -1 +1,5 @@ +/** + * @author binbin.hou + * @since 1.0.0 + */ package com.github.houbb.lock.redis; diff --git a/lock-redis/src/main/java/com/github/houbb/lock/redis/support/operator/JedisOperator.java b/lock-redis/src/main/java/com/github/houbb/lock/redis/support/operator/JedisOperator.java index ec7e71a..eaf66f7 100644 --- a/lock-redis/src/main/java/com/github/houbb/lock/redis/support/operator/JedisOperator.java +++ b/lock-redis/src/main/java/com/github/houbb/lock/redis/support/operator/JedisOperator.java @@ -60,4 +60,9 @@ public class JedisOperator implements IOperator { return LockRedisConst.RELEASE_SUCCESS.equals(result); } + @Override + public void clearExpireLock() { + + } + } diff --git a/lock-redis/src/main/java/com/github/houbb/lock/redis/support/package-info.java b/lock-redis/src/main/java/com/github/houbb/lock/redis/support/package-info.java index a9b9bd8..33fdcd2 100644 --- a/lock-redis/src/main/java/com/github/houbb/lock/redis/support/package-info.java +++ b/lock-redis/src/main/java/com/github/houbb/lock/redis/support/package-info.java @@ -1 +1 @@ -package com.github.houbb.lock.redis.support; \ No newline at end of file +package com.github.houbb.lock.redis.support; diff --git a/lock-test/pom.xml b/lock-test/pom.xml index 9063f35..6d444cf 100644 --- a/lock-test/pom.xml +++ b/lock-test/pom.xml @@ -5,7 +5,7 @@ lock com.github.houbb - 0.0.3 + 0.0.4 4.0.0 diff --git a/lock-test/src/main/java/com/github/houbb/lock/test/core/LockSpinReThread.java b/lock-test/src/main/java/com/github/houbb/lock/test/core/LockSpinReThread.java index c40f7ab..a36d79c 100644 --- a/lock-test/src/main/java/com/github/houbb/lock/test/core/LockSpinReThread.java +++ b/lock-test/src/main/java/com/github/houbb/lock/test/core/LockSpinReThread.java @@ -1,8 +1,7 @@ package com.github.houbb.lock.test.core; import com.github.houbb.lock.api.core.ILock; -import com.github.houbb.lock.redis.core.LockSpin; -import com.github.houbb.lock.redis.core.LockSpinRe; +import com.github.houbb.lock.core.core.LockSpinRe; /** * @author binbin.hou diff --git a/lock-test/src/main/java/com/github/houbb/lock/test/core/LockSpinThread.java b/lock-test/src/main/java/com/github/houbb/lock/test/core/LockSpinThread.java index 0e65dd3..ba31c77 100644 --- a/lock-test/src/main/java/com/github/houbb/lock/test/core/LockSpinThread.java +++ b/lock-test/src/main/java/com/github/houbb/lock/test/core/LockSpinThread.java @@ -1,7 +1,7 @@ package com.github.houbb.lock.test.core; import com.github.houbb.lock.api.core.ILock; -import com.github.houbb.lock.redis.core.LockSpin; +import com.github.houbb.lock.core.core.LockSpin; /** * @author binbin.hou diff --git a/lock-test/src/main/java/com/github/houbb/lock/test/core/LockWaitNotifyThread.java b/lock-test/src/main/java/com/github/houbb/lock/test/core/LockWaitNotifyThread.java index ae64102..c6dc9a7 100644 --- a/lock-test/src/main/java/com/github/houbb/lock/test/core/LockWaitNotifyThread.java +++ b/lock-test/src/main/java/com/github/houbb/lock/test/core/LockWaitNotifyThread.java @@ -2,7 +2,7 @@ package com.github.houbb.lock.test.core; import com.github.houbb.heaven.util.util.DateUtil; import com.github.houbb.lock.api.core.ILock; -import com.github.houbb.lock.redis.core.LockWaitNotify; +import com.github.houbb.lock.core.core.LockWaitNotify; import com.github.houbb.log.integration.core.Log; import com.github.houbb.log.integration.core.LogFactory; diff --git a/lock-test/src/main/java/com/github/houbb/lock/test/core/LockWaitNotifyThread2.java b/lock-test/src/main/java/com/github/houbb/lock/test/core/LockWaitNotifyThread2.java index 2228c10..fa6158d 100644 --- a/lock-test/src/main/java/com/github/houbb/lock/test/core/LockWaitNotifyThread2.java +++ b/lock-test/src/main/java/com/github/houbb/lock/test/core/LockWaitNotifyThread2.java @@ -2,7 +2,7 @@ package com.github.houbb.lock.test.core; import com.github.houbb.heaven.util.util.DateUtil; import com.github.houbb.lock.api.core.ILock; -import com.github.houbb.lock.redis.core.LockWaitNotify; +import com.github.houbb.lock.core.core.LockWaitNotify; import com.github.houbb.log.integration.core.Log; import com.github.houbb.log.integration.core.LogFactory; diff --git a/lock-test/src/main/java/com/github/houbb/lock/test/core/LockWaitNotifyThreadRe.java b/lock-test/src/main/java/com/github/houbb/lock/test/core/LockWaitNotifyThreadRe.java index ed98224..925f57d 100644 --- a/lock-test/src/main/java/com/github/houbb/lock/test/core/LockWaitNotifyThreadRe.java +++ b/lock-test/src/main/java/com/github/houbb/lock/test/core/LockWaitNotifyThreadRe.java @@ -1,14 +1,10 @@ package com.github.houbb.lock.test.core; -import com.github.houbb.heaven.util.util.DateUtil; import com.github.houbb.lock.api.core.ILock; -import com.github.houbb.lock.redis.core.LockWaitNotify; -import com.github.houbb.lock.redis.core.LockWaitNotifyRe; +import com.github.houbb.lock.core.core.LockWaitNotifyRe; import com.github.houbb.log.integration.core.Log; import com.github.houbb.log.integration.core.LogFactory; -import java.util.concurrent.TimeUnit; - /** * @author binbin.hou * @since 1.0.0 diff --git a/lock-test/src/test/java/com/github/houbb/lock/test/redis/LockRedisTest.java b/lock-test/src/test/java/com/github/houbb/lock/test/redis/LockRedisTest.java index 6bdae4c..6edf950 100644 --- a/lock-test/src/test/java/com/github/houbb/lock/test/redis/LockRedisTest.java +++ b/lock-test/src/test/java/com/github/houbb/lock/test/redis/LockRedisTest.java @@ -2,7 +2,7 @@ package com.github.houbb.lock.test.redis; import com.github.houbb.lock.api.core.ILock; import com.github.houbb.lock.api.support.IOperator; -import com.github.houbb.lock.redis.bs.LockRedisBs; +import com.github.houbb.lock.core.bs.LockBs; import com.github.houbb.lock.redis.support.operator.JedisOperator; import org.junit.Ignore; import org.junit.Test; @@ -21,7 +21,7 @@ public class LockRedisTest { IOperator operator = new JedisOperator(jedis); // 获取锁 - ILock lock = LockRedisBs.newInstance().operator(operator).lock(); + ILock lock = LockBs.newInstance(operator).lock(); try { boolean lockResult = lock.tryLock(); diff --git a/pom.xml b/pom.xml index 26d2564..da01491 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.github.houbb lock pom - 0.0.3 + 0.0.4 lock-api lock-core @@ -34,7 +34,7 @@ 1.0.6 - 0.1.148 + 0.1.161 0.0.6 0.0.1 1.1.8 diff --git a/release.bat b/release.bat index e9477f0..d896512 100644 --- a/release.bat +++ b/release.bat @@ -10,9 +10,9 @@ ECHO "============================= RELEASE START..." :: 版本号信息(需要手动指定) :::: 旧版本名称 -SET version=0.0.3 +SET version=0.0.4 :::: 新版本名称 -SET newVersion=0.0.4 +SET newVersion=0.0.5 :::: 组织名称 SET groupName=com.github.houbb :::: 项目名称