学习quartz cron表达式定时任务笔记,主要是想实现特定日期时间点执行定时任务.
1.测试任务类
package com.example.demo330.signin;
import com.example.demo330.utils.QuartzManager;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SignInJob implements Job {
static int count = 0;
static String cron = "0/5 * * * * ? *";
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
JobDetailImpl jdi = (JobDetailImpl) jobExecutionContext.getJobDetail();
JobKey jk = jdi.getKey();
String jobName = jk.getName();
String jobGroup = jk.getGroup();
Trigger trigger = jobExecutionContext.getTrigger();
TriggerKey key = trigger.getKey();
String name = key.getName();
String group = key.getGroup();
count = count + 1;
if (count > 5) {
cron = "0/10 * * * * ? *";
if (count > 10)
count = 0;
} else {
cron = "0/5 * * * * ? *";
}
String printTime = new SimpleDateFormat("yy-MM-dd HH-mm-ss").format(new Date());
System.out.println("SignInJob start at:" + printTime + "--jobName:" + jobName + "--jobGroup:" + jobGroup +
"--triggerName:" + name + "--triggerGroup:" + group + "--count:" + count +"--cron:"+cron);
QuartzManager.modifyJobTime(key, cron);
}
}
2.Quartz管理类 添加 启动 修改 停止
package com.example.demo330.utils;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzManager {
public static StdSchedulerFactory sf = new StdSchedulerFactory();
public static void addJob(JobKey jobKey,
TriggerKey triggerKey,
Class<? extends Job> jobClass,
String cro,
JobDataMap jobDataMap) {
try {
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobKey).setJobData(jobDataMap).build();
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
triggerBuilder.withIdentity(triggerKey);
triggerBuilder.startNow();
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cro));
CronTrigger trigger = (CronTrigger) triggerBuilder.build();
Scheduler scheduler = sf.getScheduler();
scheduler.scheduleJob(jobDetail, trigger);
if (!scheduler.isShutdown())
scheduler.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void modifyJobTime(TriggerKey triggerKey, String cron) {
try {
Scheduler scheduler = sf.getScheduler();
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
if (trigger == null)
return;
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(cron)) {
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
triggerBuilder.withIdentity(triggerKey);
triggerBuilder.startNow();
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
trigger = (CronTrigger) triggerBuilder.build();
scheduler.rescheduleJob(triggerKey, trigger);
} else {
}
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
public static void startJobs() {
try {
sf.getScheduler().start();
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
public static void shutdownJobs() {
try {
if (!sf.getScheduler().isShutdown())
sf.getScheduler().shutdown();
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
}
3.开启定时任务
JobDataMap dataMap1 = new JobDataMap();
dataMap1.put("类型1", "one");
dataMap1.put("类型2", "two");
JobKey jobKey = new JobKey("cron任务1", "任务组名1");
TriggerKey triggerKey = new TriggerKey("cron触发器1", "cron触发器组名1");
QuartzManager.addJob(jobKey,triggerKey,SignInJob.class,"0/5 * * * * ? *",dataMap1);
QuartzManager.startJobs();
- 测试job执行中 修改cron 定时配置. 5秒改为10秒执行一次.
SignInJob start at:22-04-08 14-46-20--jobName:cron任务1--jobGroup:任务组名1--triggerName:cron触发器1--triggerGroup:cron触发器组名1--count:1--cron:0/5 * * * * ? *
SignInJob start at:22-04-08 14-46-25--jobName:cron任务1--jobGroup:任务组名1--triggerName:cron触发器1--triggerGroup:cron触发器组名1--count:2--cron:0/5 * * * * ? *
SignInJob start at:22-04-08 14-46-30--jobName:cron任务1--jobGroup:任务组名1--triggerName:cron触发器1--triggerGroup:cron触发器组名1--count:3--cron:0/5 * * * * ? *
SignInJob start at:22-04-08 14-46-35--jobName:cron任务1--jobGroup:任务组名1--triggerName:cron触发器1--triggerGroup:cron触发器组名1--count:4--cron:0/5 * * * * ? *
SignInJob start at:22-04-08 14-46-40--jobName:cron任务1--jobGroup:任务组名1--triggerName:cron触发器1--triggerGroup:cron触发器组名1--count:5--cron:0/5 * * * * ? *
SignInJob start at:22-04-08 14-46-45--jobName:cron任务1--jobGroup:任务组名1--triggerName:cron触发器1--triggerGroup:cron触发器组名1--count:6--cron:0/10 * * * * ? *
SignInJob start at:22-04-08 14-46-50--jobName:cron任务1--jobGroup:任务组名1--triggerName:cron触发器1--triggerGroup:cron触发器组名1--count:7--cron:0/10 * * * * ? *
SignInJob start at:22-04-08 14-47-00--jobName:cron任务1--jobGroup:任务组名1--triggerName:cron触发器1--triggerGroup:cron触发器组名1--count:8--cron:0/10 * * * * ? *
SignInJob start at:22-04-08 14-47-10--jobName:cron任务1--jobGroup:任务组名1--triggerName:cron触发器1--triggerGroup:cron触发器组名1--count:9--cron:0/10 * * * * ? *
SignInJob start at:22-04-08 14-47-20--jobName:cron任务1--jobGroup:任务组名1--triggerName:cron触发器1--triggerGroup:cron触发器组名1--count:10--cron:0/10 * * * * ? *
|