Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的日程序表。Jobs可以做成标准的Java组件或 EJBs。
实例:
package
example.quartz;
import org.quartz.CronTrigger; import org.quartz.Job; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import java.util.Date; /** */ /** * @author starsiday */ /**/ /* 实现Job */ public class QuartzReport implements Job { /**/ /* 重载该函数 */ public void execute(JobExecutionContext cntxt) throws JobExecutionException { System.out.println( " Generating report - " + cntxt.getJobDetail().getJobDataMap().get( " type " ) + new Date()); } public static void main(String[] args) { try { SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory(); Scheduler sched = schedFact.getScheduler(); sched.start(); JobDetail jobDetail = new JobDetail( " Income Report " , " Report Generation " , QuartzReport. class ); jobDetail.getJobDataMap().put( " type " , " FULL " ); CronTrigger trigger = new CronTrigger( " Income Report " , " Report Generation " ); /**/ /* 每1分钟执行一次 */ trigger.setCronExpression( " 0 0/1 * * * ? " ); sched.scheduleJob(jobDetail, trigger); } catch (Exception e) { e.printStackTrace(); } } } Quartz的配置文件:quartz.properties # Configure Main Scheduler Properties org.quartz.scheduler.instanceName = TestScheduler org.quartz.scheduler.instanceId = one
# Configure ThreadPool
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 5 org.quartz.threadPool.threadPriority = 4
# Configure JobStore
org.quartz.jobStore.misfireThreshold = 5000 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore 附:cronExpression配置说明
|