Commit 63a2f096 by Ren Ping

feat:自动任务增加redis锁

1 parent 143a4fef
...@@ -35,14 +35,14 @@ public class RedissonUtil { ...@@ -35,14 +35,14 @@ public class RedissonUtil {
* @param lockKey * @param lockKey
* @param operation * @param operation
*/ */
public static void lockOperation(String lockKey, Runnable operation) { public static void lockOperation(String lockKey, long timeout, Runnable operation) {
RedissonClient redissonClient = SpringUtil.getBean(RedissonClient.class); RedissonClient redissonClient = SpringUtil.getBean(RedissonClient.class);
RLock lock = redissonClient.getLock(lockKey); RLock lock = redissonClient.getLock(lockKey);
try { try {
while (true) { while (true) {
//第一个参数:尝试获取锁的最大等待时间,超过这个值,则获取失败 //第一个参数:尝试获取锁的最大等待时间,超过这个值,则获取失败
//第二个参数:锁的持有时间,超过这个时间锁会自动失效 //第二个参数:锁的持有时间,超过这个时间锁会自动失效
boolean res = lock.tryLock(60, 60, TimeUnit.MINUTES); boolean res = lock.tryLock(timeout, timeout, TimeUnit.MINUTES);
if (res) { if (res) {
log.info(">>> " + Thread.currentThread() + " 获取 redis lock"); log.info(">>> " + Thread.currentThread() + " 获取 redis lock");
//成功获得锁,在这里处理业务 //成功获得锁,在这里处理业务
......
...@@ -114,7 +114,7 @@ public class SchedulerServiceImpl implements SchedulerService { ...@@ -114,7 +114,7 @@ public class SchedulerServiceImpl implements SchedulerService {
for (int i = 1; i <= nextDaysLimit; i++) { for (int i = 1; i <= nextDaysLimit; i++) {
String currDay = LocalDate.now().plusDays(i).format(DateTimeFormatter.ISO_LOCAL_DATE); String currDay = LocalDate.now().plusDays(i).format(DateTimeFormatter.ISO_LOCAL_DATE);
boolean finalCutOff = cutOff; boolean finalCutOff = cutOff;
RedissonUtil.lockOperation(AutoDispatchJob.TEAM_JOB_PREFIX + teamId, () -> { RedissonUtil.lockOperation(AutoDispatchJob.TEAM_JOB_PREFIX + teamId, 60, () -> {
dispatchRun2OneDay(teamId, currDay, today, finalCutOff); dispatchRun2OneDay(teamId, currDay, today, finalCutOff);
}); });
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!