SpringBoot Schedule Quartz
前一段时间写了定时任务,记录在此。
1.maven 配置 此外还有11张表,官网有提供创建表sql
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
表名
QRTZ_FIRED_TRIGGERS;
QRTZ_PAUSED_TRIGGER_GRPS;
QRTZ_SCHEDULER_STATE; -- 运行中实例的状态
QRTZ_LOCKS;
QRTZ_SIMPLE_TRIGGERS;
QRTZ_SIMPROP_TRIGGERS;
QRTZ_CRON_TRIGGERS; -- cron表达式
QRTZ_BLOB_TRIGGERS;
QRTZ_TRIGGERS; -- 每个定时任务的上次及下次执行时间, 执行状态(BLOCKED,WAITING等)
QRTZ_JOB_DETAILS;
QRTZ_CALENDARS
2.创建JobProperties类
@Getter
@Setter
public class JobProperties {
private final String jobName;
private final String jobGroup;
private final HashMap<String,String> jobParams;
private String cronExpression;
final Class< ? extends Job> jobRunner;
}
3.创建SchedulerModel类
@Getter
@Setter
public class SchedulerModel {
private final JobDetail jobDetail;
private final JobTrigger jobTrigger;
}
4.创建Model生成接口
public interface IschedulerModelGenerator {
SchedulerModel generateModelFrom(JobProperties jobProperties);
}
5.Model接口实现
@Service
public class SchedulerModelGenerator implements IschedulerModelGenerator {
public ScheduleModel generateModelFrom(JobProperties jobProperties) {
JobDetail jobDetail = getJobDetailFor(jobProperties.getJobName(), jobProperties.getJobGroup(), jobProperties.getJobParams(),
jobProperties.getJobRunner());
Trigger trigger = getTriggerFor(jobDetail,jobProperties.getCronExpression());
return new ScheduleModel(jobDetail, trigger);
}
private JobDetail getJobDetailFor(String jobName, String groupName, HashMap<String, String> jobParams, Class<? extends Job> jobRunner) {
return JobBuilder.newJob(jobRunner)
.setJobData(getJobDataMapFrom(jobParams))
.withDescription(jobName + groupName)
.withIdentity(jobName, groupName)
.storeDurably(false)
.build();
}
private JobDataMap getJobDataMapFrom(HashMap<String, String> jobParams) {
JobDataMap jobDataMap = new JobDataMap();
for (Map.Entry<String, String> entry : jobParams.entrySet()) {
jobDataMap.put(entry.getKey(), entry.getValue());
}
return jobDataMap;
}
private Trigger getTriggerFor(JobDetail jobDetail, String cronExpression) {
return TriggerBuilder.newTrigger()
.forJob(jobDetail)
.withSchedule(cronSchedule(cronExpression))
.build();
}
}
6.shceduler基础类
public abstract class CronSchedulerBase {
private final SchedulerFactoryBean schedulerFactoryBean;
private final ISchedulerModelGenerator schedulerModelGenerator;
public CronSchedulerBase(SchedulerFactoryBean schedulerFactoryBean,
ISchedulerModelGenerator schedulerModelGenerator) {
this.schedulerFactoryBean = schedulerFactoryBean;
this.schedulerModelGenerator = schedulerModelGenerator;
}
@PostConstruct
public void init() throws SchedulerException {
scheduleJob();
}
private void scheduleJob() throws SchedulerException {
JobProperties jobProperties = getJobProperties();
Scheduler scheduler = schedulerFactoryBean.getScheduler();
ScheduleModel scheduleModel = schedulerModelGenerator.generateModelFrom(jobProperties);
if (scheduler.checkExists(scheduleModel.getJobDetail().getKey())){
scheduler.deleteJob(scheduleModel.getJobDetail().getKey());
}
scheduler.scheduleJob(scheduleModel.getJobDetail(),scheduleModel.getTrigger());
scheduler.start();
}
protected abstract JobProperties getJobProperties();
}
7.创建TestScheduler
@Service
public class TestScheduler extends CronSchedulerBase {
private static final String JOB_NAME = "TestScheduler";
private static final String GROUP_NAME = "TestSchedulerGroup";
private static final String CRON_EXPRESSION = "0 0 0/1 ? * * *";
@Autowired
public TestScheduler(SchedulerFactoryBean schedulerFactoryBean,
ISchedulerModelGenerator schedulerModelGenerator) {
super(schedulerFactoryBean, schedulerModelGenerator);
}
@Override
protected JobProperties getJobProperties() {
return new JobProperties(JOB_NAME, GROUP_NAME, CRON_EXPRESSION, TestSchedulerJob.class);
}
}
8.创建TestSchedulerJob
public class TestSchedulerJob implements Job {
public void execute (JobExecutionContext jobExecutionContext) {
//业务逻辑
}
}