Search in sources :

Example 6 with EcLockAnnotation

use of com.easy.cloud.core.lock.annotation.EcLockAnnotation in project dq-easy-cloud by dq-open-cloud.

the class EcLockBO method buildDistributedLockResultProcessor.

/**
 * <p>
 * 构建分布式锁结果处理对象
 * </p>
 *
 * @return
 * @author daiqi
 * @创建时间 2018年4月14日 下午12:00:15
 */
private EcLockBO buildDistributedLockResultProcessor() {
    // 先构建注解
    EcLockAnnotation distributedLock = buildEcDistributedLock().lockDTO.getLockAnnotation();
    Class<? extends EcBaseLockResultProcessor> resultProcessorClass = distributedLock.resultProcessorClass();
    // 结果处理类class为空 使用默认的结果处理类进行处理
    if (EcBaseUtils.isNull(resultProcessorClass)) {
        resultProcessorClass = EcDefaultLockResultProcessor.class;
    }
    // 根据class创建结果处理对象
    resultProcessor = EcBeanFactory.newInstance(resultProcessorClass);
    if (EcBaseUtils.isNull(resultProcessor)) {
        throw new EcBaseBusinessException(EcLockErrorCodeEnum.RESULT_PROCESSOR_OBJ_CREATE_FAIL);
    }
    return this;
}
Also used : EcLockAnnotation(com.easy.cloud.core.lock.annotation.EcLockAnnotation) EcBaseBusinessException(com.easy.cloud.core.exception.bo.EcBaseBusinessException)

Example 7 with EcLockAnnotation

use of com.easy.cloud.core.lock.annotation.EcLockAnnotation in project dq-easy-cloud by dq-open-cloud.

the class EcLockTemplateRedission method lock.

@Override
public <T> T lock(EcLockCallback<T> callback) {
    EcLockDTO lockDTO = callback.getDistributedLockDTO();
    EcLockAnnotation lockAnnotation = lockDTO.getLockAnnotation();
    EcLogUtils.debug("锁的注解信息", lockAnnotation, logger);
    RLock lock = getLock(lockDTO.getLockNameFull(), lockAnnotation.type());
    try {
        lock.lock(lockAnnotation.leaseTime(), lockAnnotation.timeUnit());
        return callback.process(new EcLockResult(true));
    } finally {
        if (lock != null && lock.isLocked()) {
            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 8 with EcLockAnnotation

use of com.easy.cloud.core.lock.annotation.EcLockAnnotation in project dq-easy-cloud by dq-open-cloud.

the class EcLockTemplateJedis method tryLock.

@Override
public <T> T tryLock(EcLockCallback<T> callback) {
    EcLockDTO lockDTO = callback.getDistributedLockDTO();
    EcLockAnnotation lockAnnotation = lockDTO.getLockAnnotation();
    EcLock ecLock = new EcLock(stringRedisTemplateLock, lockDTO.getLockNameFull(), lockAnnotation.type());
    boolean isGainLock = false;
    try {
        isGainLock = ecLock.tryLock(lockAnnotation.waitTime(), lockAnnotation.leaseTime(), lockAnnotation.timeUnit());
        return callback.process(new EcLockResult(isGainLock));
    } catch (InterruptedException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } 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)

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