release branch 0.0.4
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>lock</artifactId>
|
||||
<groupId>com.github.houbb</groupId>
|
||||
<version>0.0.3</version>
|
||||
<version>0.0.4</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +1,5 @@
|
||||
/**
|
||||
* @author binbin.hou
|
||||
* @since 1.0.0
|
||||
*/
|
||||
package com.github.houbb.lock.redis;
|
||||
|
||||
@@ -60,4 +60,9 @@ public class JedisOperator implements IOperator {
|
||||
return LockRedisConst.RELEASE_SUCCESS.equals(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearExpireLock() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
package com.github.houbb.lock.redis.support;
|
||||
package com.github.houbb.lock.redis.support;
|
||||
|
||||
Reference in New Issue
Block a user