[jax-rs-spec users] Re: Lifecycle management in Application subclasses

From: Marek Potociar <>
Date: Fri, 14 Dec 2012 01:44:25 +0100

On Dec 13, 2012, at 6:40 PM, James Shaw <> wrote:

> On 13 December 2012 15:52, Marek Potociar <> wrote:
>> On Dec 6, 2012, at 12:29 PM, James Shaw <> wrote:
>>> I hope this is the correct place to raise this.
>>> I was looking for an Application method I could override that would be
>>> executed when the servlet container shuts down (I have some
>>> ExecutorServices that need to be stopped)
>>> I couldn't find such a method, so I've worked around the problem by
>>> implementing my own ServletContextListener and exposing the
>>> ExecutorServices through a static accessor. Something like this:
>>> @ApplicationPath("/") public class MyApplication extends Application {
>>> public MyApplication() {
>>> LifecycleListener.getExecutorService();
>>> ...
>>> }
>>> }
>>> @WebListener public class LifecycleListener implements
>>> ServletContextListener { ... }
>>> Has adding an Application.shutdown() hook to JAX-RS been discussed
>>> before? Is there another way I ought to be doing this?
>> It hasn't been discussed IIRC. I wonder why you consider your solution a workaround? Why should JAX-RS specification define another callback for Servlet container, if there is one already defined in the Servlet API?
> I didn't like the fact that I had to share state between the servlet
> context listener and the JAX-RS application using a static variable.

Well, in general I do not see any problem with static variables.
> Additionally, If Application.shutdown() existed, this would make it
> easier to write integration/acceptance tests using Jersey test
> framework (since I can pass an Application instance straight to the
> test framework without having to worry about the Servlet API). But
> perhaps I'm wandering out of JAX-RS into a Jersey discussion now?

Yes. That's something beyond JAX-RS scope.

Anyway, I can bring up your init()/shutdown() hooks question to JAX-RS EG.