@PreDestroy not invoked in JAX-RS resource, how do I close/release resources?

104 Views Asked by At

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!

1

There are 1 best solutions below

0
On

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.