Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中。它提供了巨大的灵 活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。
eg:
jar
entity (Plan)
public class Plan { //时间 private String date; //任务 private String task; public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getTask() { return task; } public void setTask(String task) { this.task = task; } public Plan(String date, String task) { this.date = date; this.task = task; } public Plan() { } }
service (RemindService)
public class RemindService { public void printMessage(){ List<Plan> list = getPlansForTooday(); for (Plan plan : list) { System.out.println("计划的时间"+plan.getDate()+plan.getTask()); } } private List<Plan> getPlansForTooday() { List<Plan> list = new ArrayList<Plan>(); Plan plan1 = new Plan("2016年11月3日","月底可以购买回家的车票"); Plan plan2 = new Plan("2016年11月3日","webService"); list.add(plan2); list.add(plan1); return list; } }
quartz (Remindjob)
public class RemindJob implements Job { private RemindService service=new RemindService(); @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { service.printMessage(); } public RemindService getService() { return service; } public void setService(RemindService service) { this.service = service; } }
测试类
一分钟后只运行一次
public class MyTest { public static void tool() throws SchedulerException{ //一 构建job JobDetail job = JobBuilder.newJob(RemindJob.class)// .withIdentity("job1","group1")// .build(); //二 构建Trigger Date runTime = DateBuilder.evenMinuteDate(new Date()); Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")// .withSchedule(SimpleScheduleBuilder.simpleSchedule() .startAt(runTime)// .build();// //三 绑定job和rigger SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); sched.scheduleJob(job,trigger); sched.start(); } public static void main(String[] args) throws SchedulerException { tool(); }
每两秒运行一次
public class MyTest { public static void tool() throws SchedulerException{ //一 构建job JobDetail job = JobBuilder.newJob(RemindJob.class)// .withIdentity("job1","group1")// .build(); //二 构建Trigger Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")// .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(2) .repeatForever()) .startAt(new Date(System.currentTimeMillis()+2000)) .build();// //三 绑定job和rigger SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); sched.scheduleJob(job,trigger); sched.start(); } public static void main(String[] args) throws SchedulerException { tool(); } }
表达式
public static void tools() throws SchedulerException{ //一 构建job JobDetail job = JobBuilder.newJob(RemindJob.class)// .withIdentity("job1","group1")// .build(); //二 构建Trigger Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")// .withSchedule(CronScheduleBuilder.cronSchedule("0 0 12 * * ?")) .startAt(new Date()).build(); //三 绑定job和rigger SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); sched.scheduleJob(job,trigger); sched.start(); } public static void main(String[] args) throws SchedulerException { tool(); tools(); }
表达式的特殊字符