Quartz定时任务框架
程序员文章站
2022-06-08 18:19:25
...
一、框架入门
引入maven
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
实现简单的定时任务
创建一个任务
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//打印时间
System.out.println(new Date());
//任务
System.out.println("Hello World");
}
}
定时任务三要素:job、trigger、scheduler
public class HelloScheduler {
public static void main(String[] args) throws SchedulerException {
//Job
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
.withIdentity("myjob", "group1")
.build();
//创建一个Trigger实例,定义该job立即执行,并且每隔两秒执行一次
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("myTrigger", "group1")
.startNow()
.withSchedule
(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever())
.build();
//创建Scheduler实例
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail,trigger);
}
}
二、JobExecutionContext获取值
设置需要传递的值
public class HelloScheduler {
public static void main(String[] args) throws SchedulerException {
//Job
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
.withIdentity("myjob", "group1")
.usingJobData("message","hello job1")
.build();
//创建一个Trigger实例,定义该job立即执行,并且每隔两秒执行一次
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("myTrigger", "group1")
.usingJobData("message","hello trigger")
.startNow()
.withSchedule
(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever())
.build();
//创建Scheduler实例
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail,trigger);
}
}
1、使用JobDataMap获取值
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//打印时间
System.out.println(new Date());
//任务
System.out.println("Hello World");
//使用JobExecutionContext传值
JobKey key = jobExecutionContext.getJobDetail().getKey();
System.out.println("jobName:"+key.getName());
TriggerKey key1 = jobExecutionContext.getTrigger().getKey();
System.out.println("triggerName:"+key1.getName());
JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
//如果key相同则trigger覆盖job中的内容
String message = jobDataMap.getString("message");
System.out.println("message:"+message);
}
}
2、使用get/set方式获取值
public class HelloJob implements Job {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//打印时间
System.out.println(new Date());
//任务
System.out.println("Hello World");
//使用JobExecutionContext传值
JobKey key = jobExecutionContext.getJobDetail().getKey();
System.out.println("jobName:"+key.getName());
TriggerKey key1 = jobExecutionContext.getTrigger().getKey();
System.out.println("triggerName:"+key1.getName());
System.out.println("message:"+message);
}
}
三、浅谈Trigger
1、设置定时起始时间和结束时间
public class HelloScheduler {
public static void main(String[] args) throws SchedulerException {
//设置开始时间
Date startTime = new Date();
startTime.setTime(startTime.getTime()+3000);
//设置结束时间
Date endTime = new Date();
endTime.setTime(endTime.getTime()+3000);
//Job
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
.withIdentity("myjob", "group1")
.build();
//创建一个Trigger实例,定义该job立即执行,并且每隔两秒执行一次
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("myTrigger", "group1")
.usingJobData("message","hello trigger")
.startAt(startTime)
.endAt(endTime)
.withSchedule
(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever())
.build();
//创建Scheduler实例
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail,trigger);
}
}
2、SimpleTrigger介绍
设置任务开始时间,并且只执行一次
public class HelloScheduler {
public static void main(String[] args) throws SchedulerException {
//设置开始时间
Date startTime = new Date();
startTime.setTime(startTime.getTime()+2000);
//Job
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
.withIdentity("myjob", "group1")
.build();
//创建一个SimpleTrigger实例,定义该job两秒钟之后执行,并且只执行一次
SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger()
.withIdentity("myTrigger", "group1")
.startAt(startTime)
.build();
//创建Scheduler实例
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail,trigger);
}
}
设置按照指定次数执行任务
public class HelloScheduler {
public static void main(String[] args) throws SchedulerException {
//设置开始时间
Date startTime = new Date();
startTime.setTime(startTime.getTime() + 2000);
//Job
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
.withIdentity("myjob", "group1")
.build();
//创建一个SimpleTrigger实例,定义该job两秒钟之后执行,设置执行的次数
SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger()
.withIdentity("myTrigger", "group1")
.startAt(startTime)
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2)
//填入执行的次数,直接填入数字,下面指的是无线循环,无限次
.withRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY))
.build();
//创建Scheduler实例
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail, trigger);
}
}
3、CronTrigger作业调度器
//创建一个CronTrigger实例
CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger()
.withIdentity("myTrigger", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?"))
.build();
四、浅谈Scheduler
//创建Scheduler实例
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
//开始任务
scheduler.start();
//挂起任务
scheduler.standby();
//结束任务
scheduler.shutdown();
//表示现在所有执行的job完毕之后再关闭scheduler
scheduler.shutdown(true);
//返回的时间是最近一次执行任务的时间
Date date = scheduler.scheduleJob(jobDetail, trigger);
上一篇: JS基础(九)定时器
下一篇: 设计一个有getMin功能的栈