0

I'm using a ScheduledExecutorService inside a Spring Singleton. It executes a periodical check that's scheduled to run every x minutes. I'm lazily (or half-lazily) initializing the Singleton member variables (the Executor) by implementing a ApplicationListener<ContextStartedEvent>#onApplicationEventmethod - that created the ScheduledExecutorService and assigns a task.

I need to deal with the shutdown of the ExecutorService, and wonder how to do that properly. I do know that Spring has it's context shutdown process, and wonder whether it's reasonable to call the shutdown somewhere from there.

What exactly happens if the ExecutorService doesn't get shutdown, and the Server is being closed or terminated? Doesn't the JVM just clears all memory related to this program?

Thanks

Ido Gal
  • 528
  • 10
  • 26
  • 1
    https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-lifecycle – JB Nizet Nov 11 '18 at 16:59
  • Thanks. I've implemented a "shutdown"/"destroy" method according to the documentation. I still can't figure out on which scenario Spring performs such a shutdown process. If I'm just stopping the web server, the method doesn't get called. – Ido Gal Nov 12 '18 at 06:40
  • Why are you doing the scheduling yourself instead of using `@EnableScheduling` with an `@Scheduled` annotation. This way Spring will manage the executor for you. Also how are you shutting down the server, nicely or by doing a `kill`? – M. Deinum Nov 12 '18 at 07:26

0 Answers0