Vaadin 7, Spring Boot, Tomcat 8
I am having difficult time cleaning up generated threads when the UI instance is no longer in use (session times out). I have a single UI application that contains an ExecutorService fixed thread pool. The detach method does not seem to get called and initiate the clean up. It appears that Tomcat becomes overrun with unused application threads. Where/how should this be done properly?
Any suggestions are greatly appreciated. Snippets of my application and UI classes below.
@Configuration
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@VaadinUI
@Push
@ComponentScan
public class OurUI extends UI implements ClientConnector.DetachListener {
final ExecutorService queryExecutor = Executors.newFixedThreadPool(40);
...
@Override
public void detach(DetachEvent event) {
// standard thread pool clean up
queryExecutor.shutdown();
try {
if (!queryExecutor.awaitTermination(60, TimeUnit.SECONDS)) {
queryExecutor.shutdownNow();
if (!queryExecutor.awaitTermination(60, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
queryExecutor.shutdownNow();
Thread.currentThread().interrupt();
}
super.detach();
}
}