use of com.easy.cloud.core.lock.annotation.EcLockAnnotation in project dq-easy-cloud by dq-open-cloud.
the class EcLockBO method buildEcDistributedLock.
/**
* 根据方法构建数据传输对象的分布式注解
*/
private EcLockBO buildEcDistributedLock() {
// 注解为空先构建注解
if (EcBaseUtils.isNull(this.lockDTO.getLockAnnotation())) {
EcLockAnnotation distributedLock = targetMethod.getAnnotation(EcLockAnnotation.class);
Assert.notNull(distributedLock, "distributedLock注解不能为空");
this.lockDTO.setLock(distributedLock);
}
return this;
}
use of com.easy.cloud.core.lock.annotation.EcLockAnnotation in project dq-easy-cloud by dq-open-cloud.
the class EcLockBO method buildLockTemplate.
private EcLockBO buildLockTemplate() {
// 先构建注解
EcLockAnnotation distributedLock = buildEcDistributedLock().lockDTO.getLockAnnotation();
// 根据类型选择分布式锁模板对象
EcLockTemplate lockTemplate = this.lockTemplateSelector.selectLockTemplateByType(distributedLock.templateType());
this.lockDTO.setLockTemplate(lockTemplate);
return this;
}
use of com.easy.cloud.core.lock.annotation.EcLockAnnotation in project dq-easy-cloud by dq-open-cloud.
the class EcLockTemplateRedission method tryLock.
@Override
public <T> T tryLock(EcLockCallback<T> callback) {
EcLockDTO lockDTO = callback.getDistributedLockDTO();
EcLockAnnotation lockAnnotation = lockDTO.getLockAnnotation();
RLock lock = getLock(lockDTO.getLockNameFull(), lockAnnotation.type());
boolean isGainLock = false;
try {
isGainLock = lock.tryLock(lockAnnotation.waitTime(), lockAnnotation.leaseTime(), lockAnnotation.timeUnit());
return callback.process(new EcLockResult(isGainLock));
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (lock != null && lock.isLocked() && isGainLock) {
lock.unlock();
}
}
}
use of com.easy.cloud.core.lock.annotation.EcLockAnnotation in project dq-easy-cloud by dq-open-cloud.
the class EcLockTemplateJedis method lock.
@Override
public <T> T lock(EcLockCallback<T> callback) {
EcLockDTO lockDTO = callback.getDistributedLockDTO();
EcLockAnnotation lockAnnotation = lockDTO.getLockAnnotation();
EcLogUtils.debug("锁的注解信息", lockAnnotation, logger);
String lockNameFull = lockDTO.getLockNameFull();
EcLock ecLock = new EcLock(stringRedisTemplateLock, lockNameFull, lockAnnotation.type());
try {
boolean gainLock = ecLock.lock(lockAnnotation.waitTime(), lockAnnotation.leaseTime(), lockAnnotation.timeUnit());
if (gainLock) {
return callback.process(new EcLockResult(true));
}
return callback.process(new EcLockResult(false));
} finally {
ecLock.unlock();
}
}
use of com.easy.cloud.core.lock.annotation.EcLockAnnotation in project dq-easy-cloud by dq-open-cloud.
the class EcLockBO method buildLockName.
/**
* <p>
* 构建锁的名称
* </p>
*
* @param args
* @return
* @author daiqi
* @创建时间 2018年4月13日 上午11:37:42
*/
private EcLockBO buildLockName(Object[] args) {
// 注解为空先构建注解
EcLockAnnotation distributedLock = buildEcDistributedLock().lockDTO.getLockAnnotation();
String lockName = distributedLock.nameBody();
if (EcStringUtils.isEmpty(lockName)) {
if (EcArrayUtils.isEmpty(args)) {
throw new EcBaseBusinessException(EcBaseErrorCodeEnum.OBJECT_CANT_NULL);
}
String param = distributedLock.param();
int argNum = distributedLock.argNum();
// 注解中param为empty,直接使用方法形参所在的位置的值作为lockName
if (EcStringUtils.isEmpty(param)) {
lockName = String.valueOf(getArgByArgNum(args, argNum));
} else {
Object arg = getArgByArgNum(args, argNum);
lockName = getParamValue(arg, param);
}
}
if (EcStringUtils.isEmpty(lockName)) {
throw new EcBaseBusinessException(EcLockErrorCodeEnum.LOCK_NAME_CANT_EMPTY);
}
this.lockDTO.setLockName(lockName);
return this;
}
Aggregations