Tomcat memory leak warning on HttpURLConnection

守給你的承諾、 提交于 2019-12-05 21:40:56

You are using @Scheduled within Tomcat to spawn threads. You must ensure that these threads will finish when the ServletContext is destroyed e.g. WAR being undeployed, Tomcat warns you about this. The HTTP request code is unrelated as URLConnection doesn't start new threads to perform the request.

One way to make Tomcat happy is to use daemon threads, as explained in this answer. This can be done with custom taskScheduler bean:

@Configuration
@EnableScheduling
public class TaskConfiguration {

  @Bean(destroyMethod = "shutdown")
  public Executor taskScheduler() {
    return Executors.Executors.newFixedThreadPool(4,
        new ThreadFactory() {
          public Thread newThread(Runnable r) {
            Thread t = Executors.defaultThreadFactory().newThread(r);
            t.setDaemon(true);
            return t;
          }
        });
  }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!