package com.csl.quartz; import static org.quartz.CronScheduleBuilder.cronSchedule; import static org.quartz.JobBuilder.newJob; import static org.quartz.TriggerBuilder.newTrigger; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.impl.StdSchedulerFactory; public class QuartzTest { /** * @param args */ public static void main(String[] args) { SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); // jobs can be scheduled before sched.start() has been called // job 1 will run every 20 seconds String groupName = "group1"; JobDetail job = newJob(SimpleJob.class) .withIdentity("job1", groupName) .build(); CronTrigger trigger = newTrigger() .withIdentity("trigger1", groupName) .withSchedule(cronSchedule("0/20 * * * * ?")) .build(); sched.start(); } }
public class SimpleJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("xxxxxxx"); } }
此文摘自quartz的example3
文中job的groupName和trigger的groupName必须保持一致,
"0/20 * * * * ?" 可配置于配置文件中,但配置文件名不可为quartz.properties,因为StdSchedulerFactory在获取getScheduler的时候会自动去类路径下寻找quartz.properties,然后做一些初始化,如果没有quartz.properties也就算了,如果有的话,以至于一些属性没有配置会导致启动出错,Exception:Thread count must be >0