问题:我想在应用程序启动之后去执行任务怎么办呢!
Quartz:使用QuartzInitializerServlet可满足需要
参考资料
1 Quartz调度框架应用总结
http://java.chinaitlab.com/advance/752064_3.html
2 Integrating quartz in a web application
http://www.oreillynet.com/cs/user/view/cs_msg/52725
3 基于Quartz的开源项目
myschedule
4 Adding multiple jobs via the quartz_jobs.xml skips jobs
http://jira.opensymphony.com/browse/QRTZNET-250
5 Quartz with Apache ServiceMix
http://forums.terracotta.org/forums/posts/list/5427.page
6 Web应用中使用Quartz进行任务调度
http://www.hujun.me/post?id=12001
7 Quartz的XML定义说明:New in Quartz.Net 2.0–New Job File Format
http://jvilalta.blogspot.com/2011/03/new-in-quartznet-20new-job-file-format.html
来自于Quartz的文档说明:
A Servlet that can be used to initialize Quartz, if configured as a load-on-startup servlet in a web application.Using this start-up servlet may be preferred to using the QuartzInitializerListener in some situations - namely when you want to initialize more than one scheduler in the same application.You'll want to add something like this to your WEB-INF/web.xml file:
- <servlet>
- <servlet-name>
- QuartzInitializer
- </servlet-name>
- <display-name>
- Quartz Initializer Servlet
- </display-name>
- <servlet-class>
- org.quartz.ee.servlet.QuartzInitializerServlet
- </servlet-class>
- <load-on-startup>
- 1
- </load-on-startup>
- <init-param>
- <param-name>config-file</param-name>
- <param-value>/some/path/my_quartz.properties</param-value>
- </init-param>
- <init-param>
- <param-name>shutdown-on-unload</param-name>
- <param-value>true</param-value>
- </init-param>
- <init-param>
- <param-name>wait-on-shutdown</param-name>
- <param-value>true</param-value>
- </init-param>
- <init-param>
- <param-name>start-scheduler-on-load</param-name>
- <param-value>true</param-value>
- </init-param>
- </servlet>
相应参数说明:
The init parameter 'config-file' can be used to specify the path (and filename) of your Quartz properties file. If you leave out this parameter, the default ("quartz.properties") will be used.
The init parameter 'shutdown-on-unload' can be used to specify whether you want scheduler.shutdown() called when the servlet is unloaded (usually when the application server is being shutdown). Possible values are "true" or "false". The default is "true".
The init parameter 'wait-on-shutdown' has effect when 'shutdown-on-unload' is specified "true", and indicates whether you want scheduler.shutdown(true) called when the listener is unloaded (usually when the application server is being shutdown). Passing "true" to the shutdown() call causes the scheduler to wait for existing jobs to complete. Possible values are "true" or "false". The default is "false".
The init parameter 'start-scheduler-on-load' can be used to specify whether you want the scheduler.start() method called when the servlet is first loaded. If set to false, your application will need to call the start() method before the scheduler begins to run and process jobs. Possible values are "true" or "false". The default is "true", which means the scheduler is started.
A StdSchedulerFactory instance is stored into the ServletContext. You can gain access to the factory from a ServletContext instance like this:
StdSchedulerFactory factory = (StdSchedulerFactory) ctx
.getAttribute(QuartzFactoryServlet.QUARTZ_FACTORY_KEY);
The init parameter 'servlet-context-factory-key' can be used to override the name under which the StdSchedulerFactory is stored into the ServletContext, in which case you will want to use this name rather than QuartzFactoryServlet.QUARTZ_FACTORY_KEY in the above example.
The init parameter 'scheduler-context-servlet-context-key' if set, the ServletContext will be stored in the SchedulerContext under the given key name (and will therefore be available to jobs during execution).
The init parameter 'start-delay-seconds' can be used to specify the amount of time to wait after initializing the scheduler before scheduler.start() is called.
Once you have the factory instance, you can retrieve the Scheduler instance by calling getScheduler() on the factory.
所要的jar文件包含:
quartz-all-2.0.1.jar, jta-1.1.jar,log4j-1.2.14.jar,slf4j-api-1.6.1.jar,slf4j-log4j12-1.6.1.jar
具体代码如下:
web.xml
- <servlet>
- <servlet-name>QuartzInitializer</servlet-name>
- <display-name>Quartz Initializer Servlet</display-name>
- <servlet-class>
- org.quartz.ee.servlet.QuartzInitializerServlet
- </servlet-class>
- <load-on-startup>1</load-on-startup>
- <init-param>
- <param-name>config-file</param-name>
- <param-value>/quartz.properties</param-value>
- </init-param>
- <init-param>
- <param-name>shutdown-on-unload</param-name>
- <param-value>true</param-value>
- </init-param>
- <!--
- <init-param>
- <param-name>start-scheduler-on-load</param-name>
- <param-value>true</param-value>
- </init-param>
- <init-param>
- <param-name>wait-on-shutdown</param-name>
- <param-value>true</param-value>
- </init-param>
- -->
- </servlet>
位于src下:quartz.properties
- org.quartz.scheduler.threadsInheritContextClassLoaderOfInitializer=true
- org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
- # Configure Main Scheduler Properties
- org.quartz.scheduler.instanceName=QuartzScheduler
- org.quartz.scheduler.instanceId=AUTO
- org.quartz.scheduler.skipUpdateCheck=true
- org.quartz.scheduler.threadsInheritContextClassLoaderOfInitializer=true
- # Configure ThreadPool
- org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
- org.quartz.threadPool.threadCount=3
- org.quartz.threadPool.threadPriority=5
- org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
- # Configure JobStore
- org.quartz.jobStore.misfireThreshold=60000
- org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
- #org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
- #org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
- #org.quartz.jobStore.useProperties=false
- #org.quartz.jobStore.dataSource=myDS
- #org.quartz.jobStore.tablePrefix=QRTZ_
- #org.quartz.jobStore.isClustered=false
- # Configure Datasources
- #org.quartz.dataSource.myDS.driver=org.postgresql.Driver
- #org.quartz.dataSource.myDS.URL=jdbc:postgresql://localhost/dev
- #org.quartz.dataSource.myDS.user=jhouse
- #org.quartz.dataSource.myDS.password=
- #org.quartz.dataSource.myDS.maxConnections=5
- # Configure Plugins
- org.quartz.plugin.triggHistory.class=org.quartz.plugins.history.LoggingJobHistoryPlugin org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
- org.quartz.plugin.jobInitializer.fileNames=quartz_job.xml
- org.quartz.plugin.jobInitializer.failOnFileNotFound=true
- org.quartz.plugin.jobInitializer.scanInterval=120
- org.quartz.plugin.jobInitializer.wrapInUserTransaction=false
有些时候其实可按需配置,如下:
- org.quartz.scheduler.instanceName= MyQuartzScheduler
- org.quartz.scheduler.rmi.export= false
- org.quartz.scheduler.rmi.proxy= false
- org.quartz.scheduler.wrapJobExecutionInUserTransaction= false
- org.quartz.threadPool.class= org.quartz.simpl.SimpleThreadPool
- org.quartz.threadPool.threadCount= 10
- org.quartz.threadPool.threadPriority= 5
- org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread= true
- org.quartz.jobStore.misfireThreshold= 60000
- org.quartz.jobStore.class= org.quartz.simpl.RAMJobStore
- org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
- #job and trigger configuration file
- org.quartz.plugin.jobInitializer.fileNames =quartz_job.xml
位于src下的:quartz_job.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <job-scheduling-data
- xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData
- http://www.quartz-scheduler.org/xml/job_scheduling_data_2_0.xsd"
- version="2.0">
- <pre-processing-commands>
- <delete-jobs-in-group>*</delete-jobs-in-group> <!-- clear all jobs in scheduler -->
- <delete-triggers-in-group>*</delete-triggers-in-group> <!-- clear all triggers in scheduler -->
- </pre-processing-commands>
- <processing-directives>
- <!-- if there are any jobs/trigger in scheduler of same name (as in this file), overwrite them -->
- <overwrite-existing-data>true</overwrite-existing-data>
- <!-- if there are any jobs/trigger in scheduler of same name (as in this file), and over-write is false, ignore them rather then generating an error -->
- <ignore-duplicates>false</ignore-duplicates>
- </processing-directives>
- <schedule>
- <job>
- <name>job1</name>
- <job-class>net.liuzd.tools.quartz.servlet.Job1</job-class>
- </job>
- <trigger>
- <cron>
- <name>t1</name>
- <job-name>job1</job-name>
- <cron-expression>0/10 * * * * ?</cron-expression>
- </cron>
- </trigger>
- <job>
- <name>job2</name>
- <job-class>net.liuzd.tools.quartz.servlet.Job2</job-class>
- </job>
- <trigger>
- <cron>
- <name>t2</name>
- <job-name>job2</job-name>
- <cron-expression>0/20 * * * * ?</cron-expression>
- </cron>
- </trigger>
- </schedule>
- </job-scheduling-data>
src下的log4j.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
- <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
- <appender name="default" class="org.apache.log4j.ConsoleAppender">
- <param name="target" value="System.out"/>
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="[%p] %d{yyyy-MM-dd hh:mm:ss.SSS aa} %t [%c]%n%m%n%n"/>
- </layout>
- </appender>
- <logger name="org.quartz">
- <level value="info" />
- </logger>
- <root>
- <level value="info" />
- <appender-ref ref="default" />
- </root>
- </log4j:configuration>
二个任务类:
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.quartz.Job;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class Job1 implements Job {
- private static Logger _log = LoggerFactory.getLogger(Job1.class);
- public Job1() {
- }
- public void execute(JobExecutionContext context)
- throws JobExecutionException {
- _log.info("执行天涯的第一个任务: " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
- }
- }
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.quartz.Job;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class Job2 implements Job {
- private static Logger _log = LoggerFactory.getLogger(Job2.class);
- public Job2() {
- }
- public void execute(JobExecutionContext context)
- throws JobExecutionException {
- _log.info("执行天涯的第二个任务: " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
- }
- }
在WEB应用启动时控制台输出结果如下:
- [INFO] 2011-08-15 02:18:50.015 下午 QuartzScheduler_Worker-2 [org.quartz.plugins.history.LoggingJobHistoryPlugin]
- Job DEFAULT.job1 fired (by trigger DEFAULT.t1) at: 14:18:50 08/15/2011
- [INFO] 2011-08-15 02:18:50.015 下午 QuartzScheduler_Worker-2 [net.liuzd.tools.quartz.servlet.Job1]
- 执行天涯的第一个任务: 2011-08-15 02:18:50
- [INFO] 2011-08-15 02:18:50.015 下午 QuartzScheduler_Worker-2 [org.quartz.plugins.history.LoggingJobHistoryPlugin]
- Job DEFAULT.job1 execution complete at 14:18:50 08/15/2011 and reports: null
- [INFO] 2011-08-15 02:19:00.000 下午 QuartzScheduler_Worker-3 [org.quartz.plugins.history.LoggingJobHistoryPlugin]
- Job DEFAULT.job1 fired (by trigger DEFAULT.t1) at: 14:19:00 08/15/2011
- [INFO] 2011-08-15 02:19:00.000 下午 QuartzScheduler_Worker-3 [net.liuzd.tools.quartz.servlet.Job1]
- 执行天涯的第一个任务: 2011-08-15 02:19:00
- [INFO] 2011-08-15 02:19:00.000 下午 QuartzScheduler_Worker-3 [org.quartz.plugins.history.LoggingJobHistoryPlugin]
- Job DEFAULT.job1 execution complete at 14:19:00 08/15/2011 and reports: null
- [INFO] 2011-08-15 02:19:00.000 下午 QuartzScheduler_Worker-1 [org.quartz.plugins.history.LoggingJobHistoryPlugin]
- Job DEFAULT.job2 fired (by trigger DEFAULT.t2) at: 14:19:00 08/15/2011
- [INFO] 2011-08-15 02:19:00.000 下午 QuartzScheduler_Worker-1 [net.liuzd.tools.quartz.servlet.Job2]
- 执行天涯的第二个任务: 2011-08-15 02:19:00
相关推荐
1. **Job(作业)**:这是Quartz中的核心概念之一,它代表了要执行的任务。一个`Job`实例必须实现`org.quartz.Job`接口,并且重写`execute`方法来定义具体的任务逻辑。如果任务需要保存状态,则需要实现`StatefulJob...
1. **添加QuartzInitializerServlet**:在`web.xml`中配置`QuartzInitializerServlet`。 2. **配置文件**:可以通过配置文件`quartz.properties`或`quartz-job.xml`来定义触发器和定时描述等。 #### 五、Quartz高级...
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet <param-name>config-file <param-value>/schedule/quartz.properties <param-name>shutdown-on-unload <param-value>true ...
在 Web 应用中使用 Quartz,通常需要在 `web.xml` 文件中配置 `QuartzInitializerServlet`,并提供配置文件(如 `quartz.properties` 或 `quartz-job.xml`)来定义 Triggers 和 JobDetails。 通过以上描述,我们...
例如,你可以配置 `QuartzInitializerServlet` 类作为 Servlet,并设置初始化参数 `shutdown-on-unload` 为 `true`,这意味着当应用服务器卸载时,Quartz 会自动关闭。同时,你需要指定 `config-file` 参数为 `...
2. **配置Web应用启动器**:在`web.xml`中配置Quartz初始化Servlet,这样当Web应用启动时,Quartz也能随之初始化。具体配置如下: ```xml <servlet-name>QuartzInitializer <display-name>...
- 在Java Web应用中,需要在web.xml中配置Quartz的监听器(org.quartz.ee.servlet.QuartzInitializerServlet)以启动调度器。 - 配置quartz.properties文件,设定数据库存储Job和Trigger(如果选择使用数据库存储...
在Web应用中,我们可以通过`web.xml`配置`org.quartz.servlet.QuartzInitializerServlet`来启动Quartz。例如,我们可以设置初始化参数`org.quartz.scheduler.instanceName`为应用的实例名,`org.quartz.jobStore....
内容提要:Quartz 从属性文件中加载多个插件类时不能保证加载的顺序,所以本节引入一个自定义的统一按顺序加载其他插件的,名之为插件加载器的东西,其实也就是其他插件类的父亲。 第八章. 使用 Quartz 插件 (第五...
QuartzInitializerServlet.startScheduler(app); // 启动任务 } catch (Exception e) {} } } ``` #### 四、总结 本文详细介绍了如何配置和部署 Quartz 定时任务框架,主要包括了 `quartz.properties` 配置文件的...
- 在Web应用中使用Quartz,通常需要在web.xml中配置ContextLoaderListener和QuartzInitializerServlet。Quartz的配置文件(如quartz.properties)用于设置调度器的属性,如job store类型、线程池大小等。 6. **API...
- **Servlet Class**: org.quartz.ee.servlet.QuartzInitializerServlet,指定了初始化Servlet的具体类。 - **Init-Param**: - **config-file**: /quartz.properties,指定Quartz配置文件的位置。 - **shutdown-...
要在Servlet环境中使用Quartz,需要在`web.xml`中配置QuartzInitializerServlet,这将初始化Quartz并将其与Web应用程序集成。此外,可以使用`quartz.properties`或`quartz-job.xml`文件来定义Trigger和Job的详细...
这里可能包含了启动Quartz Scheduler的监听器配置,例如`org.quartz.ee.servlet.QuartzInitializerServlet`,以及相关的初始化参数。 3. **quartzDem**:这是一个包含具体业务逻辑的Java类或包。它应该包含了自定义...