// 添加Job并关联trigger,触发
Date scheduleJob(JobDetail var1, Trigger var2) throws SchedulerException;
// 已经存在Job并trigger已于job关联,触发
Date scheduleJob(Trigger var1) throws SchedulerException;
void scheduleJobs(Map<JobDetail, Set<? extends Trigger>> var1, boolean var2) throws SchedulerException;
void scheduleJob(JobDetail var1, Set<? extends Trigger> var2, boolean var3) throws SchedulerException;
boolean unscheduleJob(TriggerKey var1) throws SchedulerException;
boolean unscheduleJobs(List<TriggerKey> var1) throws SchedulerException;
Date rescheduleJob(TriggerKey var1, Trigger var2) throws SchedulerException;
void addJob(JobDetail var1, boolean var2) throws SchedulerException;
void addJob(JobDetail var1, boolean var2, boolean var3) throws SchedulerException;
boolean deleteJob(JobKey var1) throws SchedulerException;
boolean deleteJobs(List<JobKey> var1) throws SchedulerException;
void triggerJob(JobKey var1) throws SchedulerException;
void triggerJob(JobKey var1, JobDataMap var2) throws SchedulerException;
void pauseJob(JobKey var1) throws SchedulerException;
void pauseJobs(GroupMatcher<JobKey> var1) throws SchedulerException;
void pauseTrigger(TriggerKey var1) throws SchedulerException;
void pauseTriggers(GroupMatcher<TriggerKey> var1) throws SchedulerException;
void resumeJob(JobKey var1) throws SchedulerException;
void resumeJobs(GroupMatcher<JobKey> var1) throws SchedulerException;
void resumeTrigger(TriggerKey var1) throws SchedulerException;
void resumeTriggers(GroupMatcher<TriggerKey> var1) throws SchedulerException;
void pauseAll() throws SchedulerException;
void resumeAll() throws SchedulerException;
主要分
- scheduleJob 新增job和trigger,启动触发器(不能重复添加,否则会抛出异常)
- unscheduleJob 删除trigger,停止触发器(不抛出异常)
- rescheduleJob 替换tiggle,启动触发器(不存在传入的triggerkey什么也不做,暂停的触发器也会重新启动)
- addJob 新增job或替换以前原有的job,保持原来状态
- deleteJob 删除Job和关联的Trigger,停止触发器(不抛出异常)
- triggerJob 触发Job类,只掉用一次,调用前必须在持久化中存在Job中(手动触发)(不存在会抛出异常)
- pauseJob
- pauseTrigger
- resumeJob
- resumeTrigger
一个triggle对应一个job
一个job可以对应多个triggle
所以当调用unscheduleJob 可能删除triggle,但是与job关联的有多个triggle,任务job仍在执行。
Date scheduleJob(JobDetail var1, Trigger var2);
示例:
- 先判断job和trigger是否存在,存在会抛出异常org.quartz.ObjectAlreadyExistsException: Unable to store Job : ‘gz.gz’, because one already exists with this identification.
// 2、创建JobDetail实例,并与PrintWordsJob类绑定(Job执行内容)
JobDetail jobDetail2 = JobBuilder.newJob(PrintWordsJob2.class)
.withIdentity("job2", "group2").build();
// 3、构建Trigger实例,每隔1s执行一次
Trigger trigger2 =TriggerBuilder.newTrigger().withIdentity("trigger2", "triggerGroup2").startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(60)//每隔1s执行一次
.repeatForever()).build();//一直执行
//4、执行
// 添加Job并关联、执行
scheduler.scheduleJob(jobDetail2, trigger2);
Date scheduleJob(Trigger var1)
- 使用时必须先判断job是否存在,不存在,会抛出org.quartz.JobPersistenceException: The job (gz.gz) referenced by the trigger does not exist.异常
- 必须判断trigger是否存在,存在,会抛出org.quartz.ObjectAlreadyExistsException: Unable to store Trigger with name: ‘gz’ and group: ‘gz’, because one already exists with this identification.异常
示例:
// 2、创建JobDetail实例,并与PrintWordsJob类绑定(Job执行内容)
JobDetail jobDetail2 = JobBuilder.newJob(PrintWordsJob2.class)
.withIdentity("job2", "group2").build();
// 3、构建Trigger实例,每隔1s执行一次
Trigger trigger2 =TriggerBuilder.newTrigger().withIdentity("trigger2", "triggerGroup2").forJob(jobDetail2).startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(60)//每隔1s执行一次
.repeatForever()).build();//一直执行
//4、执行
// 已经存在Job并trigger已于job关联,触发
scheduler.addJob(jobDetail2,true,true);
scheduler.scheduleJob(trigger2);
测试类
@RestController
@RequestMapping("/quartz")
public class QuartzController {
@Autowired
private Scheduler scheduler;
// 测试 scheduler.scheduleJob(jobDetail,trigger);
@RequestMapping("/scheduleJob")
public String scheduleJob(String name, String group) throws SchedulerException {
JobDetail jobDetail =
JobBuilder.newJob(HelloJob2.class).storeDurably()
.withIdentity(name, group).build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group)
.startNow()//立即生效
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(10)//每隔1s执行一次
.repeatForever()).build();//一直执行
scheduler.scheduleJob(jobDetail,trigger);
return "成功";
}
// 测试scheduler.scheduleJob(trigger);
@RequestMapping("/scheduleJob2")
public String scheduleJob2(String name, String group) throws SchedulerException {
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group).forJob("gz", "gz")
.startNow()//立即生效
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(1)//每隔1s执行一次
.repeatForever()).build();//一直执行
scheduler.scheduleJob(trigger);
return "成功";
}
// 测试 scheduler.unscheduleJob(new TriggerKey(name, group));
@RequestMapping("/unscheduleJob")
public String unscheduleJob(String name, String group) throws SchedulerException {
scheduler.unscheduleJob(new TriggerKey(name, group));
return "成功";
}
// 测试 scheduler.rescheduleJob(new TriggerKey(name, group), trigger);
@RequestMapping("/rescheduleJob")
public String rescheduleJob(String name, String group) throws SchedulerException {
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group)
.startNow()//立即生效
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(10)//每隔1s执行一次
.repeatForever()).build();//一直执行
scheduler.rescheduleJob(new TriggerKey(name, group), trigger);
return "成功";
}
// 测试 scheduler.deleteJob(new JobKey(name, group));
@RequestMapping("/addJob")
public String addJob(String name, String group) throws SchedulerException {
JobDetail jobDetail =
JobBuilder.newJob(HelloJob3.class).storeDurably()
.withIdentity(name, group).build();
scheduler.addJob(jobDetail, true, true);
return "成功";
}
// 测试 scheduler.deleteJob(new JobKey(name, group));
@RequestMapping("/deleteJob")
public String deleteJob(String name, String group) throws SchedulerException {
scheduler.deleteJob(new JobKey(name, group));
return "成功";
}
// 测试 scheduler.pauseJob(new JobKey(name, group));
@RequestMapping("/pauseJob")
public String pauseJob(String name, String group) throws SchedulerException {
scheduler.pauseJob(new JobKey(name, group));
return "成功";
}
// 测试 scheduler.pauseTrigger(new TriggerKey(name, group));
@RequestMapping("/pauseTrigger")
public String pauseTrigger(String name, String group) throws SchedulerException {
scheduler.pauseTrigger(new TriggerKey(name, group));
return "成功";
}
@RequestMapping("/resumeJob")
public String resumeJob(String name, String group) throws SchedulerException {
scheduler.resumeJob(new JobKey(name, group));
return "成功";
}
@RequestMapping("/resumeTrigger")
public String resumeTrigger(String name, String group) throws SchedulerException {
scheduler.resumeTrigger(new TriggerKey(name, group));
return "成功";
}
@RequestMapping("/triggerJob")
public String triggerJob(String name, String group) throws SchedulerException {
scheduler.triggerJob(new JobKey(name, group));
return "成功";
}
}