Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中。它提供了巨大的灵 活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。它有很多特征,如:数据库支持,集群,插件,EJB作业预构 建,JavaMail及其它,支持cron-like表达式等等。
该项目已经被 Terracotta收购。
例子:下面我们就来实现一个例子,每隔六分钟就在指定的目录统计xml文件和各个xml文件的大小.
上代码:
import java.io.File; import java.io.FileFilter; import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class ScanDirectoryJob implements Job { static Log logger = LogFactory.getLog(ScanDirectoryJob.class); @Override public void execute(JobExecutionContext context) throws JobExecutionException { JobDetail jobDetail = context.getJobDetail(); String jobName = jobDetail.getKey().getName(); logger.info(jobName + " fire at " + new Date()); //System.out.println(jobName + " fire at " + new Date()); JobDataMap dataMap = jobDetail.getJobDataMap(); String dirName = dataMap.getString("scan"); if(null == dirName){ throw new JobExecutionException("Directory not fount"); } File dir = new File(dirName); if(!dir.exists()){ throw new JobExecutionException("Invalid dir" + dirName); } File[] files = dir.listFiles(new FileFilter(){ @Override public boolean accept(File f) { if(f.getName().endsWith(".xml") && f.isFile()) return true; return false; } }); if(files == null || files.length <= 0){ logger.info("no xml file exists in " + dir); return; } int size = files.length; for(int i = 0; i < size; i++){ File file = files[i]; File aFile = file.getAbsoluteFile(); long fileSize = file.length(); String msg = aFile + ", Size: " + fileSize; logger.info(msg); } } }
import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.quartz.DailyTimeIntervalScheduleBuilder; import org.quartz.DailyTimeIntervalTrigger; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.core.SchedulerSignalerImpl; import org.quartz.impl.StdSchedulerFactory; public class SimpleScheduler { static Log logger = LogFactory.getLog(SimpleScheduler.class); public static void main(String[] args) { SimpleScheduler simple = new SimpleScheduler(); try{ Scheduler scheduler = simple.createScheduler(); simple.scheduleJob(scheduler); scheduler.start(); logger.info("Schedule stated at " + new Date()); } catch (SchedulerException e) { logger.info(e); } } public Scheduler createScheduler() throws SchedulerException{ return StdSchedulerFactory.getDefaultScheduler(); } private void scheduleJob(Scheduler scheduler) throws SchedulerException{ JobDetail jobDetail = JobBuilder.newJob(ScanDirectoryJob.class).withIdentity("myJob").build(); jobDetail.getJobDataMap().put("scan", "D:\\360Downloads\\quartz-2.1.5\\quartz-2.1.5\\examples"); SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("class") .withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(2) .withIntervalInMilliseconds(6000)).build(); /* DailyTimeIntervalTrigger dailyTrigger = TriggerBuilder.newTrigger().withIdentity("Daily trigger") .withSchedule(DailyTimeIntervalScheduleBuilder.dailyTimeIntervalSchedule().withIntervalInHours(1) .withRepeatCount(0)).build();*/ scheduler.scheduleJob(jobDetail, trigger); } }
因为里面用到了log4j,所以你可以添加一个资源文件:
# Configure logging for testing
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
结果:
2012-08-06 10:11:01,068 INFO [SimpleScheduler] - Schedule stated at Mon Aug 06 10:11:01 CST 2012
2012-08-06 10:11:01,076 INFO [ScanDirectoryJob] - myJob fire at Mon Aug 06 10:11:01 CST 2012
2012-08-06 10:11:01,077 INFO [ScanDirectoryJob] - D:\360Downloads\quartz-2.1.5\quartz-2.1.5\examples\build.xml, Size: 1613
2012-08-06 10:11:01,077 INFO [ScanDirectoryJob] - D:\360Downloads\quartz-2.1.5\quartz-2.1.5\examples\pom.xml, Size: 6085
2012-08-06 10:11:07,056 INFO [ScanDirectoryJob] - myJob fire at Mon Aug 06 10:11:07 CST 2012
2012-08-06 10:11:07,056 INFO [ScanDirectoryJob] - D:\360Downloads\quartz-2.1.5\quartz-2.1.5\examples\build.xml, Size: 1613
2012-08-06 10:11:07,056 INFO [ScanDirectoryJob] - D:\360Downloads\quartz-2.1.5\quartz-2.1.5\examples\pom.xml, Size: 6085
2012-08-06 10:11:13,057 INFO [ScanDirectoryJob] - myJob fire at Mon Aug 06 10:11:13 CST 2012
2012-08-06 10:11:13,057 INFO [ScanDirectoryJob] - D:\360Downloads\quartz-2.1.5\quartz-2.1.5\examples\build.xml, Size: 1613
2012-08-06 10:11:13,057 INFO [ScanDirectoryJob] - D:\360Downloads\quartz-2.1.5\quartz-2.1.5\examples\pom.xml, Size: 6085