Springboot 使用 Quartz 定时器执行多个定时任务 配置篇
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import java.util.Date;
import static java.lang.Thread.sleep;
/**
@Author : JCccc
@CreateTime : 2020/3/9
@Description :
**/
@Component
@EnableScheduling
public class TaskJobOne {
public void testJobOneMethod() throws InterruptedException {
System.out.println("1 定时任务 1 正在执行......"+new Date());
sleep(1000);
System.out.println("1 定时任务 1 业务代码执行完毕......"+new Date());
}
}
然后是创建多个,也就是第二个定时任务的实例,TaskJobTwo.class:?(注意这里的 @EnableScheduling 别忘记了)
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import java.util.Date;
import static java.lang.Thread.sleep;
/**
@Author : JCccc
@CreateTime : 2020/3/9
@Description :
**/
@Component
@EnableScheduling
public class TaskJobTwo {
public void testJobTwoMethod() throws InterruptedException {
System.out.println("2 定时任务 2 正在执行......" + new Date());
sleep(1000);
System.out.println("2 定时任务 2 业务代码执行完毕......"+new Date());
}
}
然后是给这些任务实例绑定触发器,然后将触发器加入到调度工厂里:
创建?QuartzConfiguration.class:
(注意看每一步的注释,或者看后面我的简要要点描述!)
import com.demo.elegant.quartz.taskOne.TaskJobOne;
import com.demo.elegant.quartz.taskTwo.TaskJobTwo;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.anno
tation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
import java.util.Date;
/**
@Author : JCccc
@CreateTime : 2020/3/9
@Description :
**/
@Configuration
public class QuartzConfiguration {
/*
定时任务配置包括 :
jobDetail 任务实例
定时任务执行的具体内容等 -相关业务代码
trigger 触发器
设置相关的运行周期等 -绑定任务实例
scheduler 调度器
决定哪些定时任务会执行 -绑定触发器
*/
/**
定时任务 1
*/
// 配置定时任务 1 的任务实例
@Bean(name = "firstJobDetail")
public MethodInvokingJobDetailFactoryBean firstJobDetail(TaskJobOne taskJobOne) {
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
// 是否并发执行
jobDetail.setConcurrent(false);
// 为需要执行的实体类对应的对象
jobDetail.setTargetObject(taskJobOne);
// 需要执行的方法
jobDetail.setTargetMethod("testJobOneMethod");
return jobDetail;
}
// 配置触发器 1
@Bean(name = "firstTrigger")
public SimpleTriggerFactoryBean firstTrigger(JobDetail firstJobDetail) {
SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
trigger.setJobDetail(firstJobDetail);
// 设置任务启动延迟
trigger.setStartDelay(0);
// 设置定时任务启动时间
trigger.setStartTime(new Date());
// 每 5 秒执行一次
trigger.setRepeatInterval(5000);
return trigger;
}
/**
定时任务 2
@param taskJobTwo
@return
*/
// 配置定时任务 2 的任务实例
@Bean(name = "secondJobDetail")
public MethodInvokingJobDetailFactoryBean secondJobDetail(TaskJobTwo taskJobTwo) {
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
// 是否并发执行
jobDetail.setConcurrent(true);
// 为需要执行的实体类对应的对象
jobDetail.setTargetObject(taskJobTwo);
// 需要执行的方法
jobDetail.setTargetMethod("testJobTwoMethod");
return jobDetail;
}
// 配置触发器 2
@Bean(name = "secondTrigger")
public CronTriggerFactoryBean secondTrigger(JobDetail secondJobDetail) {
CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
trigger.setJobDetail(secondJobDetail);
// 设置定时任务启动时间
trigger.setStartTime(new Date());
// cron 表达式
trigger.setCronExpression("*/7 * * * * ?");
return trigger;
}
// 配置 Scheduler
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactory(Trigger firstTrigger, Trigger secondTrigger) {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
// 延时启动,应用启动 1 秒后
bean.setStartupDelay(1);
// 注册触发器
bean.setTriggers(firstTrigger, secondTrigger);
return bean;
}
}
评论