1. 在Spring Boot项目的pom.xml文件中添加quartz-starter依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
2. 配置quartz
在Spring Boot项目的application.yml文件中添加quartz配置:
quartz:
job-store-type: jdbc
jdbc:
initialize-schema: always
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=utf-8
username: root
password: root
properties:
org.quartz.scheduler.instanceName: MyClusteredScheduler
org.quartz.scheduler.instanceId: AUTO
org.quartz.scheduler.skipUpdateCheck: true
org.quartz.scheduler.jmx.export: true
org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties: false
org.quartz.jobStore.tablePrefix: QRTZ_
org.quartz.jobStore.isClustered: true
org.quartz.jobStore.clusterCheckinInterval: 20000
设置集群的并发锁,防止集群执行重复
org.quartz.jobStore.acquireTriggersWithinLock=true
3. 创建quartz数据库
在MySQL中创建quartz数据库,并执行quartz.sql脚本,创建quartz所需的表。
4. 创建QuartzConfig类
在Spring Boot项目中创建QuartzConfig类,用于配置quartz:
@Configuration
public class QuartzConfig {
@Bean
public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource) throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setDataSource(dataSource);
// quartz参数
Properties prop = new Properties();
prop.put("org.quartz.scheduler.instanceName", "MyClusteredScheduler");
prop.put("org.quartz.scheduler.instanceId", "AUTO");
prop.put("org.quartz.scheduler.skipUpdateCheck", "true");
prop.put("org.quartz.scheduler.jmx.export", "true");
prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
prop.put("org.quartz.jobStore.useProperties", "false");
prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
prop.put("org.quartz.jobStore.isClustered", "true");
prop.put("org.quartz.jobStore.clusterCheckinInterval", "20000");
factory.setQuartzProperties(prop);
factory.setSchedulerName("MyClusteredScheduler");
// 延时启动
factory.setStartupDelay(1);
factory.setApplicationContextSchedulerContextKey("applicationContextKey");
// 可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
factory.setOverwriteExistingJobs(true);
// 设置自动启动,默认为true
factory.setAutoStartup(true);
return factory;
}
}
5. 创建定时任务
在Spring Boot项目中创建定时任务,实现QuartzJobBean接口:
@Component
@DisallowConcurrentExecution
public class SampleJob implements QuartzJobBean {
private String name;
@Override
public void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("Hello " + name + "!");
}
public void setName(String name) {
this.name = name;
}
}
6. 创建JobDetail
在Spring Boot项目中创建JobDetail,用于配置定时任务:
@Component
public class SampleJobDetail {
@Autowired
private Scheduler scheduler;
@PostConstruct
public void start() throws SchedulerException {
JobDetail jobDetail = JobBuilder.newJob(SampleJob.class).withIdentity("sampleJob").storeDurably().build();
jobDetail.getJobDataMap().put("name", "quartz");
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
CronTrigger cronTrigger = TriggerBuilder.newTrigger().forJob(jobDetail).withIdentity("sampleTrigger").withSchedule(cronScheduleBuilder).build();
scheduler.scheduleJob(jobDetail, cronTrigger);
}
}
7. 启动Spring Boot项目
启动Spring Boot项目,定时任务就会按照配置的时间执行,且不会重复执行。