定时任务调度器Quartz的使用

一、需要包

quartz-all-1.6.0.jar

commons-collections-3.2.jar

二、简单实例

1、任务类

package com.test; import java.util.Date; import org.quartz.Job; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class MyJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { // 在控制台输出如下信息 System.out.println("Quartz Test at " + new Date() + "*****************"); //取工作项信息 JobDetail jobDetail = context.getJobDetail(); String jobName = jobDetail.getName(); System.out.println("Name: " + jobDetail.getFullName()); System.out.println("Job Class: " + jobDetail.getJobClass()); System.out.println(jobName + " fired at " + context.getFireTime()); System.out.println("Next fire time " + context.getNextFireTime()); System.out.println("************************************************************"); } }

2、测试实例

package com.test; import java.util.Date; import org.quartz.CronExpression; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.StdSchedulerFactory; public class MyTest { public static void main(String args[]) { try { //创建一个JobDetail实例,指定MyJob JobDetail jobDetail = new JobDetail("Job Test", "Job Group", com.test.MyJob.class); //通过CronTrigger定义调度规则:每分钟执行一次 CronTrigger trigger = new CronTrigger("Job Trigger", "Trigger Group"); CronExpression cexp = new CronExpression("0/5 * * * * ?"); trigger.setCronExpression(cexp); //通过SimpleTrigger定义调度规则:马上启动,每2秒运行一次,共运行100次 // SimpleTrigger trigger = new SimpleTrigger("Job Trigger", "Trigger Group"); // trigger.setStartTime(new Date()); // trigger.setRepeatInterval(2000); // trigger.setRepeatCount(100); SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); sched.scheduleJob(jobDetail, trigger); sched.start(); } catch (Exception e) { e.printStackTrace(); } } }

执行测试类,会在控制台每5秒输出一条信息。

三、配置实例

1、在项目src根目录下或者classpath根目录下放置配置文件

默认名称quartz.propertiesquartz_jobs.xml

quartz.properties清单如下:

#=============================================================== #Configure Main Scheduler Properties #=============================================================== org.quartz.scheduler.instanceName = QuartzScheduler org.quartz.scheduler.instanceId = AUTO #=============================================================== #Configure ThreadPool #=============================================================== org.quartz.threadPool.threadCount = 5 org.quartz.threadPool.threadPriority = 5 org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool #=============================================================== #Configure JobStore #=============================================================== org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore #=============================================================== #Configure Plugins #=============================================================== org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin org.quartz.plugin.jobInitializer.overWriteExistingJobs = true org.quartz.plugin.jobInitializer.failOnFileNotFound = true org.quartz.plugin.jobInitializer.validating=false

quartz_jobs.xml清单如下:

<?xml version="1.0" encoding="UTF-8"?> <quartz> <job> <job-detail> <name>helloworld</name> <group>DEFAULT</group> <description>The job description</description> <job-class>com.test.MyJob</job-class> </job-detail> <trigger> <cron> <name>helloworld</name> <group>DEFAULT</group> <job-name>helloworld</job-name> <job-group>DEFAULT</job-group> <cron-expression>0/10 * * * * ?</cron-expression> </cron> </trigger> </job> </quartz>

2、测试类

package com.test; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.impl.StdSchedulerFactory; public class Test { public static void main(String args[]) { try { SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); sched.start(); } catch (SchedulerException e) { e.printStackTrace(); } } }

执行测试类,效果同上,每10秒输出信息。

 

参考文章:http://jeasony.javaeye.com/category/48401?show_full=true

你可能感兴趣的:(定时任务调度器Quartz的使用)