修改多节点同时执行任务情况
This commit is contained in:
@@ -396,10 +396,13 @@
|
||||
</select>
|
||||
|
||||
<update id="updateJobDown" parameterType="java.util.List">
|
||||
update job set job_status='1' where del_flag='0' AND job_id in
|
||||
<foreach collection="list" item="jobId" open="(" close=")" separator=",">
|
||||
#{jobId}
|
||||
</foreach>
|
||||
update job set job_status='1' where del_flag='0'
|
||||
<if test="list != null and !list.isEmpty()">
|
||||
AND job_id in
|
||||
<foreach collection="list" item="jobId" open="(" close=")" separator=",">
|
||||
#{jobId}
|
||||
</foreach>
|
||||
</if>
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user