优化流程提醒的加锁机制

FlowLong 起多个实例时,比如业务使用 Redis 加锁,应该只有一个实例进行流程提醒处理,其他实例应该立即返回,而不是线程阻塞
This commit is contained in:
songyinyin 2024-04-12 11:45:50 +08:00
parent 5401795039
commit 24d89e63ae
5 changed files with 19 additions and 6 deletions

1
.gitignore vendored
View File

@ -20,3 +20,4 @@ bin/
jdbc.properties
**/target/
.flattened-pom.xml

View File

@ -16,9 +16,11 @@ package com.aizuda.bpm.engine.scheduling;
public interface JobLock {
/**
* 进入锁
* 进入锁获取锁立即返回不会阻塞等待锁
*
* @return true -> 获取到锁false -> 未获取到锁
*/
void lock();
boolean tryLock();
/**
* 解除锁

View File

@ -33,8 +33,8 @@ public class LocalLock implements JobLock {
}
@Override
public void lock() {
getLocalLock().lock();
public boolean tryLock() {
return getLocalLock().tryLock();
}
@Override

View File

@ -13,6 +13,7 @@ import com.aizuda.bpm.engine.scheduling.RemindParam;
import com.aizuda.bpm.engine.scheduling.TaskReminder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.List;
@ -27,6 +28,7 @@ import java.util.List;
* @author noear
* @since 1.0
*/
@Slf4j
@Setter
@Getter
public class SolonScheduler {
@ -53,7 +55,10 @@ public class SolonScheduler {
*/
public void remind() {
try {
jobLock.lock();
if (!jobLock.tryLock()) {
log.info("[FlowLong] remind is already running, just return.");
return;
}
TaskService taskService = context.getTaskService();
List<FlwTask> flwTaskList = taskService.getTimeoutOrRemindTasks();
if (ObjectUtils.isNotEmpty(flwTaskList)) {

View File

@ -10,6 +10,7 @@ import com.aizuda.bpm.engine.scheduling.RemindParam;
import com.aizuda.bpm.engine.scheduling.TaskReminder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
@ -28,6 +29,7 @@ import java.util.List;
* @author hubin
* @since 1.0
*/
@Slf4j
@Getter
@Setter
public class SpringBootScheduler implements SchedulingConfigurer {
@ -53,7 +55,10 @@ public class SpringBootScheduler implements SchedulingConfigurer {
*/
public void remind() {
try {
jobLock.lock();
if (!jobLock.tryLock()) {
log.info("[FlowLong] remind is already running, just return.");
return;
}
TaskService taskService = context.getTaskService();
List<FlwTask> flwTaskList = taskService.getTimeoutOrRemindTasks();
if (ObjectUtils.isNotEmpty(flwTaskList)) {