How To Release Redisson RLock

Intro How To Release Redisson RLock

這篇介紹How To Release Redisson RLock.

Intro:

RLock lock = null;
try {
lock = LockDAO.getLock(event);
if (!lock.isLocked()) {
lock.lock(SystemConstant.REDIS_LOCK_EXPIRE_IN_MINUTES, TimeUnit.MINUTES);
System.out.println(i);
Thread.sleep(((long)(Math.random() * 1000)));
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
Optional.ofNullable(lock).filter(x -> x.isHeldByCurrentThread()).filter(x -> x.isLocked())
.ifPresent(x -> x.unlock());
}

經測試使用

1.Optional.ofNullable(lock).filter(x -> x.isHeldByCurrentThread()).filter(x -> x.isLocked())
.ifPresent(x -> x.unlock());
2.設定lock(long leaseTime, TimeUnit unit),leaseTime到期自動釋放
兩種方法皆會Release Redisson RLock。