<dependency>
<groupId>org.quartz-schedulergroupId>
<artifactId>quartzartifactId>
<version>2.3.2version>
dependency>
@Slf4j
public class QuartzTask implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
log.info(new Date() + "");
}
}
public class Test {
public static void main(String[] args) throws SchedulerException {
// 1 创建 job 对象
JobDetail job = JobBuilder.newJob(QuartzTask.class).build();
// 2 创建 trigger 对象
// Trigger trigger = TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever()).build();
Trigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("*/2 * * * * ?")).build();
// 3 创建 scheduler 对象
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.scheduleJob(job,trigger);
// 4 启动
scheduler.start();
}
}
<dependency>
<groupId>org.quartz-schedulergroupId>
<artifactId>quartzartifactId>
<version>2.3.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
dependency>
@Configuration
public class QuartzConfig {
// 1 创建 job 对象
@Bean
public JobDetailFactoryBean jobDetailFactoryBean(){
JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
jobDetailFactoryBean.setJobClass(SpringbootQuartzTask.class);
return jobDetailFactoryBean;
}
// 2 创建 trigger 对象
@Bean
public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
SimpleTriggerFactoryBean simpleTriggerFactoryBean = new SimpleTriggerFactoryBean();
simpleTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
simpleTriggerFactoryBean.setRepeatInterval(2000);
return simpleTriggerFactoryBean;
}
// 3 创建 scheduler 对象
@Bean
public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean){
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setTriggers(simpleTriggerFactoryBean.getObject());
return schedulerFactoryBean;
}
}
@Configuration
public class QuartzConfig {
// 1 创建 job 对象
@Bean
public JobDetailFactoryBean jobDetailFactoryBean(){
JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
jobDetailFactoryBean.setJobClass(SpringbootQuartzTask.class);
return jobDetailFactoryBean;
}
// 2 创建 trigger 对象
@Bean
public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
cronTriggerFactoryBean.setCronExpression("*/2 * * * * ?");
return cronTriggerFactoryBean;
}
// 3 创建 scheduler 对象
@Bean
public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean){
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setTriggers(cronTriggerFactoryBean.getObject());
return schedulerFactoryBean;
}
}
@EnableScheduling
@SpringBootApplication
public class SpringbootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskApplication.class, args);
}
}
java.lang.NullPointerException
如果按照上面的方法,在 job 类中调用 service 中的方法,会出现
java.lang.NullPointerException: null
错误。这是因为在实例化 job 类时并没有由 spring 处理,而是由 AdaptableJobFactory 类中的
protected Object createJobInstance(TriggerFiredBundle bundle)
通过反射创建的,所以 spring 的 ioc 容器中就没有 job 的实例。因此,service 的实例就无法注入。调用会出现空指针。注入的与被注入的对象都要被 spring 管理才能完成注入。
部分源码
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Class<?> jobClass = bundle.getJobDetail().getJobClass();
return ReflectionUtils.accessibleConstructor(jobClass).newInstance();
}
可以自己编写一个类,继承 AdaptableJobFactory,然后重写 createJobInstance 方法。将创建的实例对象手动添加到 IOC 容器中,完成注入。
package com.example.demo;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;
@Component
public class MyAdaptableJobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object object = super.createJobInstance(bundle);
autowireCapableBeanFactory.autowireBean(object);
return object;
}
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean,MyAdaptableJobFactory myAdaptableJobFactory){
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setTriggers(cronTriggerFactoryBean.getObject());
// 设置 setJobFactory
schedulerFactoryBean.setJobFactory(myAdaptableJobFactory);
return schedulerFactoryBean;
}
@Configuration
public class QuartzConfig {
@Autowired
SpringbootQuartzTask springbootQuartzTask;
// 1 创建 job 对象
@Bean
public JobDetailFactoryBean jobDetailFactoryBean(){
JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
jobDetailFactoryBean.setJobClass(springbootQuartzTask.getClass());
return jobDetailFactoryBean;
}
// 2 创建 trigger 对象
@Bean
public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
cronTriggerFactoryBean.setCronExpression("*/2 * * * * ?");
return cronTriggerFactoryBean;
}
// 3 创建 scheduler 对象
@Bean
public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean, MyAdaptableJobFactory myAdaptableJobFactory){
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setTriggers(cronTriggerFactoryBean.getObject());
// // 设置 setJobFactory
schedulerFactoryBean.setJobFactory(myAdaptableJobFactory);
return schedulerFactoryBean;
}
@Component
private class MyAdaptableJobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object object = super.createJobInstance(bundle);
autowireCapableBeanFactory.autowireBean(object);
return object;
}
}
}