release branch 0.0.4
This commit is contained in:
@@ -11,5 +11,15 @@
|
||||
|
||||
<artifactId>lock-api</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.houbb</groupId>
|
||||
<artifactId>common-cache-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.houbb</groupId>
|
||||
<artifactId>id-api</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -1,29 +1,27 @@
|
||||
package com.github.houbb.lock.api.core;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
/**
|
||||
* 锁定义
|
||||
* 分布式锁接口定义
|
||||
* @author binbin.hou
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public interface ILock extends Lock {
|
||||
public interface ILock {
|
||||
|
||||
/**
|
||||
* 尝试加锁
|
||||
* 尝试加锁,如果失败,会一直尝试。
|
||||
*
|
||||
* @param time 时间
|
||||
* @param unit 当为
|
||||
* @param key key
|
||||
* @return 返回
|
||||
* @throws InterruptedException 异常
|
||||
* @since 0.0.1
|
||||
*/
|
||||
boolean tryLock(long time, TimeUnit unit,
|
||||
String key) throws InterruptedException;
|
||||
boolean tryLock(long time, TimeUnit unit, String key);
|
||||
|
||||
/**
|
||||
* 尝试加锁
|
||||
* 尝试加锁,只加锁一次
|
||||
* @param key key
|
||||
* @return 返回
|
||||
* @since 0.0.1
|
||||
@@ -35,6 +33,6 @@ public interface ILock extends Lock {
|
||||
* @param key key
|
||||
* @since 0.0.1
|
||||
*/
|
||||
void unlock(String key);
|
||||
boolean unlock(String key);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.github.houbb.lock.api.core;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 分布式锁接口定义
|
||||
* @author binbin.hou
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public interface ILockSupport {
|
||||
|
||||
/**
|
||||
* 尝试加锁,如果失败,会一直尝试。
|
||||
*
|
||||
* @param time 时间
|
||||
* @param unit 单位
|
||||
* @param key key
|
||||
* @param context 上下文
|
||||
* @return 返回
|
||||
* @since 0.0.1
|
||||
*/
|
||||
boolean tryLock(long time, TimeUnit unit, String key, final ILockSupportContext context);
|
||||
|
||||
/**
|
||||
* 尝试加锁,只加锁一次
|
||||
* @param key key
|
||||
* @param context 上下文
|
||||
* @return 返回
|
||||
* @since 0.0.1
|
||||
*/
|
||||
boolean tryLock(String key, final ILockSupportContext context);
|
||||
|
||||
/**
|
||||
* 解锁
|
||||
* @param key key
|
||||
* @param context 上下文
|
||||
* @since 0.0.1
|
||||
*/
|
||||
boolean unlock(String key, final ILockSupportContext context);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.github.houbb.lock.api.core;
|
||||
|
||||
import com.github.houbb.common.cache.api.service.ICommonCacheService;
|
||||
import com.github.houbb.id.api.Id;
|
||||
|
||||
/**
|
||||
* 分布式锁接口定义
|
||||
* @author binbin.hou
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public interface ILockSupportContext {
|
||||
|
||||
/**
|
||||
* @return 标识策略
|
||||
* @since 0.0.4
|
||||
*/
|
||||
Id id();
|
||||
|
||||
/**
|
||||
* @return 缓存策略
|
||||
* @since 0.0.4
|
||||
*/
|
||||
ICommonCacheService commonCacheService();
|
||||
|
||||
/**
|
||||
* 锁的过期时间
|
||||
* @return 结果
|
||||
*/
|
||||
int lockExpireMills();
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.github.houbb.lock.api.core;
|
||||
|
||||
/**
|
||||
* 读写锁定义接口
|
||||
* @author binbin.hou
|
||||
* @since 0.0.2
|
||||
*/
|
||||
public interface IReadWriteLock {
|
||||
|
||||
/**
|
||||
* 获取读锁
|
||||
* @since 0.0.2
|
||||
*/
|
||||
void lockRead();
|
||||
|
||||
/**
|
||||
* 释放读锁
|
||||
*/
|
||||
void unlockRead();
|
||||
|
||||
/**
|
||||
* 获取写锁
|
||||
* @since 0.0.2
|
||||
*/
|
||||
void lockWrite();
|
||||
|
||||
/**
|
||||
* 释放写锁
|
||||
*/
|
||||
void unlockWrite();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.github.houbb.lock.api.exception;
|
||||
|
||||
/**
|
||||
* 加锁运行时异常
|
||||
* @since 1.0.0
|
||||
* @author dh
|
||||
*/
|
||||
public class LockException extends RuntimeException {
|
||||
|
||||
public LockException() {
|
||||
}
|
||||
|
||||
public LockException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public LockException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public LockException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public LockException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.github.houbb.lock.api.support;
|
||||
|
||||
/**
|
||||
* 操作接口定义
|
||||
*
|
||||
* ps: 可以基于集中式数据库做操作
|
||||
*
|
||||
* @author binbin.hou
|
||||
* @since 0.0.3
|
||||
*/
|
||||
public interface IOperator {
|
||||
|
||||
/**
|
||||
* 尝试获取分布式锁
|
||||
*
|
||||
* @param lockKey 锁
|
||||
* @param requestId 请求标识
|
||||
* @param expireTimeMills 超期时间
|
||||
* @return 是否获取成功
|
||||
* @since 0.0.3
|
||||
*/
|
||||
boolean lock(String lockKey, String requestId, int expireTimeMills);
|
||||
|
||||
/**
|
||||
* 解锁
|
||||
* @param lockKey 锁 key
|
||||
* @param requestId 请求标识
|
||||
* @return 结果
|
||||
* @since 0.0.3
|
||||
*/
|
||||
boolean unlock(String lockKey, String requestId);
|
||||
|
||||
/**
|
||||
* 清空过期的锁
|
||||
*
|
||||
* 避免单个线程 unlock 失败,定时移除过期的锁。
|
||||
* @since 0.0.4
|
||||
*/
|
||||
void clearExpireLock();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user