Search in sources :

Example 1 with EcLock

use of com.easy.cloud.core.lock.pojo.EcLock 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 2 with EcLock

use of com.easy.cloud.core.lock.pojo.EcLock 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)2 EcLockResult (com.easy.cloud.core.lock.callback.result.EcLockResult)2 EcLock (com.easy.cloud.core.lock.pojo.EcLock)2 EcLockDTO (com.easy.cloud.core.lock.pojo.dto.EcLockDTO)2