本文最后更新于 1769 天前,其中的信息可能已经有所发展或是发生改变。
1. pom
<!--springboot整合quartz需要用到-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!--Quartz定时器-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--Scheduled定时器坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
2. 创建Job类
/**
* @author yuyy
* @date 20-2-5 下午3:11
*/
public class QuartzDemo implements Job {
@Autowired
private UserService userService;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//System.out.println("Quartz定时器执行--------"+new Date());
userService.testQuartz();
}
}
3. 创建Quartz启动类
/**
* SpringBoot整合Quartz的配置类
* @author yuyy
* @date 20-2-5 下午4:32
*/
@Configuration
public class QuartzConfig {
/**
* 创建job对象
* 做什么
*/
@Bean
public JobDetailFactoryBean jobDetailFactoryBean(){
JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
//关联自定义的job类
factoryBean.setJobClass(QuartzDemo.class);
return factoryBean;
}
/**
* 创建Trigger对象
* 什么时候做
*/
//@Bean
public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
//关联jobdetail
factoryBean.setJobDetail(jobDetailFactoryBean.getObject());
//执行相隔毫秒
factoryBean.setRepeatInterval(2000);
//执行重复次数
factoryBean.setRepeatCount(5);
return factoryBean;
}
/**
* 使用cron表达式
* 创建Trigger对象
* 什么时候做
*/
@Bean
public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
//关联jobdetail
factoryBean.setJobDetail(jobDetailFactoryBean.getObject());
factoryBean.setCronExpression("0/2 * * * * ?");
return factoryBean;
}
/**
* 创建Schedul对象
*/
@Bean
public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean
,MyAdaptableJobFactory MyAdaptableJobFactory){
SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
factoryBean.setTriggers(cronTriggerFactoryBean.getObject());
//避免注入异常,没有向Job类注入其他对象时不用写这个
factoryBean.setJobFactory(MyAdaptableJobFactory);
return factoryBean;
}
}
4. 修改启动类
@SpringBootApplication
@EnableScheduling //启用定时器
public class SpringBootDemoApplication {
public static void main(String[] args) throws SchedulerException {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
5. 注入异常
- 实例化Job类时使用了
AdaptableJobFactory
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Class<?> jobClass = bundle.getJobDetail().getJobClass();
return ReflectionUtils.accessibleConstructor(jobClass, new Class[0]).newInstance();
}
- 这里是利用反射来实例化对象,并没有添加到SpringIoc容器里,所以向Job类对象注入service对象时报错
- 重写该方法
package com.yuyy.springbootdemo.config;
/**
* 重写AdaptableJobFactory的createJobInstance()方法
* 避免在Quartz job中无法使用ioc里的对象,例如service
* @author yuyy
* @date 20-2-5 下午4:57
*/
@Component("myAdaptableJobFactory")
public class MyAdaptableJobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
//将对象添加到springIOC容器中,并且完成该对象注入
autowireCapableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}