mirror of
https://gitee.com/wangkang/udf.git
synced 2026-10-20 15:03:43 +00:00
修复禁止并发job的问题
This commit is contained in:
Vendored
+6
@@ -1,5 +1,6 @@
|
||||
package org.itkk.udf.cache.redis.lock;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.itkk.udf.cache.redis.CacheRedisProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
@@ -11,6 +12,7 @@ import java.util.concurrent.TimeUnit;
|
||||
* RedisBasedDistributedLock
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class RedisBasedDistributedLock {
|
||||
|
||||
/**
|
||||
@@ -60,6 +62,8 @@ public class RedisBasedDistributedLock {
|
||||
//设置超时时间
|
||||
redisTemplate.expire(key, timeout, timeUnit);
|
||||
}
|
||||
//日志输出
|
||||
log.info("RedisBasedDistributedLock : lock key : {} : {}", key, lock);
|
||||
//返回
|
||||
return lock;
|
||||
}
|
||||
@@ -74,6 +78,8 @@ public class RedisBasedDistributedLock {
|
||||
String key = cacheRedisProperties.getPrefix() + DISTRIBUTED_LOCK_KEY_PREFIX + name;
|
||||
//删除缓存
|
||||
redisTemplate.delete(key);
|
||||
//日志输出
|
||||
log.info("RedisBasedDistributedLock : unlock key : {}", key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
-1
@@ -39,6 +39,11 @@ import java.util.concurrent.TimeUnit;
|
||||
@Slf4j
|
||||
public abstract class AbstractBaseRmsJob extends AbstractBaseJob {
|
||||
|
||||
/**
|
||||
* RMS_JOB_DISALLOW_CONCURRENT_PREFIX
|
||||
*/
|
||||
public static final String RMS_JOB_DISALLOW_CONCURRENT_PREFIX = "rmsJobDisallowConcurrent_";
|
||||
|
||||
/**
|
||||
* redisBasedDistributedLock
|
||||
*/
|
||||
@@ -85,13 +90,18 @@ public abstract class AbstractBaseRmsJob extends AbstractBaseJob {
|
||||
*/
|
||||
protected void disallowConcurrentExecute(RmsJobParam rmsJobParam) throws JobExecutionException {
|
||||
//定义锁信息 (锁超时时间为60分钟) (异步任务,解锁在callback中完成,同步任务,解锁在本方法完成)
|
||||
String name = "rmsJobDisallowConcurrent_" + rmsJobParam.getServiceCode() + "_" + rmsJobParam.getBeanName();
|
||||
String name = AbstractBaseRmsJob.RMS_JOB_DISALLOW_CONCURRENT_PREFIX + rmsJobParam.getServiceCode() + "_" + rmsJobParam.getBeanName();
|
||||
final long timeout = 60;
|
||||
TimeUnit timeUnit = TimeUnit.MINUTES;
|
||||
//尝试锁定,锁定成功则触发,锁定失败则跳过
|
||||
if (redisBasedDistributedLock.lock(name, timeout, timeUnit, rmsJobParam)) {
|
||||
try {
|
||||
this.execute(rmsJobParam);
|
||||
} catch (JobExecutionException e) {
|
||||
//解锁
|
||||
redisBasedDistributedLock.unlock(name);
|
||||
//抛出异常
|
||||
throw e;
|
||||
} finally {
|
||||
//同步任务解锁
|
||||
if (!rmsJobParam.getAsync()) {
|
||||
|
||||
+2
-1
@@ -10,6 +10,7 @@ import org.itkk.udf.cache.redis.lock.RedisBasedDistributedLock;
|
||||
import org.itkk.udf.core.RestResponse;
|
||||
import org.itkk.udf.scheduler.IRmsJobEvent;
|
||||
import org.itkk.udf.scheduler.client.domain.RmsJobResult;
|
||||
import org.itkk.udf.scheduler.job.AbstractBaseRmsJob;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -43,7 +44,7 @@ public class SchClientCallbackController implements ISchClientCallbackController
|
||||
return new RestResponse<>();
|
||||
} finally {
|
||||
//获得key
|
||||
String name = result.getParam().getServiceCode() + "_" + result.getParam().getBeanName();
|
||||
String name = AbstractBaseRmsJob.RMS_JOB_DISALLOW_CONCURRENT_PREFIX + result.getParam().getServiceCode() + "_" + result.getParam().getBeanName();
|
||||
//解锁
|
||||
redisBasedDistributedLock.unlock(name);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user