From c2b62fac6dc79b4cb829d96f17f2b7df089abd4b Mon Sep 17 00:00:00 2001 From: sh Date: Thu, 2 Apr 2026 17:46:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=9A=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E6=89=A7=E8=A1=8C=E4=BB=BB=E5=8A=A1=E6=83=85?= =?UTF-8?q?=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/resources/mapper/app/JobMapper.xml | 11 ++++-- .../core/redis/DistributedLockUtil.java | 38 +++++++++++++++++++ .../service/impl/SysJobServiceImpl.java | 2 +- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/ruoyi-bussiness/src/main/resources/mapper/app/JobMapper.xml b/ruoyi-bussiness/src/main/resources/mapper/app/JobMapper.xml index 4ab2b11..25a17b9 100644 --- a/ruoyi-bussiness/src/main/resources/mapper/app/JobMapper.xml +++ b/ruoyi-bussiness/src/main/resources/mapper/app/JobMapper.xml @@ -396,10 +396,13 @@ - update job set job_status='1' where del_flag='0' AND job_id in - - #{jobId} - + update job set job_status='1' where del_flag='0' + + AND job_id in + + #{jobId} + + \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/DistributedLockUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/DistributedLockUtil.java index a7b87da..a0409eb 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/DistributedLockUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/DistributedLockUtil.java @@ -28,6 +28,7 @@ public class DistributedLockUtil { private static final long DEFAULT_EXPIRE_SECONDS = 30; private static final long DEFAULT_ACQUIRE_TIMEOUT_SECONDS = 5; private static final long RENEW_INTERVAL_SECONDS = DEFAULT_EXPIRE_SECONDS / 3; + private static final long DEFAULT_EXPIRE_SECONDS_15 = 900; // 续期线程池 private final ScheduledExecutorService renewExecutor = Executors.newScheduledThreadPool( @@ -267,4 +268,41 @@ public class DistributedLockUtil { // 使用默认的锁过期时间(30秒),也可根据需要改为让调用方传入 return acquireLockWithRenewal(lockKey, DEFAULT_EXPIRE_SECONDS, acquireTimeoutSeconds); } + + /** + * 设置15min + * @param lockKey + * @param timeout + * @param unit + * @return + */ + public String acquireLockWith15Timeout(String lockKey, long timeout, TimeUnit unit) { + // 转换超时时间为秒(向上取整避免精度丢失) + long acquireTimeoutSeconds = (long) Math.ceil((double) unit.toMillis(timeout) / 1000); + // 使用默认的锁过期时间(30秒),也可根据需要改为让调用方传入 + return acquireLockWithRenewal(lockKey, DEFAULT_EXPIRE_SECONDS_15, acquireTimeoutSeconds); + } + + /** + * 定时任务 + * @param lockKey + * @param timeout + * @param unit + * @return + */ + public AutoReleaseLock tryLockJob(String lockKey, long timeout, TimeUnit unit) { + if (lockKey == null || lockKey.trim().isEmpty()) { + throw new IllegalArgumentException("锁key不能为空"); + } + if (timeout < 0) { + throw new IllegalArgumentException("超时时间不能为负数"); + } + if (unit == null) { + throw new IllegalArgumentException("时间单位不能为空"); + } + + String identifier = acquireLockWith15Timeout(lockKey, timeout, unit); + + return new AutoReleaseLock(this, lockKey, identifier); + } } \ No newline at end of file diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/SysJobServiceImpl.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/SysJobServiceImpl.java index 4e1a7a5..32d2f58 100644 --- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/SysJobServiceImpl.java +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/SysJobServiceImpl.java @@ -48,7 +48,7 @@ public class SysJobServiceImpl implements ISysJobService public void init() throws SchedulerException, TaskException { try (DistributedLockUtil.AutoReleaseLock autoLock = - distributedLockUtil.tryLock(JOB_INIT_LOCK_KEY, LOCK_ACQUIRE_TIMEOUT_SECONDS, TimeUnit.SECONDS)) { + distributedLockUtil.tryLockJob(JOB_INIT_LOCK_KEY, LOCK_ACQUIRE_TIMEOUT_SECONDS, TimeUnit.SECONDS)) { if (autoLock == null || !autoLock.isLocked()) { System.out.println("【定时任务初始化】其他实例已持有锁,当前实例跳过初始化"); return;