定时任务 quartz
Quartz有四个核心接口Job、JobDetail、Trigger和Scheduler。Trigger主要有CronTrigger(表达式触发)和SimpleTrigger(简单触发)两个接口,表达式触发的功能更强大,在开发时使用比较广泛,Job是一个实现org.quartz.Job接口的java类。将需要完成的任务写在execute()方法中。一个job可以有多个trigger,一个trigger只能有一个job。JobDetail包含了Job的调度策略和调度方案。Scheduler是对定时任务进行调度的控制器。Quartz的运行原理如图所示。
Scheduler是整个任务调度的总部,里面注册了很多的JobDetail和trigger。Trigger主要用来设置调度参数,用来设置多久进行一次任务调度。当Scheduler容器启动时,JobDetail和Trigger会在scheduler容器上进行注册。注册后,JobDetail和Trigger将会组成的一对装配好的作业。在Scheduler中有一个线程池,按照Trigger中配置的规则,为每个作业开启一个线程来并行执行。
SimpleTrigger
应用小demo
public class HelloJob implements Job{
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println(new Date());
}
}
=================================================================
package job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import java.util.Date;
import static org.quartz.DateBuilder.evenSecondDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
/**
* created by reedfan on 2019/7/4 0004
*/
public class SimpleExample {
public void run() throws Exception {
System.out.println("------- Initializing ----------------------");
// 定义调度器
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
System.out.println("------- Initialization Complete -----------");
// 获取当前时间的下一秒钟
Date runTime = evenSecondDate(new Date());
System.out.println("------- Scheduling Job -------------------");
// 定义job
// 在quartz中,有组的概念,组+job名称 唯一的
JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();
// 定义触发器,在下一秒钟启动
Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
// 将job注册到调度器
sched.scheduleJob(job, trigger);
System.out.println(job.getKey() + " will run at: " + runTime);
// 启动调度器
sched.start();
System.out.println("------- Started Scheduler -----------------");
// 等待5秒
System.out.println("------- Waiting 5 seconds... -------------");
try {
// wait 65 seconds to show job
Thread.sleep(5L * 1000L);
// executing...
} catch (Exception e) {
//
}
// 关闭调度器
System.out.println("------- Shutting Down ---------------------");
sched.shutdown(true);
System.out.println("------- Shutdown Complete -----------------");
}
public static void main(String[] args) throws Exception {
SimpleExample example = new SimpleExample();
example.run();
}
}
运行结果
CronTrigger
package job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import java.util.Date;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.DateBuilder.evenSecondDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
/**
* created by reedfan on 2019/7/4 0004
*/
public class SimpleExample {
public void run() throws Exception {
System.out.println("------- Initializing ----------------------");
// 定义调度器
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
System.out.println("------- Initialization Complete -----------");
// 获取当前时间的下一秒钟
Date runTime = evenSecondDate(new Date());
System.out.println("------- Scheduling Job -------------------");
// 定义job
// 在quartz中,有组的概念,组+job名称 唯一的
JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();
// 定义触发器,在下一钟启动
// Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
// 将job注册到调度器
// 定义触发器,每2秒执行一次
Trigger trigger = newTrigger().withIdentity("trigger1", "group1")
.withSchedule(cronSchedule("0/2 * * * * ?")).build();
sched.scheduleJob(job, trigger);
System.out.println(job.getKey() + " will run at: " + runTime);
// 启动调度器
sched.start();
System.out.println("------- Started Scheduler -----------------");
// 等待5秒
System.out.println("------- Waiting 10 seconds... -------------");
try {
// wait 10 seconds to show job
Thread.sleep(10L * 1000L);
// executing...
} catch (Exception e) {
//
}
// 关闭调度器
System.out.println("------- Shutting Down ---------------------");
sched.shutdown(true);
System.out.println("------- Shutdown Complete -----------------");
}
public static void main(String[] args) throws Exception {
SimpleExample example = new SimpleExample();
example.run();
}
}