1.pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 定时器任务 quartz需要导入的坐标 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
2.生成job的工厂交给spring管理
@Component
public class MyJobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
// 调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
// 进行注入
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
3.定义quartz的配置类,向spring 注入
SchedulerFactoryBean 、JobDetailFactoryBean 、updateTriggerFactoryBean
JobDetailFactoryBean
package com.example.quartzdemo;
import java.io.IOException;import java.util.Properties;
import org.quartz.JobDetail;import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
/**
* Author: hezishan
* Date: 2018/6/6.
* Description:
**/
@Configuration
@EnableScheduling
public class QuartzSchedule {
@Autowired
private MyJobFactory myJobFactory;
@Bean
public SchedulerFactoryBean schedulerFactoryBean(Trigger updateTrigger) throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setOverwriteExistingJobs(true);
// 延时启动
factory.setStartupDelay(20);
// 自定义Job Factory,用于Spring注入
factory.setJobFactory(myJobFactory);
factory.setTriggers(updateTrigger); return factory;
}
@Bean(name = "updateJobDetail")
public JobDetailFactoryBean uploadJobDetail() {
JobDetailFactoryBean jobDetail = new JobDetailFactoryBean();
jobDetail.setJobClass(MyJob.class);
return jobDetail;
}
@Bean(name = "updateTrigger")
public CronTriggerFactoryBean updateTriggerFactoryBean(JobDetail updateJobDetail) {
CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
trigger.setCronExpression("0 0/1 * * * ?");
trigger.setJobDetail(updateJobDetail);
return trigger;
}
}
4.任务类:能够注入业务类,
@DisallowConcurrentExecution 标识这个任务是串行执行,不是并发执行
package com.example.quartzdemo;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.Date;
/**
* Author: hezishan
* Date: 2018/6/6.
* Description:
**/
@DisallowConcurrentExecution
public class MyJob implements Job, Serializable {
private static final long serialVersionUID = 1L;
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private SomeService someService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println(new Date());
someService.doSomething();
}
}
5.启动项目即可