quartz---的Scheduler

                      quartz---的Scheduler

    quartz---的Scheduler

  从这副图中可以很直观的看出来quartz的关系:

    调度:Scheduler任务调度器,是实际执行任务调度的控制器。在spring中通过SchedulerFactoryBean封装起来。

    触发器:Trigger 触发器,用于定义任务调度的时间规则,有SimpleTrigger,CronTrigger,DateIntervalTrigger和NthIncludedDayTrigger,其中CronTrigger用的比较多,本文主要介绍这种方式。CronTrigger在spring中封装在CronTriggerFactoryBean中。

   1)scheduler.start();scheduler.standby();用代码解释:

 package com.imooc.demo.helloQuartz;

 import java.text.SimpleDateFormat;
import java.util.Date; import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) throws SchedulerException, InterruptedException {
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Current Time is:"+sf.format(date));
// 创建一个jobDatail实例,将该实力与helloJob绑定
JobDetail detail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob")
.build(); //美每秒触发一次任务
CronTrigger trigger = (CronTrigger)TriggerBuilder
.newTrigger()
.withIdentity("myTrigger", "group1")
//2017-2019每月的第二周的星期5下午14开始触发,每隔5秒允许一次
.withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?"))
.build();
// 创建Scheduler实例
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.start();
// 把jobDetail和Trigger与scheuler绑定在一起:触发启动
System.out.println("----:"+sf.format(scheduler.scheduleJob(detail, trigger)));;
Thread.sleep(2000L);
//挂起
scheduler.standby(); //scheduler挂起3秒后继续执行
Thread.sleep(3000L);
scheduler.start();
}
}

    每秒执行一次,2秒后停止,执行3次后停止执行,运行结果:

quartz---的Scheduler

  2)shutdown();

 package com.imooc.demo.helloQuartz;

 import java.text.SimpleDateFormat;
import java.util.Date; import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) throws SchedulerException, InterruptedException {
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Current Time is:"+sf.format(date));
// 创建一个jobDatail实例,将该实力与helloJob绑定
JobDetail detail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob")
.build(); //美每秒触发一次任务
CronTrigger trigger = (CronTrigger)TriggerBuilder
.newTrigger()
.withIdentity("myTrigger", "group1")
//2017-2019每月的第二周的星期5下午14开始触发,每隔5秒允许一次
.withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?"))
.build();
// 创建Scheduler实例
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.start();
// 把jobDetail和Trigger与scheuler绑定在一起:触发启动
System.out.println("----:"+sf.format(scheduler.scheduleJob(detail, trigger)));;
Thread.sleep(2000L);
//挂起
scheduler.shutdown(); //scheduler挂起3秒后继续执行
Thread.sleep(3000L);
scheduler.start();
}
}

运行结果为:

    quartz---的Scheduler

   总结 :

   在shutdown()后,程序不能再从新start().

 package com.imooc.demo.helloQuartz;

 import java.text.SimpleDateFormat;
import java.util.Date; import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) throws SchedulerException, InterruptedException {
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Current Time is:"+sf.format(date));
// 创建一个jobDatail实例,将该实力与helloJob绑定
JobDetail detail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob")
.build(); //美每秒触发一次任务
CronTrigger trigger = (CronTrigger)TriggerBuilder
.newTrigger()
.withIdentity("myTrigger", "group1")
//2017-2019每月的第二周的星期5下午14开始触发,每隔5秒允许一次
.withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?"))
.build();
// 创建Scheduler实例
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.start();
// 把jobDetail和Trigger与scheuler绑定在一起:触发启动
System.out.println("----:"+sf.format(scheduler.scheduleJob(detail, trigger)));;
Thread.sleep(2000L);
//shutdown(true)标志等待所有正在执行的job执行完之后,再关闭scheduler
//shutdown(false)即shutdown()表示job执行完毕之后,再关闭scheduler
//shutdown挂起3秒后继续执行
scheduler.shutdown(true);
System.out.println("scheruler is shut down?:"+scheduler.isShutdown()); }
}

运行结果:

  quartz---的Scheduler

总结 :

    shutdown(true)标志等待所有正在执行的job执行完之后,再关闭scheduler

quartz---的Scheduler

总结 :
    shutdown(false)即关闭scheduler ,然后执行job之后,关掉程序

上一篇:Why does typeof array with objects return “Object” and not “Array”?


下一篇:ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展(实例)