Search in sources :

Example 1 with EcLockAnnotation

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;
}
Also used : EcLockAnnotation(com.easy.cloud.core.lock.annotation.EcLockAnnotation)

Example 2 with EcLockAnnotation

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;
}
Also used : EcLockAnnotation(com.easy.cloud.core.lock.annotation.EcLockAnnotation) EcLockTemplate(com.easy.cloud.core.lock.template.EcLockTemplate)

Example 3 with EcLockAnnotation

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();
        }
    }
}
Also used : EcLockAnnotation(com.easy.cloud.core.lock.annotation.EcLockAnnotation) EcLockDTO(com.easy.cloud.core.lock.pojo.dto.EcLockDTO) EcLockResult(com.easy.cloud.core.lock.callback.result.EcLockResult) RLock(org.redisson.api.RLock)

Example 4 with EcLockAnnotation

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();
    }
}
Also used : EcLockAnnotation(com.easy.cloud.core.lock.annotation.EcLockAnnotation) EcLockDTO(com.easy.cloud.core.lock.pojo.dto.EcLockDTO) EcLockResult(com.easy.cloud.core.lock.callback.result.EcLockResult) EcLock(com.easy.cloud.core.lock.pojo.EcLock)

Example 5 with EcLockAnnotation

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;
}
Also used : EcLockAnnotation(com.easy.cloud.core.lock.annotation.EcLockAnnotation) EcBaseBusinessException(com.easy.cloud.core.exception.bo.EcBaseBusinessException) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint)

Aggregations

EcLockAnnotation (com.easy.cloud.core.lock.annotation.EcLockAnnotation)8 EcLockResult (com.easy.cloud.core.lock.callback.result.EcLockResult)4 EcLockDTO (com.easy.cloud.core.lock.pojo.dto.EcLockDTO)4 EcBaseBusinessException (com.easy.cloud.core.exception.bo.EcBaseBusinessException)2 EcLock (com.easy.cloud.core.lock.pojo.EcLock)2 RLock (org.redisson.api.RLock)2 EcLockTemplate (com.easy.cloud.core.lock.template.EcLockTemplate)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1