views:

31

answers:

1

I have Quartz scheduler in my web application with Guice. I followed code found here. Everything works fine, but I can't figure out how to shutdown scheduler. My context listener looks like this:

public class MyAppContextListener extends GuiceServletContextListener{

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new QuartzModule(), new MyAppServletModule());
    }
}

And Quartz module looks like this:

public class QuartzModule extends AbstractModule {

@Override
protected void configure() {
    bind(SchedulerFactory.class).to(StdSchedulerFactory.class).in(Scopes.SINGLETON);
    bind(GuiceJobFactory.class).in(Scopes.SINGLETON);
    bind(Quartz.class).in(Scopes.SINGLETON);
}

What is the best way to shutdown scheduler when application is being stopped or undeployed?

+1  A: 

You can make use of the ServletContextListener.

The app server will call the contextDestroyed() when your wep-app is stopped.

This will give you time to call the necessaries on your QuartzModule (inside the contextDestroyed() method) just before the web-app stops.

Just remember to add the <listener> tags in the web.xml of your web-app.

Koekiebox
I thought about using contextDestroyed, but how can I get access to injector in contextDestroyed() method?
jjczopek
If you look at the QuartzModule class, it is defined as a SINGLETON, so there will only be one Quartz instance in the VM. So no matter where you get the instance from, it will be the same Quartz instance. The following in the contextDestroyed() should work: i.getInstance(Quartz.class).shutdown();
Koekiebox