Java 关闭石英调度器
我在使用Guice的web应用程序中有Quartz调度程序。我遵循找到的代码。一切正常,但我不知道如何关闭调度程序。我的上下文侦听器如下所示:Java 关闭石英调度器,java,quartz-scheduler,guice,servlets,Java,Quartz Scheduler,Guice,Servlets,我在使用Guice的web应用程序中有Quartz调度程序。我遵循找到的代码。一切正常,但我不知道如何关闭调度程序。我的上下文侦听器如下所示: public class MyAppContextListener extends GuiceServletContextListener{ @Override protected Injector getInjector() { return Guice.createInjector(new QuartzModule(
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);
}
石英模块如下所示:
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);
}
当应用程序停止或取消部署时,关闭计划程序的最佳方法是什么?您可以使用
当wep应用程序停止时,应用程序服务器将调用contextdestromed()
这将使您有时间在web应用程序停止之前调用QuartzModule
(在contextDestroyed()方法中)上的必需项
请记住在web应用程序的web.xml中添加
标记。我考虑过使用ContextDestructed,但是如何在ContextDestructed()方法中访问injector?如果查看QuartzModule类,它被定义为单例,因此VM中只有一个Quartz实例。因此,无论从何处获得实例,它都将是相同的Quartz实例。contextDestroyed()中的以下内容应该可以工作:i.getInstance(Quartz.class).shutdown();我也有同样的问题,但这个答案对我没有帮助。由于ServletContexListener不是由guice创建的,我如何才能将injector放到那里?您可以尝试在启动调度程序的线程上添加一个关闭钩子。请参阅:仅指出对我有帮助的内容:请参阅如何在中访问喷油器