`

Quartz之QuartzInitializerListener

阅读更多
问题:我想在WEB容器启动时就执行任务怎么办呢
Quartz:使用QuartzInitializerListener就可办到了

请注意它的优先级别比QuartzInitializerServlet要高
在web.xml中可配置的参数如下:
如:
<context-param>
         <param-name>quartz:config-file</param-name>
         <param-value>/quartz.properties</param-value>
</context-param>

以下二者参数可代表都是同一个意思
quartz:config-file                                                       或者 config-file
quartz:shutdown-on-unload                                       或者  shutdown-on-unload
quartz:wait-on-shutdown
quartz:start-on-load                                                  或者   start-scheduler-on-load
quartz:start-delay-seconds                                        或者   start-delay-seconds
quartz:servlet-context-factory-key                              或者   servlet-context-factory-key
默认值为:org.quartz.impl.StdSchedulerFactory.KEY
quartz:scheduler-context-servlet-context-key              或者 scheduler-context-servlet-context-key

以上参数都是根据QuartzInitializerListener源码得来的
QuartzInitializerListener源码如下:
package org.quartz.ee.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QuartzInitializerListener implements ServletContextListener {
	
	public static final String QUARTZ_FACTORY_KEY = "org.quartz.impl.StdSchedulerFactory.KEY";
	private boolean performShutdown;
	private boolean waitOnShutdown;
	private Scheduler scheduler;
	private final Logger log;

	public QuartzInitializerListener() {
		this.performShutdown = true;
		this.waitOnShutdown = false;

		this.scheduler = null;

		this.log = LoggerFactory.getLogger(super.getClass());
	}

	public void contextInitialized(ServletContextEvent sce) {
		this.log
				.info("Quartz Initializer Servlet loaded, initializing Scheduler...");

		ServletContext servletContext = sce.getServletContext();
		try {
			String configFile = servletContext
					.getInitParameter("quartz:config-file");
			if (configFile == null)
				configFile = servletContext.getInitParameter("config-file");
			String shutdownPref = servletContext
					.getInitParameter("quartz:shutdown-on-unload");
			if (shutdownPref == null)
				shutdownPref = servletContext
						.getInitParameter("shutdown-on-unload");
			if (shutdownPref != null) {
				this.performShutdown = Boolean.valueOf(shutdownPref)
						.booleanValue();
			}
			String shutdownWaitPref = servletContext
					.getInitParameter("quartz:wait-on-shutdown");
			if (shutdownPref != null)
				this.waitOnShutdown = Boolean.valueOf(shutdownWaitPref)
						.booleanValue();

			StdSchedulerFactory factory;

			if (configFile != null)
				factory = new StdSchedulerFactory(configFile);
			else {
				factory = new StdSchedulerFactory();
			}

			this.scheduler = factory.getScheduler();

			String startOnLoad = servletContext
					.getInitParameter("quartz:start-on-load");
			if (startOnLoad == null) {
				startOnLoad = servletContext
						.getInitParameter("start-scheduler-on-load");
			}
			int startDelay = 0;
			String startDelayS = servletContext
					.getInitParameter("quartz:start-delay-seconds");
			if (startDelayS == null)
				startDelayS = servletContext
						.getInitParameter("start-delay-seconds");
			try {
				if ((startDelayS != null) && (startDelayS.trim().length() > 0))
					startDelay = Integer.parseInt(startDelayS);
			} catch (Exception e) {
				this.log
						.error("Cannot parse value of 'start-delay-seconds' to an integer: "
								+ startDelayS + ", defaulting to 5 seconds.");
				startDelay = 5;
			}

			if ((startOnLoad == null)
					|| (Boolean.valueOf(startOnLoad).booleanValue()))
				if (startDelay <= 0) {
					this.scheduler.start();
					this.log.info("Scheduler has been started...");
				} else {
					this.scheduler.startDelayed(startDelay);
					this.log.info("Scheduler will start in " + startDelay
							+ " seconds.");
				}
			else {
				this.log
						.info("Scheduler has not been started. Use scheduler.start()");
			}

			String factoryKey = servletContext
					.getInitParameter("quartz:servlet-context-factory-key");
			if (factoryKey == null)
				factoryKey = servletContext
						.getInitParameter("servlet-context-factory-key");
			if (factoryKey == null) {
				factoryKey = "org.quartz.impl.StdSchedulerFactory.KEY";
			}

			this.log
					.info("Storing the Quartz Scheduler Factory in the servlet context at key: "
							+ factoryKey);

			servletContext.setAttribute(factoryKey, factory);

			String servletCtxtKey = servletContext
					.getInitParameter("quartz:scheduler-context-servlet-context-key");
			if (servletCtxtKey == null)
				servletCtxtKey = servletContext
						.getInitParameter("scheduler-context-servlet-context-key");
			if (servletCtxtKey != null) {
				this.log
						.info("Storing the ServletContext in the scheduler context at key: "
								+ servletCtxtKey);

				this.scheduler.getContext().put(servletCtxtKey, servletContext);
			}
		} catch (Exception e) {
			this.log.error("Quartz Scheduler failed to initialize: "
					+ e.toString());
			e.printStackTrace();
		}
	}

	public void contextDestroyed(ServletContextEvent sce) {
		if (!this.performShutdown) {
			return;
		}
		try {
			if (this.scheduler != null)
				this.scheduler.shutdown(this.waitOnShutdown);
		} catch (Exception e) {
			this.log.error("Quartz Scheduler failed to shutdown cleanly: "
					+ e.toString());
			e.printStackTrace();
		}

		this.log.info("Quartz Scheduler successful shutdown.");
	}
}






分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics