Skip to content

Commit

Permalink
Merge pull request #393 from trevzhang/master
Browse files Browse the repository at this point in the history
perf: 刷新上下文避免重复创建调度任务
  • Loading branch information
yanhom1314 committed Jan 23, 2024
2 parents 44744f8 + 8420b27 commit 9e75364
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 9e75364

Please sign in to comment.