我有一些类使用Scheduled annotation注释方法。
当我在Tomcat中停止/取消部署/重新部署此应用程序时,我收到了这样的消息:
"The following web applications were stopped (reloaded, undeployed), but their classes from previous runs are still loaded in memory, thus causing a memory leak (use a profiler to confirm):
/my-app
/my-app
/my-app"
我已经尝试过" autowire" ContextClosed处理程序中的ThreadPoolTaskExecutor并使用shutdown()方法将其停止。但它没有任何帮助。似乎预定的方法(线程)不受Tomcat控制。
我也尝试过设置taskExecutor.setWaitForTasksToCompleteOnShutdown(false)。
当我获得堆的转储时,我只看到包含Scheduled方法和与之关联的类的类(实例)。
这会导致我们的服务器()在几天内停止响应请求时出现问题。
配置:
@Configuration
@EnableWebMvc
@EnableScheduling
public class AppConfig {
@Bean
public ThreadPoolTaskScheduler taskScheduler(){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(20);
executor.setMaxPoolSize(40);
executor.setQueueCapacity(400);
executor.setWaitForTasksToCompleteOnShutdown(false);
executor.initialize();
return executor;
}
}
调度程序:
@Component
public class HistoryCleaner {
@Autowired
private HistoryService historyService;
@Autowired
private AppProperties app;
@Scheduled(fixedDelay = 10000)
public void cleanHistory() {
...
}
}