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

recreate sechedule pool on context refreshed #529

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -50,7 +50,7 @@
@Slf4j
public class DtpMonitor {

private static final ScheduledExecutorService MONITOR_EXECUTOR = ThreadPoolCreator.newScheduledThreadPool("dtp-monitor", 1);
private static ScheduledExecutorService MONITOR_EXECUTOR;

private final DtpProperties dtpProperties;

Expand All @@ -73,6 +73,9 @@ public synchronized void onContextRefreshedEvent(CustomContextRefreshedEvent eve
if (monitorFuture != null) {
monitorFuture.cancel(true);
}
if (MONITOR_EXECUTOR == null || MONITOR_EXECUTOR.isShutdown() || MONITOR_EXECUTOR.isTerminated()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里优化感也不是何必要,除非改成是否开启监控的配置决定出是否实例化

Copy link
Author

@sage417 sage417 Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在多次执行onContextRefreshedEvent时这个判断是必要的,在dev tool classloader restart时,全局共享的MONITOR_EXECUTOR先会被shutdown,dtpmonitor创建新实例后重新执行onContextRefreshedEvent,这时复用老的MONITOR_EXECUTOR会发生RejectedExecutionException

简单来说dtpmonitor二次实例化会出现RejectedExecutionException

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在多次执行onContextRefreshedEvent时这个判断是必要的,在dev tool classloader restart时,全局共享的MONITOR_EXECUTOR先会被shutdown,dtpmonitor创建新实例后重新执行onContextRefreshedEvent,这时复用老的MONITOR_EXECUTOR会发生RejectedExecutionException

简单来说dtpmonitor二次实例化会出现RejectedExecutionException

确实,在SmartLifecycle中stop的时候shutdown了,确实如果restart要这么处理

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那就不只是DtpMonitor中静态线程池受影响,其他功能类中的也受影响吧。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

很多框架好像对devtools支持都不太好

MONITOR_EXECUTOR = ThreadPoolCreator.newScheduledThreadPool("dtp-monitor", 1);
}
monitorInterval = dtpProperties.getMonitorInterval();
monitorFuture = MONITOR_EXECUTOR.scheduleWithFixedDelay(this::run,
0, dtpProperties.getMonitorInterval(), TimeUnit.SECONDS);
Expand Down