相关概念就不介绍了,直接上代码。
pom.xml配置
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
Quartz配置类
@Configuration
public class QuartzConfig {
private final Scheduler scheduler;
public QuartzConfig(Scheduler scheduler) {
this.scheduler = scheduler;
}
@Bean(name = "scheduler")
public Scheduler setScheduler() {
return scheduler;
}
}
启动Quartz配置类
@Configuration
public class StartQuartz implements CommandLineRunner {
@Resource(name = "scheduler")
private Scheduler scheduler;
@Override
public void run(String... args) throws Exception {
// 启动调度器
scheduler.start();
// 定时执行任务
JobDetail exportJob = JobBuilder.newJob(StartQuartzJob.class).withIdentity(StartQuartzJob.class.getName(), "startTask").build();
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
CronTrigger exportTrigger = TriggerBuilder.newTrigger().withIdentity(StartQuartzJob.class.getName(), "startTask")
.withSchedule(scheduleBuilder).build();
try {
scheduler.scheduleJob(exportJob, exportTrigger);
} catch (SchedulerException e) {
System.out.println("创建定时任务失败" + e);
throw new Exception("创建定时任务失败");
}
}
}
Job任务类
public class StartQuartzJob extends QuartzJobBean {
@Resource(name = "scheduler")
private Scheduler scheduler;
@Autowired
private TaskAdo taskAdo;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
System.out.println(simpleDateFormat.format(new Date()) + ":[StartQuartzJob]:"+jobExecutionContext.getJobDetail().getKey().toString());
String jobName = "";
String jobCron = "";
String taskGroup = "";
String taskType = "";
Long dtID = 0L;
JobDetail exportJob = null;
Date onceTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
JobKey jobKey = null;
try {
List<Map<String, Object>> mapTaskCronList = null;
// 查询任务,Cron
mapTaskCronList = taskAdo.getAllTaskCron();
for (Map mapTaskCron : mapTaskCronList) {
taskGroup = mapTaskCron.get("task_group").toString();
jobCron = mapTaskCron.get("cron") == null ? "" : mapTaskCron.get("cron").toString();
jobName = mapTaskCron.get("task_name").toString();
taskType = mapTaskCron.get("task_type").toString();
jobKey = JobKey.jobKey(jobName, taskGroup);
dtID = Long.parseLong(mapTaskCron.get("dt_id").toString());
// 判断任务是否已经存在
if (!scheduler.checkExists(jobKey)) {
// 添加一个任务
exportJob = JobBuilder.newJob(ReadDbJob.class).withIdentity(jobName, taskGroup).build();
// 根据 任务类型创建不同触发器
if ("REPT".equals(taskType)) {
// cron 表达式重复执行触发器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(jobCron);
CronTrigger exportTrigger = TriggerBuilder.newTrigger().withIdentity(jobName, taskGroup).withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(exportJob, exportTrigger);
} else {
// 指定时间点单次执行
onceTime = sdf.parse(mapTaskCron.get("one_time").toString().replace("T", " "));
SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger().withIdentity(jobName, taskGroup).startAt(onceTime)
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(10) // 指定时间点滞后多少秒
.withRepeatCount(0)) // 重复执行多少次
.build();
scheduler.scheduleJob(exportJob, simpleTrigger);
}
} else {
// 只有重复执行的才需要的才会有变更cron表达式重复条件
if ("REPT".equals(taskType)) {
// 根据jobName获取TriggerKey,CronTrigger
TriggerKey currTriggerKey = TriggerKey.triggerKey(jobName, taskGroup);
CronTrigger currTrigger = (CronTrigger) scheduler.getTrigger(currTriggerKey);
//当前Trigger使用的cron
String currentCron = currTrigger.getCronExpression();
// 如果不一致,则刷新任务
if (!currentCron.equals(jobCron)) {
//表达式调度构建器
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(jobCron);
//按新的cronExpression表达式重新构建trigger
currTrigger = (CronTrigger) scheduler.getTrigger(currTriggerKey);
currTrigger = currTrigger.getTriggerBuilder().withIdentity(currTriggerKey).withSchedule(scheduleBuilder).build();
// 按新的trigger重新设置job执行
scheduler.rescheduleJob(currTriggerKey, currTrigger);
}
}
}
}
} catch (Exception e) {
System.out.println("checkExists失败" + e);
}
}
}
ReadDbJob任务类
public class ReadDbJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
System.out.println(simpleDateFormat.format(new Date()) + ":ReadDbJob:"+jobExecutionContext.getJobDetail().getKey().toString());
}
}