users@jersey.java.net

Re: [Jersey] Missing shutdown hook at end of resources lifecycle.

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Wed, 20 Oct 2010 11:15:32 +0200

Hi Kristof,

You may be able to do what you require by registering your own
implementation of Application that has @PostConstruct and @PreDestroy
methods (if using Jersey injection of a DI that supports those
annotations).

The post construct method will be called at Jersey initialization time
(before the registered provider and resource classes have been
processed). The pre destroy method will be called when the application
is undeployed.

This should work for Spring when the Application is spring managed.

It will not work for Guice, unless you are using GuicyFruit
extensions, because Guice does not support @PostConstruct and
@PreDestroy. For Guice you may need to register a servlet listener
ServletContextListener in the servlet module.

Hth,
Paul.

On Oct 17, 2010, at 11:27 AM, Kristof wrote:

> Hi all,
>
> I want to use Jersey to build a REST service around an api that
> accesses a database. The api has initialize and shutdown methods to
> setup/shutdown connection pool, database connections etc.
>
> The main issue I have is that I don't know of a way to call the
> shutdown method when the jersey resource lifecycle ends. For example
> when a user shuts down/undeploys/reloads the webapp in a Tomcat
> environment. Jersey does not seem to provide any hooks to
> accomplish this? There are pre- and post request hooks but no hooks
> that are called at startup/shutdown of the application/container.
>
> Shutting down properly is required to avoid memory leaks.
>
> To me this looks like something trivial but none of the REST
> frameworks that I looked at (Jersey, Resteasy, Restlet) seem to
> support this? There was a similar post on the mailing list asking
> the same question: https://jersey.dev.java.net/servlets/ReadMsg?list=users&msgNo=4271
> but the questions was not really answered.
>
> Ideally the init/shutdown should also be integrated with Guice/
> Spring support so init/shutdown methods can be invoked on Guice/
> Spring injected objects.
>
> Any suggestions?
>
> Thanks!
> Kristof
>