Quartz Job Scheduling Framework[翻译]第十三章. Quartz 和 Web 应用 (第四部分)
五. 使用 ServletContextListener
很值得一提的是你可以配置和集成 Quartz 到 Web 应用的另一种方式。从 2.3 版本的 Servlet API 开始,你能创建监听器,由容器在其生命周期中的某个特定时间回调。其中的一个监听器接口叫做 java.servlet.ServletContextListener,它包括有两个方法:
public void contextInitialized(ServletContextEvent sce);
public void contextDestroyed(ServeltContextEvent sce);
容器会在启动和关闭的时候相应的调用这两个方法。这就可以在 contextInitialized() 方法中初始化 Quartz Scheduler,并通过 contextDestroyed() 方法关闭它。代码 13.5 描述了这种用法:
代码 13.5. ServletContextListener 也能被用于初始化 Quartz
正如我们在 QuartzInitializerServlet 中所做的,我们需要为这个监听器加入一些配置信息到 Web 部署描术文件(web.xml) 中。针对我们的监听器,我们需要加一个 <listener> 元素到部署描述中。如下片断中显示:
·新的 QuartzInitializerListener 已加入到 Quartz 中
ServletContextListener 为顺应用户社区而来的呼声被加入到 Quartz 框架中。13.5 中的代码应当认为是一个例子,实际应用时你需要开发你自己的监听器。 永久链接 https://yanbin.blog/quartz-job-scheduling-framework-13-4/, 来自 隔叶黄莺 Yanbin's Blog
[版权声明]
本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。
很值得一提的是你可以配置和集成 Quartz 到 Web 应用的另一种方式。从 2.3 版本的 Servlet API 开始,你能创建监听器,由容器在其生命周期中的某个特定时间回调。其中的一个监听器接口叫做 java.servlet.ServletContextListener,它包括有两个方法:
public void contextInitialized(ServletContextEvent sce);
public void contextDestroyed(ServeltContextEvent sce);
容器会在启动和关闭的时候相应的调用这两个方法。这就可以在 contextInitialized() 方法中初始化 Quartz Scheduler,并通过 contextDestroyed() 方法关闭它。代码 13.5 描述了这种用法:
代码 13.5. ServletContextListener 也能被用于初始化 Quartz
1import javax.servlet.ServletContext;
2import javax.servlet.ServletContextEvent;
3import javax.servlet.ServletContextListener;
4
5import org.apache.commons.logging.Log;
6import org.apache.commons.logging.LogFactory;
7import org.quartz.SchedulerException;
8import org.quartz.impl.StdSchedulerFactory;
9
10public class QuartzServletContextListener implements ServletContextListener {
11 private static Log logger = LogFactory
12 .getLog(QuartzServletContextListener.class);
13
14 public static final String QUARTZ_FACTORY_KEY =
15 "org.quartz.impl.StdSchedulerFactory.KEY";
16
17 private ServletContext ctx = null;
18
19 private StdSchedulerFactory factory = null;
20
21 /**
22 * Called when the container is shutting down.
23 */
24 public void contextDestroyed(ServletContextEvent sce) {
25 try {
26 factory.getDefaultScheduler().shutdown();
27 } catch (SchedulerException ex) {
28 logger.error("Error stopping Quartz", ex);
29 }
30
31 }
32
33 /**
34 * Called when the container is first started.
35 */
36 public void contextInitialized(ServletContextEvent sce) {
37
38 ctx = sce.getServletContext();
39
40 try {
41
42 factory = new StdSchedulerFactory();
43
44 // Start the scheduler now
45
46 factory.getScheduler().start();
47
48 logger.info("Storing QuartzScheduler Factory at"
49 + QUARTZ_FACTORY_KEY);
50
51 ctx.setAttribute(QUARTZ_FACTORY_KEY, factory);
52
53 } catch (Exception ex) {
54 logger.error("Quartz failed to initialize", ex);
55 }
56 }
57}正如我们在 QuartzInitializerServlet 中所做的,我们需要为这个监听器加入一些配置信息到 Web 部署描术文件(web.xml) 中。针对我们的监听器,我们需要加一个 <listener> 元素到部署描述中。如下片断中显示:
1<web-app>
2 <listener>
3 <listener-class>
4 org.cavaness.jobconsole.web.QuartzServletContextListener
5 </listener-class>
6 </listener>
7 <!--Other deployment descriptor info not shown -->
8</web-app>·新的 QuartzInitializerListener 已加入到 Quartz 中
ServletContextListener 为顺应用户社区而来的呼声被加入到 Quartz 框架中。13.5 中的代码应当认为是一个例子,实际应用时你需要开发你自己的监听器。 永久链接 https://yanbin.blog/quartz-job-scheduling-framework-13-4/, 来自 隔叶黄莺 Yanbin's Blog
[版权声明]
本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。