Quartz之QuartzInitializerListener

12 篇文章 0 订阅
[color=red]问题:我想在WEB容器启动时就执行任务怎么办呢
Quartz:使用QuartzInitializerListener就可办到了[/color] :lol:
[color=red]请注意它的优先级别比QuartzInitializerServlet要高[/color]
在web.xml中可配置的参数如下:
如:
<context-param>
<param-name>quartz:config-file</param-name>
<param-value>/quartz.properties</param-value>
</context-param>

以下二者参数可代表都是同一个意思
[color=red]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[/color]
以上参数都是根据QuartzInitializerListener源码得来的 :lol:
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.");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值