I'm using RESTEasy 3.1.0.CR3 with its servlet initializer in Tomcat 8.5, via annotations (no web.xml):
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.1.0.CR3</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.1.0.CR3</version>
</dependency>
I have a JAX-RS application that I need to know before it is destroyed/uninstalled from the container, so that I can release some resources (stop a thread):
@ApplicationPath("/")
public class MyRESTApplication extends Application {
…
@Override
public Set<Class<?>> getClasses() {
…
}
@Override
public Set<Object> getSingletons() {
…
}
@PreDestroy
public void end() {
//TODO release resources
}
But my end() method doesn't seem to be called. Am I doing this wrong? Is there a better way to detect when a JAX-RS application is taken down?
I'm not sure if this is supported. For
@PreDestorythe same rule applies like for@PostConstruct:Is dependency injection in an
Applicationclass supported? A JAX-RS implementation may integrate Managed Beans, EJBs or CDI but this is optional. JAX-RS itself provides dependency injection via@Contextbut explicitly not in anApplicationclass (ch. 9.2.1 of the specification).So
@PreDestorymay work in some environments but there's no guarantee.In your environment I would implement a custom
ServletContextListenerand release resources in#contextDestroyed. YourApplicationclass should be accessible via the ServletContext:You could also handle everything in a custom
ServletContextListenerwithout the Application class and store references as ServletContext Attributes.