定时任务 Quartz调度(采用配置文件)

来源:互联网 发布:mac地址 制造商 编辑:程序博客网 时间:2024/06/06 01:42

采用配置文件方式的Quartz调度:

1、所需的第三方包

commons-beanutils.jar

commons-collections-3.2.jar

commons-digester.jar

commons-logging.jar

jta.jar

quartz.jar

 

2、自定被调度的Job

package com.config;import java.text.SimpleDateFormat;import java.util.Date;import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;public class MyJob implements Job {/** * 需要定时调度的方法 */public void execute(JobExecutionContext arg0) throws JobExecutionException {System.out.println("我是被定时调度的方法啊,当前时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SS").format(new Date()));}}

3.在 web.xml加入:

<servlet> <servlet-name>QuartzInitializer</servlet-name><servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class> <init-param><param-name>shutdown-on-unload</param-name><param-value>true</param-value></init-param><init-param><param-name>config-file</param-name><param-value>quartz.properties</param-value> </init-param><load-on-startup>1</load-on-startup></servlet>

4.在classes的目录下建立quartz.properties
#============================================================================
# Configure Main Scheduler Properties  
#============================================================================
org.quartz.scheduler.instanceName = QuartzScheduler
#============================================================================
# Configure ThreadPool  
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 1000
#org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure Plugins 
#============================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileName = quartz_jobs.xml
#org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
#org.quartz.plugin.jobInitializer.failOnFileNotFound = true
5.在classes的目录下建立quartz_jobs.xml
<?xml version='1.0' encoding='utf-8'?><quartz><job><job-detail><name>test</name><group>DEFAULT</group><description>testJobhere</description><job-class>com.allen.MyJob</job-class></job-detail><trigger><cron><name>testTrigger</name><group>DEFAULT</group><job-name>test</job-name><job-group>DEFALUT</job-group><cron-expression>0/5 * * * * ?</cron-expression></cron></trigger></job></quartz>


6.用main方法测试:
package com.config;import java.util.Date;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.SimpleTrigger;import org.quartz.impl.StdSchedulerFactory;public class QuartzTest {public static void main(String[] args) {QuartzTest q = new QuartzTest();try {q.startScheduler();} catch (SchedulerException e) {e.printStackTrace();}}protected void startScheduler() throws SchedulerException {Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();JobDetail jobDetail = new JobDetail("testJob", Scheduler.DEFAULT_GROUP, MyJob.class);// 结束时间long end = System.currentTimeMillis() + 9000L;// 执行10次,每3秒执行一次,到9秒后结束SimpleTrigger trigger = new SimpleTrigger("test", null, new Date(), new Date(end), 10, 3000L);scheduler.scheduleJob(jobDetail, trigger);scheduler.start();}}

7.在应用服务器中发布,就可以在console看到调度
 
常用示例:
 
0 0 12 * * ?每天12点触发0 15 10 ? * *每天10点15分触发0 15 10 * * ?每天10点15分触发0 15 10 * * ? *每天10点15分触发0 15 10 * * ? 20052005年每天10点15分触发0 * 14 * * ?每天下午的 2点到2点59分每分触发0 0/5 14 * * ?每天下午的 2点到2点59分(整点开始,每隔5分触发)0 0/5 14,18 * * ?每天下午的 2点到2点59分(整点开始,每隔5分触发)
每天下午的 18点到18点59分(整点开始,每隔5分触发)0 0-5 14 * * ?每天下午的 2点到2点05分每分触发0 10,44 14 ? 3 WED3月分每周三下午的 2点10分和2点44分触发0 15 10 ? * MON-FRI从周一到周五每天上午的10点15分触发0 15 10 15 * ?每月15号上午10点15分触发0 15 10 L * ?每月最后一天的10点15分触发0 15 10 ? * 6L每月最后一周的星期五的10点15分触发0 15 10 ? * 6L 2002-2005从2002年到2005年每月最后一周的星期五的10点15分触发0 15 10 ? * 6#3每月的第三周的星期五开始触发0 0 12 1/5 * ?每月的第一个中午开始每隔5天触发一次0 11 11 11 11 ?每年的11月11号 11点11分触发(光棍节)

0 0
原创粉丝点击