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 4203f251111d11aa83e5ee9dadb921304a8606fa..5271f1abf2b67deb38fc32a98ed6bb7c0c056e3a 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 81d140d7c19769868d67ac1f2331f0c5d5f18470..cca8fbe8df29252840e50a6197312bd32d8047fd 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 +}