2010-10-09 70 views
2

我在我的web應用程序中有Guice的Quartz調度程序。我跟着代碼找到here。一切正常,但我無法弄清楚如何關閉調度程序。我的上下文偵聽器看起來是這樣的:關機石英調度程序

public class MyAppContextListener extends GuiceServletContextListener{ 

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

和石英模塊如下所示:

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); 
} 

什麼是關機調度的最佳途徑時,應用程序被停止或取消部署?

回答

3

您可以使用​​。

當wep-app停止時,應用程序服務器將調用contextDestroyed()

這會給你時間打電話必需品您QuartzModule(在contextDestroyed()方法中)在web-app停止之前。

只記得在您的網絡應用程序的web.xml中添加<listener>標記。

+0

我想過使用contextDestroyed,但我怎麼才能訪問注入器在contextDestroyed()方法? – jjczopek 2010-10-09 21:33:25

+0

如果你看看QuartzModule類,它被定義爲SINGLETON,所以VM中只有一個Quartz實例。所以無論你從哪裏獲得實例,它都將是相同的Quartz實例。 contextDestroyed()中的以下內容應該可以工作:i.getInstance(Quartz.class).shutdown(); – Koekiebox 2010-10-10 15:09:48

+0

我有同樣的問題,但這個答案不幫助我。由於ServletContexListener不是由guice創建的,我將如何在那裏獲得注入器? – 0lukasz0 2011-12-22 17:09:07