users@jersey.java.net

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

From: Kristof <kr_adr_at_yahoo.co.uk>
Date: Tue, 26 Oct 2010 19:07:17 +0100 (BST)

Hi Paul, I was actually able to get things working with Guice. I indeed use GuiceyFruit and am binding the provided Jsr250Module to enable support for @PostConstruct and @PreDestroy. After doing this the @PostConstruct annotation started to work immediately. I had some problems however to get @PreDestroy to work. To trigger @PreDestroy I'm overriding contextDestroyed(ServletContextEvent) of my GuiceServletContextListener in which I call Injectors.close(myInjector). However at first this did not work. While debugging Injectors.close I found out that I had 2 Guice versions on my class path. The guiceyfruit-core dependency pulls in Guice and the jersey-guice dependency pulls in Guice as well. But they both pull it in as a different artifact id. The problem is that the guiceyfruit Guice version seems to be a modified version. Initially it looked like I was creating my Injector with the Guice version that comes with jersey-guice and with that version the Injectors.close(myInjector) did not trigger the @PreDestroy. It was only after excluding the guice dependency from jersey-guice that @PreDestroy started to work. So at the point I only had 1 guice version in my class path, the one from guiceyfruit. I'm now able to initialize my database connection etc in my Service Layer, get it injected in my Resources and properly shut my Service layer down when the application is stopped/undeployed. I think such an example could help others as well. So I'm willing to make a simple example project if you think if this would be valuable? Thanks for the support, Kristof -------- Original Message -------- Subject: Re: [Jersey] Missing shutdown hook at end of resources lifecycle. Date: Wed, 20 Oct 2010 11:15:32 +0200 From: Paul Sandoz <Paul.Sandoz@oracle.com> To: users@jersey.dev.java.net 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 >