mirror of
https://github.com/hpd840321/starRiverProperty.git
synced 2026-06-09 08:20:31 +08:00
chore: 工作区反编译与 Maven/文档/脚本同步到发布分支
- artifacts/decompiled 树与相关源码变更 - maven-cw-elevator-application 业务 docs 与 package-info - scripts 下 formatter 校验与辅助脚本 - 其他子工程/接口与发布线一并纳入版本控制 Made-with: Cursor Former-commit-id: e102e8cab64e575bcd23c9a66a598aa1892bb492
This commit is contained in:
+7
-19
@@ -7,24 +7,12 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties({LockProperties.class})
|
||||
@ConditionalOnProperty(prefix = "intelligent.lock", name = {"enable"}, havingValue = "true")
|
||||
public class IntelligentLockConfiguration
|
||||
{
|
||||
@Bean
|
||||
public LockFactory lockFactory() {
|
||||
return new LockFactory();
|
||||
@EnableConfigurationProperties({ LockProperties.class })
|
||||
@ConditionalOnProperty(prefix = "intelligent.lock", name = { "enable" }, havingValue = "true")
|
||||
public class IntelligentLockConfiguration {
|
||||
@Bean
|
||||
public LockFactory lockFactory() {
|
||||
return new LockFactory();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+12
-14
@@ -10,19 +10,17 @@ import java.lang.annotation.Target;
|
||||
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD})
|
||||
@Target({ ElementType.METHOD })
|
||||
public @interface RequiredLock {
|
||||
String name() default "DEFAULT-LOCK";
|
||||
|
||||
LockType lockType() default LockType.REENTRANT;
|
||||
|
||||
long lockWaitTime() default -1L;
|
||||
|
||||
long leaseTime() default -1L;
|
||||
|
||||
LockAcquireTimeoutStrategy lockAcquireTimeoutStrategy() default LockAcquireTimeoutStrategy.FAIL_FAST;
|
||||
|
||||
String customAcquireTimeoutHandleMethod() default "";
|
||||
String name() default "DEFAULT-LOCK";
|
||||
|
||||
LockType lockType() default LockType.REENTRANT;
|
||||
|
||||
long lockWaitTime() default -1L;
|
||||
|
||||
long leaseTime() default -1L;
|
||||
|
||||
LockAcquireTimeoutStrategy lockAcquireTimeoutStrategy() default LockAcquireTimeoutStrategy.FAIL_FAST;
|
||||
|
||||
String customAcquireTimeoutHandleMethod() default "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
+93
-154
@@ -20,174 +20,113 @@ import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
@Order(-999)
|
||||
public class LockAspect
|
||||
{
|
||||
/* 37 */ private static final Logger logger = LoggerFactory.getLogger(LockAspect.class);
|
||||
public class LockAspect {
|
||||
private static final Logger logger = LoggerFactory.getLogger(LockAspect.class);
|
||||
|
||||
@Autowired
|
||||
private LockFactory lockFactory;
|
||||
@Autowired
|
||||
private LockFactory lockFactory;
|
||||
|
||||
@Autowired
|
||||
private LockProperties lockProperties;
|
||||
@Autowired
|
||||
private LockProperties lockProperties;
|
||||
|
||||
/* 45 */ private ThreadLocal<Lock> currentThreadLock = new ThreadLocal<>();
|
||||
/* 46 */ private ThreadLocal<LockResult> currentThreadLockResult = new ThreadLocal<>();
|
||||
private ThreadLocal<Lock> currentThreadLock = new ThreadLocal<>();
|
||||
private ThreadLocal<LockResult> currentThreadLockResult = new ThreadLocal<>();
|
||||
|
||||
@Around("@annotation(requiredLock)")
|
||||
public Object around(ProceedingJoinPoint joinPoint, RequiredLock requiredLock) throws Throwable {
|
||||
LockInfo lockInfo = getLockInfo(joinPoint, requiredLock);
|
||||
this.currentThreadLockResult.set(new LockResult(lockInfo, Boolean.valueOf(false)));
|
||||
Lock lock = this.lockFactory.create(lockInfo);
|
||||
boolean isAcquired = lock.acquire();
|
||||
|
||||
if (!isAcquired) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("获取锁等待超时({})", lockInfo.getName());
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(requiredLock.customAcquireTimeoutHandleMethod())) {
|
||||
|
||||
requiredLock.lockAcquireTimeoutStrategy().handle(lockInfo, lock, (JoinPoint) joinPoint);
|
||||
}
|
||||
|
||||
throw new AcquireLockTimeoutException(String.format("获取锁(%s)超时(%dms)",
|
||||
new Object[] { lockInfo.getName(), Long.valueOf(lockInfo.getLockWaitTime()) }));
|
||||
}
|
||||
|
||||
this.currentThreadLock.set(lock);
|
||||
((LockResult) this.currentThreadLockResult.get()).setLocked(Boolean.valueOf(true));
|
||||
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
|
||||
@AfterReturning("@annotation(requiredLock)")
|
||||
public void afterReturning(JoinPoint joinPoint, RequiredLock requiredLock) throws Throwable {
|
||||
releaseLock();
|
||||
flushThreadLocal();
|
||||
}
|
||||
|
||||
@Around("@annotation(requiredLock)")
|
||||
public Object around(ProceedingJoinPoint joinPoint, RequiredLock requiredLock) throws Throwable {
|
||||
/* 58 */ LockInfo lockInfo = getLockInfo(joinPoint, requiredLock);
|
||||
/* 59 */ this.currentThreadLockResult.set(new LockResult(lockInfo, Boolean.valueOf(false)));
|
||||
/* 60 */ Lock lock = this.lockFactory.create(lockInfo);
|
||||
/* 61 */ boolean isAcquired = lock.acquire();
|
||||
@AfterThrowing(value = "@annotation(requiredLock)", throwing = "throwable")
|
||||
public void afterThrowing(JoinPoint joinPoint, RequiredLock requiredLock, Throwable throwable) throws Throwable {
|
||||
releaseLock();
|
||||
flushThreadLocal();
|
||||
throw throwable;
|
||||
}
|
||||
|
||||
/* 63 */ if (!isAcquired) {
|
||||
/* 64 */ if (logger.isWarnEnabled()) {
|
||||
/* 65 */ logger.warn("获取锁等待超时({})", lockInfo.getName());
|
||||
private void flushThreadLocal() {
|
||||
this.currentThreadLock.remove();
|
||||
this.currentThreadLockResult.remove();
|
||||
}
|
||||
|
||||
private LockInfo getLockInfo(ProceedingJoinPoint joinPoint, RequiredLock lock) {
|
||||
long lockWaitTime = (lock.lockWaitTime() < 0L) ? this.lockProperties.getDefaultWaitTime().longValue()
|
||||
: lock.lockWaitTime();
|
||||
|
||||
long leaseTime = (lock.leaseTime() <= 0L) ? -1L : lock.leaseTime();
|
||||
String lockName = parseLockName(joinPoint, lock.name());
|
||||
return new LockInfo(lock.lockType(), lockName, lockWaitTime, leaseTime);
|
||||
}
|
||||
|
||||
private String parseLockName(ProceedingJoinPoint joinPoint, String name) {
|
||||
return (String) ExpressionParseUtil.parse(joinPoint, name, String.class);
|
||||
}
|
||||
|
||||
private void releaseLock() throws Throwable {
|
||||
LockResult lockResult = this.currentThreadLockResult.get();
|
||||
if (lockResult.getLocked().booleanValue()) {
|
||||
boolean releaseRes = ((Lock) this.currentThreadLock.get()).unlock();
|
||||
lockResult.setLocked(Boolean.valueOf(false));
|
||||
if (!releaseRes)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
private class LockResult {
|
||||
private LockInfo lockInfo;
|
||||
|
||||
private Boolean isLocked;
|
||||
|
||||
public LockResult(LockInfo lockInfo, Boolean isLocked) {
|
||||
this.lockInfo = lockInfo;
|
||||
this.isLocked = isLocked;
|
||||
}
|
||||
|
||||
public LockInfo getLockInfo() {
|
||||
return this.lockInfo;
|
||||
}
|
||||
|
||||
public void setLockInfo(LockInfo lockInfo) {
|
||||
this.lockInfo = lockInfo;
|
||||
}
|
||||
|
||||
public Boolean getLocked() {
|
||||
return this.isLocked;
|
||||
}
|
||||
|
||||
public void setLocked(Boolean locked) {
|
||||
this.isLocked = locked;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 68 */ if (StringUtils.isEmpty(requiredLock.customAcquireTimeoutHandleMethod()))
|
||||
{
|
||||
|
||||
|
||||
/* 72 */ requiredLock.lockAcquireTimeoutStrategy().handle(lockInfo, lock, (JoinPoint)joinPoint);
|
||||
}
|
||||
|
||||
/* 75 */ throw new AcquireLockTimeoutException(String.format("获取锁(%s)超时(%dms)", new Object[] { lockInfo.getName(),
|
||||
/* 76 */ Long.valueOf(lockInfo.getLockWaitTime()) }));
|
||||
}
|
||||
|
||||
|
||||
/* 80 */ this.currentThreadLock.set(lock);
|
||||
/* 81 */ ((LockResult)this.currentThreadLockResult.get()).setLocked(Boolean.valueOf(true));
|
||||
|
||||
/* 83 */ return joinPoint.proceed();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@AfterReturning("@annotation(requiredLock)")
|
||||
public void afterReturning(JoinPoint joinPoint, RequiredLock requiredLock) throws Throwable {
|
||||
/* 95 */ releaseLock();
|
||||
/* 96 */ flushThreadLocal();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@AfterThrowing(value = "@annotation(requiredLock)", throwing = "throwable")
|
||||
public void afterThrowing(JoinPoint joinPoint, RequiredLock requiredLock, Throwable throwable) throws Throwable {
|
||||
releaseLock();
|
||||
flushThreadLocal();
|
||||
throw throwable;
|
||||
}
|
||||
|
||||
private void flushThreadLocal() {
|
||||
this.currentThreadLock.remove();
|
||||
this.currentThreadLockResult.remove();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private LockInfo getLockInfo(ProceedingJoinPoint joinPoint, RequiredLock lock) {
|
||||
long lockWaitTime = (lock.lockWaitTime() < 0L) ? this.lockProperties.getDefaultWaitTime().longValue() : lock.lockWaitTime();
|
||||
|
||||
|
||||
long leaseTime = (lock.leaseTime() <= 0L) ? -1L : lock.leaseTime();
|
||||
String lockName = parseLockName(joinPoint, lock.name());
|
||||
return new LockInfo(lock.lockType(), lockName, lockWaitTime, leaseTime);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private String parseLockName(ProceedingJoinPoint joinPoint, String name) {
|
||||
return (String)ExpressionParseUtil.parse(joinPoint, name, String.class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void releaseLock() throws Throwable {
|
||||
LockResult lockResult = this.currentThreadLockResult.get();
|
||||
if (lockResult.getLocked().booleanValue()) {
|
||||
boolean releaseRes = ((Lock)this.currentThreadLock.get()).unlock();
|
||||
lockResult.setLocked(Boolean.valueOf(false));
|
||||
if (!releaseRes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private class LockResult
|
||||
{
|
||||
private LockInfo lockInfo;
|
||||
|
||||
|
||||
private Boolean isLocked;
|
||||
|
||||
|
||||
public LockResult(LockInfo lockInfo, Boolean isLocked) {
|
||||
this.lockInfo = lockInfo;
|
||||
this.isLocked = isLocked;
|
||||
}
|
||||
|
||||
public LockInfo getLockInfo() {
|
||||
return this.lockInfo;
|
||||
}
|
||||
|
||||
public void setLockInfo(LockInfo lockInfo) {
|
||||
this.lockInfo = lockInfo;
|
||||
}
|
||||
|
||||
public Boolean getLocked() {
|
||||
return this.isLocked;
|
||||
}
|
||||
|
||||
public void setLocked(Boolean locked) {
|
||||
this.isLocked = locked;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+3
-22
@@ -1,26 +1,7 @@
|
||||
package cn.cloudwalk.intelligent.lock.common.enums;
|
||||
|
||||
public enum LockType {
|
||||
REENTRANT,
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public enum LockType
|
||||
{
|
||||
REENTRANT,
|
||||
|
||||
|
||||
|
||||
|
||||
REDLOCK;
|
||||
REDLOCK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+4
-16
@@ -1,19 +1,7 @@
|
||||
package cn.cloudwalk.intelligent.lock.common.exception;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class AcquireLockTimeoutException
|
||||
extends RuntimeException
|
||||
{
|
||||
public AcquireLockTimeoutException(String message) {
|
||||
super(message);
|
||||
public class AcquireLockTimeoutException extends RuntimeException {
|
||||
public AcquireLockTimeoutException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+7
-19
@@ -1,23 +1,11 @@
|
||||
package cn.cloudwalk.intelligent.lock.common.exception;
|
||||
|
||||
public class LockOperationException extends RuntimeException {
|
||||
public LockOperationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class LockOperationException
|
||||
extends RuntimeException
|
||||
{
|
||||
public LockOperationException(String message) {
|
||||
super(message);
|
||||
public LockOperationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
public LockOperationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+35
-62
@@ -2,75 +2,48 @@ package cn.cloudwalk.intelligent.lock.common.model;
|
||||
|
||||
import cn.cloudwalk.intelligent.lock.common.enums.LockType;
|
||||
|
||||
public class LockInfo {
|
||||
private LockType type;
|
||||
private String name;
|
||||
private long lockWaitTime;
|
||||
private long leaseTime;
|
||||
|
||||
public LockInfo(LockType type, String name, long lockWaitTime, long leaseTime) {
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
this.lockWaitTime = lockWaitTime;
|
||||
this.leaseTime = leaseTime;
|
||||
}
|
||||
|
||||
public LockType getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public void setType(LockType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getLockWaitTime() {
|
||||
return this.lockWaitTime;
|
||||
}
|
||||
|
||||
public void setLockWaitTime(long lockWaitTime) {
|
||||
this.lockWaitTime = lockWaitTime;
|
||||
}
|
||||
|
||||
public long getLeaseTime() {
|
||||
return this.leaseTime;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class LockInfo
|
||||
{
|
||||
private LockType type;
|
||||
private String name;
|
||||
private long lockWaitTime;
|
||||
private long leaseTime;
|
||||
|
||||
public LockInfo(LockType type, String name, long lockWaitTime, long leaseTime) {
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
this.lockWaitTime = lockWaitTime;
|
||||
this.leaseTime = leaseTime;
|
||||
public void setLeaseTime(long leaseTime) {
|
||||
this.leaseTime = leaseTime;
|
||||
}
|
||||
}
|
||||
|
||||
public LockType getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public void setType(LockType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getLockWaitTime() {
|
||||
return this.lockWaitTime;
|
||||
}
|
||||
|
||||
public void setLockWaitTime(long lockWaitTime) {
|
||||
this.lockWaitTime = lockWaitTime;
|
||||
}
|
||||
|
||||
public long getLeaseTime() {
|
||||
return this.leaseTime;
|
||||
}
|
||||
|
||||
public void setLeaseTime(long leaseTime) {
|
||||
this.leaseTime = leaseTime;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+16
-44
@@ -10,51 +10,23 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class ExpressionParseUtil {
|
||||
private static ExpressionParser parser = (ExpressionParser) new SpelExpressionParser();
|
||||
|
||||
public static <T> T parse(ProceedingJoinPoint joinPoint, String spel, Class<T> clazz) {
|
||||
String[] parameterNames = (new LocalVariableTableParameterNameDiscoverer())
|
||||
.getParameterNames(((MethodSignature) joinPoint.getSignature()).getMethod());
|
||||
Object[] values = joinPoint.getArgs();
|
||||
|
||||
StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
|
||||
for (int i = 0; i < parameterNames.length; i++) {
|
||||
standardEvaluationContext.setVariable(parameterNames[i], values[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class ExpressionParseUtil
|
||||
{
|
||||
private static ExpressionParser parser = (ExpressionParser)new SpelExpressionParser();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static <T> T parse(ProceedingJoinPoint joinPoint, String spel, Class<T> clazz) {
|
||||
String[] parameterNames = (new LocalVariableTableParameterNameDiscoverer()).getParameterNames(((MethodSignature)joinPoint.getSignature()).getMethod());
|
||||
Object[] values = joinPoint.getArgs();
|
||||
|
||||
StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
|
||||
for (int i = 0; i < parameterNames.length; i++) {
|
||||
standardEvaluationContext.setVariable(parameterNames[i], values[i]);
|
||||
if (StringUtils.isEmpty(spel)) {
|
||||
return null;
|
||||
}
|
||||
Expression expression = parser.parseExpression(spel);
|
||||
return (T) expression.getValue((EvaluationContext) standardEvaluationContext, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(spel)) {
|
||||
return null;
|
||||
}
|
||||
Expression expression = parser.parseExpression(spel);
|
||||
return (T)expression.getValue((EvaluationContext)standardEvaluationContext, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+3
-5
@@ -1,9 +1,7 @@
|
||||
package cn.cloudwalk.intelligent.lock.locks;
|
||||
|
||||
public interface Lock {
|
||||
boolean acquire();
|
||||
|
||||
boolean unlock();
|
||||
boolean acquire();
|
||||
|
||||
boolean unlock();
|
||||
}
|
||||
|
||||
|
||||
|
||||
+6
-18
@@ -4,23 +4,11 @@ import cn.cloudwalk.intelligent.lock.common.model.LockInfo;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class LockFactory {
|
||||
@Autowired
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class LockFactory
|
||||
{
|
||||
@Autowired
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
public Lock create(LockInfo lockInfo) {
|
||||
return new ReentrantLock(lockInfo, this.redissonClient);
|
||||
public Lock create(LockInfo lockInfo) {
|
||||
return new ReentrantLock(lockInfo, this.redissonClient);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+27
-44
@@ -6,51 +6,34 @@ import java.util.concurrent.TimeUnit;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
|
||||
public class ReentrantLock implements Lock {
|
||||
private LockInfo lockInfo;
|
||||
private RLock rLock;
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
public ReentrantLock(LockInfo lockInfo, RedissonClient redissonClient) {
|
||||
this.lockInfo = lockInfo;
|
||||
this.redissonClient = redissonClient;
|
||||
}
|
||||
|
||||
public boolean acquire() {
|
||||
try {
|
||||
this.rLock = this.redissonClient.getLock(this.lockInfo.getName());
|
||||
return this.rLock.tryLock(this.lockInfo.getLockWaitTime(), this.lockInfo.getLeaseTime(),
|
||||
TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
throw new LockOperationException("获取锁操作异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class ReentrantLock
|
||||
implements Lock
|
||||
{
|
||||
private LockInfo lockInfo;
|
||||
private RLock rLock;
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
public ReentrantLock(LockInfo lockInfo, RedissonClient redissonClient) {
|
||||
this.lockInfo = lockInfo;
|
||||
this.redissonClient = redissonClient;
|
||||
public boolean unlock() {
|
||||
if (this.rLock.isHeldByCurrentThread()) {
|
||||
try {
|
||||
return ((Boolean) this.rLock.forceUnlockAsync().get()).booleanValue();
|
||||
} catch (Exception e) {
|
||||
throw new LockOperationException("释放锁操作异常", e);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean acquire() {
|
||||
try {
|
||||
this.rLock = this.redissonClient.getLock(this.lockInfo.getName());
|
||||
return this.rLock.tryLock(this.lockInfo.getLockWaitTime(), this.lockInfo.getLeaseTime(), TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
throw new LockOperationException("获取锁操作异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public boolean unlock() {
|
||||
if (this.rLock.isHeldByCurrentThread()) {
|
||||
try {
|
||||
return ((Boolean)this.rLock.forceUnlockAsync().get()).booleanValue();
|
||||
} catch (Exception e) {
|
||||
throw new LockOperationException("释放锁操作异常", e);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-3
@@ -5,7 +5,5 @@ import cn.cloudwalk.intelligent.lock.locks.Lock;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
|
||||
public interface LockAcquireTimeoutHandler {
|
||||
void handle(LockInfo paramLockInfo, Lock paramLock, JoinPoint paramJoinPoint);
|
||||
void handle(LockInfo paramLockInfo, Lock paramLock, JoinPoint paramJoinPoint);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+31
-56
@@ -6,66 +6,41 @@ import cn.cloudwalk.intelligent.lock.locks.Lock;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
|
||||
public enum LockAcquireTimeoutStrategy implements LockAcquireTimeoutHandler {
|
||||
NO_OPERATION {
|
||||
|
||||
public void handle(LockInfo lockInfo, Lock lock, JoinPoint joinPoint) {
|
||||
}
|
||||
},
|
||||
FAIL_FAST {
|
||||
public void handle(LockInfo lockInfo, Lock lock, JoinPoint joinPoint) {
|
||||
throw new AcquireLockTimeoutException(String.format("获取锁(%s)超时(%dms)",
|
||||
new Object[] { lockInfo.getName(), Long.valueOf(lockInfo.getLockWaitTime())
|
||||
|
||||
}));
|
||||
}
|
||||
},
|
||||
KEEP_ACQUIRE {
|
||||
private static final long DEFAULT_INTERVAL = 100L;
|
||||
|
||||
private static final long DEFAULT_MAX_INTERVAL = 180000L;
|
||||
|
||||
public void handle(LockInfo lockInfo, Lock lock, JoinPoint joinPoint) {
|
||||
long interval = 100L;
|
||||
while (!lock.acquire()) {
|
||||
|
||||
if (interval > 180000L) {
|
||||
throw new AcquireLockTimeoutException(
|
||||
String.format("获取锁(%s)阻塞时间过长", new Object[] { lockInfo.getName() }));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public enum LockAcquireTimeoutStrategy
|
||||
implements LockAcquireTimeoutHandler
|
||||
{
|
||||
NO_OPERATION
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void handle(LockInfo lockInfo, Lock lock, JoinPoint joinPoint) {}
|
||||
},
|
||||
FAIL_FAST
|
||||
{
|
||||
public void handle(LockInfo lockInfo, Lock lock, JoinPoint joinPoint) {
|
||||
throw new AcquireLockTimeoutException(String.format("获取锁(%s)超时(%dms)", new Object[] { lockInfo.getName(),
|
||||
Long.valueOf(lockInfo.getLockWaitTime())
|
||||
|
||||
|
||||
|
||||
}));
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(interval);
|
||||
interval <<= 1L;
|
||||
} catch (InterruptedException e) {
|
||||
throw new AcquireLockTimeoutException(String.format("获取锁(%s)失)", new Object[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
KEEP_ACQUIRE
|
||||
{
|
||||
private static final long DEFAULT_INTERVAL = 100L;
|
||||
|
||||
private static final long DEFAULT_MAX_INTERVAL = 180000L;
|
||||
|
||||
|
||||
public void handle(LockInfo lockInfo, Lock lock, JoinPoint joinPoint) {
|
||||
long interval = 100L;
|
||||
while (!lock.acquire()) {
|
||||
|
||||
if (interval > 180000L) {
|
||||
throw new AcquireLockTimeoutException(String.format("获取锁(%s)阻塞时间过长", new Object[] { lockInfo.getName() }));
|
||||
}
|
||||
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(interval);
|
||||
interval <<= 1L;
|
||||
} catch (InterruptedException e) {
|
||||
throw new AcquireLockTimeoutException(String.format("获取锁(%s)失)", new Object[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
+10
-25
@@ -2,31 +2,16 @@ package cn.cloudwalk.intelligent.lock.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ConfigurationProperties(prefix = "intelligent.lock.config")
|
||||
public class LockProperties
|
||||
{
|
||||
public static final String PREFIX = "intelligent.lock.config";
|
||||
private Long defaultWaitTime = Long.valueOf(15000L);
|
||||
public class LockProperties {
|
||||
public static final String PREFIX = "intelligent.lock.config";
|
||||
private Long defaultWaitTime = Long.valueOf(15000L);
|
||||
|
||||
public void setDefaultWaitTime(Long defaultWaitTime) {
|
||||
this.defaultWaitTime = defaultWaitTime;
|
||||
public void setDefaultWaitTime(Long defaultWaitTime) {
|
||||
this.defaultWaitTime = defaultWaitTime;
|
||||
}
|
||||
|
||||
public Long getDefaultWaitTime() {
|
||||
return this.defaultWaitTime;
|
||||
}
|
||||
}
|
||||
|
||||
public Long getDefaultWaitTime() {
|
||||
return this.defaultWaitTime;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user