Search in sources :

Example 1 with EcLockDTO

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

use of com.easy.cloud.core.lock.pojo.dto.EcLockDTO 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 3 with EcLockDTO

use of com.easy.cloud.core.lock.pojo.dto.EcLockDTO 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 4 with EcLockDTO

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