1

I have a java class annotated with the jakarta.ws.rs-api annotations, which is a rest resource class. I have created an executor service as part of the resource class (which contains methods with annotations like @GET, @PUT, etc.), and am looking for a way to close this executor service upon the shutdown of the servlet container (jetty using jersey). I came across @PreDestroy annotation, used it in the resource class, but it isn't getting invoked upon graceful shutdown of the container.

Is there a way where the resources like executor-service in a resource class can be closed with a predefined hook or annotation? Thanks!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I remember having to do cleanup like this using a `contextDestroyed` method on a `Servlet`. Although I don't remember if it was because of Jersey not properly calling those `@PreDestroy` methods, or something to do with Tomcat, or with Guice. But at least this may give you another option to look at. – Jorn Jun 19 '23 at 07:25
  • It's indeed overlooked in JAX-RS: https://stackoverflow.com/q/41187018. Theoretically, if CDI is available, you could use an Extension to patch this. – BalusC Jun 19 '23 at 10:57

1 Answers1

0

One way of listening to the container's "shutdown" could be using an implementation of ServletContextListener interface, and writing the logic for closing your executor service under the contextDestroyed(ServletContext) method.