From 44a3923f1fd4e30f4bfa2048bab73431c06ccbd0 Mon Sep 17 00:00:00 2001 From: liaozan <378024053@qq.com> Date: Mon, 25 Sep 2023 20:19:30 +0800 Subject: [PATCH] Add leaseTime option for RedisLockUtils --- .../com/schbrain/common/util/IdWorker.java | 1 + .../util/support/lock/RedisLockUtils.java | 30 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/commons/common-util/src/main/java/com/schbrain/common/util/IdWorker.java b/commons/common-util/src/main/java/com/schbrain/common/util/IdWorker.java index 4203f25..5271f1a 100644 --- a/commons/common-util/src/main/java/com/schbrain/common/util/IdWorker.java +++ b/commons/common-util/src/main/java/com/schbrain/common/util/IdWorker.java @@ -23,6 +23,7 @@ import java.util.concurrent.ThreadLocalRandom; * 刚开始使用时一般为 18 位,但时间距离起始时间超过一定值后,会变为 19 位。 *

* 消耗完 18 位所需的时间:1 * 10^18 / (3600 * 24 * 365 * 1000 * 2^22) ≈ 7.56 年。 + * 消耗完 19 位所需的时间:1 * 10^19 / (3600 * 24 * 365 * 1000 * 2^22) ≈ 75.6 年。 *

* 所以从 2015-01-01 起,大概在 2022-07-22,即时间差超过 7.56 年,就会达到 19 位。 */ diff --git a/commons/common-util/src/main/java/com/schbrain/common/util/support/lock/RedisLockUtils.java b/commons/common-util/src/main/java/com/schbrain/common/util/support/lock/RedisLockUtils.java index 81d140d..cca8fbe 100644 --- a/commons/common-util/src/main/java/com/schbrain/common/util/support/lock/RedisLockUtils.java +++ b/commons/common-util/src/main/java/com/schbrain/common/util/support/lock/RedisLockUtils.java @@ -22,33 +22,47 @@ public class RedisLockUtils { private static final String APPLICATION_NAME = ApplicationName.get(); + private static final Duration DEFAULT_WAIT_TIME = Duration.ofSeconds(3); + /** + * if -1, then lock will be renew automatically by watchdog thread + */ + private static final Duration DEFAULT_LEASE_TIME = Duration.ofSeconds(-1); + private static RedissonClient CLIENT; public static void executeWithLock(String lockName, Runnable action) { - executeWithLock(lockName, Duration.ofSeconds(3), action); + executeWithLock(lockName, DEFAULT_WAIT_TIME, action); + } + + public static void executeWithLock(String lockName, Duration waitTime, Runnable action) { + executeWithLock(lockName, waitTime, DEFAULT_LEASE_TIME, action); } - public static void executeWithLock(String lockName, Duration timeout, Runnable action) { - executeWithLock(lockName, timeout, () -> { + public static void executeWithLock(String lockName, Duration waitTime, Duration leaseTime, Runnable action) { + executeWithLock(lockName, waitTime, leaseTime, () -> { action.run(); return null; }); } public static T executeWithLock(String lockName, Callable action) { - return executeWithLock(lockName, Duration.ofSeconds(3), action); + return executeWithLock(lockName, DEFAULT_WAIT_TIME, action); + } + + public static T executeWithLock(String lockName, Duration waitTime, Callable action) { + return executeWithLock(lockName, waitTime, DEFAULT_LEASE_TIME, action); } - public static T executeWithLock(String lockName, Duration timeout, Callable action) { + public static T executeWithLock(String lockName, Duration waitTime, Duration leaseTime, Callable action) { RLock lock = getClient().getLock(withPrefix(lockName)); boolean locked; try { - locked = lock.tryLock(timeout.toMillis(), TimeUnit.MILLISECONDS); + locked = lock.tryLock(waitTime.toMillis(), leaseTime.toMillis(), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new BaseException("Lock thread has been interrupted", e); } if (!locked) { - throw new BaseException(String.format("Lock cannot be acquired within %d seconds", timeout.toSeconds())); + throw new BaseException(String.format("Lock cannot be acquired within %d mills", waitTime.toMillis())); } try { @@ -77,4 +91,4 @@ public class RedisLockUtils { return String.join(StrPool.COLON, APPLICATION_NAME, "lock", lockName); } -} \ No newline at end of file +} -- GitLab