Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: 刷新上下文避免重复创建调度任务 #393

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading