Skip to content

Commit

Permalink
Merge pull request #392 from trevzhang/dev
Browse files Browse the repository at this point in the history
perf: 刷新上下文避免重复创建调度任务
  • Loading branch information
KamToHung authored Jan 20, 2024
2 parents 09bf614 + 953e505 commit 193efd4
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import static org.dromara.dynamictp.common.constant.DynamicTpConst.SCHEDULE_NOTIFY_ITEMS;
Expand All @@ -51,13 +52,26 @@ public class DtpMonitor extends OnceApplicationContextEventListener {

private final DtpProperties dtpProperties;

private ScheduledFuture<?> monitorFuture;

private int monitorInterval;

public DtpMonitor(DtpProperties dtpProperties) {
this.dtpProperties = dtpProperties;
}

@Override
protected void onContextRefreshedEvent(ContextRefreshedEvent event) {
MONITOR_EXECUTOR.scheduleWithFixedDelay(this::run,
protected synchronized void onContextRefreshedEvent(ContextRefreshedEvent event) {
// if monitorInterval is same as before, do nothing.
if (monitorInterval == dtpProperties.getMonitorInterval()) {
return;
}
// cancel old monitor task.
if (monitorFuture != null) {
monitorFuture.cancel(true);
}
monitorInterval = dtpProperties.getMonitorInterval();
monitorFuture = MONITOR_EXECUTOR.scheduleWithFixedDelay(this::run,
0, dtpProperties.getMonitorInterval(), TimeUnit.SECONDS);
}

Expand Down

0 comments on commit 193efd4

Please sign in to comment.