Skip to content

Commit

Permalink
优化流程提醒的加锁机制
Browse files Browse the repository at this point in the history
FlowLong 起多个实例时,比如业务使用 Redis 加锁,应该只有一个实例进行流程提醒处理,其他实例应该立即返回,而不是线程阻塞
  • Loading branch information
dudiao committed Apr 12, 2024
1 parent 5401795 commit 24d89e6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ bin/
jdbc.properties

**/target/
.flattened-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
public interface JobLock {

/**
* 进入锁
* 进入锁(获取锁),立即返回,不会阻塞等待锁
*
* @return true -> 获取到锁,false -> 未获取到锁
*/
void lock();
boolean tryLock();

/**
* 解除锁
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static Lock getLocalLock() {
}

@Override
public void lock() {
getLocalLock().lock();
public boolean tryLock() {
return getLocalLock().tryLock();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand All @@ -27,6 +28,7 @@
* @author noear
* @since 1.0
*/
@Slf4j
@Setter
@Getter
public class SolonScheduler {
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand All @@ -28,6 +29,7 @@
* @author hubin
* @since 1.0
*/
@Slf4j
@Getter
@Setter
public class SpringBootScheduler implements SchedulingConfigurer {
Expand All @@ -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)) {
Expand Down

0 comments on commit 24d89e6

Please sign in to comment.