修改登录-添加先排除网格员
This commit is contained in:
@@ -208,4 +208,44 @@ public class DistributedLockUtil {
|
||||
String identifier = acquireLockWithRenewal(lockKey);
|
||||
return new AutoReleaseLock(this, lockKey, identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 锁+超时时间
|
||||
* @param lockKey
|
||||
* @param timeout
|
||||
* @param unit
|
||||
* @return
|
||||
*/
|
||||
public AutoReleaseLock tryLock(String lockKey, long timeout, TimeUnit unit) {
|
||||
// 1. 校验参数合法性(防御性编程)
|
||||
if (lockKey == null || lockKey.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("锁key不能为空");
|
||||
}
|
||||
if (timeout < 0) {
|
||||
throw new IllegalArgumentException("超时时间不能为负数");
|
||||
}
|
||||
if (unit == null) {
|
||||
throw new IllegalArgumentException("时间单位不能为空");
|
||||
}
|
||||
|
||||
// 2. 带超时获取锁(核心逻辑,需实现 acquireLockWithTimeout 方法)
|
||||
String identifier = acquireLockWithTimeout(lockKey, timeout, unit);
|
||||
|
||||
// 3. 返回自动释放锁(复用原有 AutoReleaseLock,无需修改)
|
||||
return new AutoReleaseLock(this, lockKey, identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 带超时时间获取锁(支持自定义超时单位)
|
||||
* @param lockKey 锁键
|
||||
* @param timeout 获取锁的超时时间
|
||||
* @param unit 时间单位
|
||||
* @return 锁标识(获取失败返回null)
|
||||
*/
|
||||
public String acquireLockWithTimeout(String lockKey, long timeout, TimeUnit unit) {
|
||||
// 转换超时时间为秒(向上取整避免精度丢失)
|
||||
long acquireTimeoutSeconds = (long) Math.ceil((double) unit.toMillis(timeout) / 1000);
|
||||
// 使用默认的锁过期时间(30秒),也可根据需要改为让调用方传入
|
||||
return acquireLockWithRenewal(lockKey, DEFAULT_EXPIRE_SECONDS, acquireTimeoutSeconds);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user