When using Quartz in Spring Boot, you generally need to reference Spring-managed beans in JOB and automatically inject them by defining a Job Factory.
Spring has its own Schedule timing tasks. When used in Spring boot, it cannot dynamically manage JOBs, so it is implemented using Quartz.
Configure Quartz in Spring Boot:
import java.io.IOException;import java.util.Properties;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.SchedulerFactoryBean;@Configuration@EnableSchedulingpublic class QuartzSchedule { @Autowired private MyJobFactory myJobFactory; @Bean public SchedulerFactoryBean schedulerFactoryBean() throws IOException { SchedulerFactoryBean factory = new SchedulerFactoryBean(); factory.setOverwriteExistingJobs(true); // Delay startup factory.setStartupDelay(20); // Load quartz data source configuration factory.setQuartzProperties(quartzProperties()); // Custom Job Factory for Spring injection factory.setJobFactory(myJobFactory); return factory; } /** * Load quartz data source configuration* * @return * @throws IOException */ @Bean public Properties quartzProperties() throws IOException { PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties")); propertiesFactoryBean.afterPropertiesSet(); return propertiesFactoryBean.getObject(); }} In order to use Spring-managed Beans in JOB, a Job Factory needs to be redefined:
@Componentpublic class MyJobFactory extends AdaptableJobFactory { @Autowired private AutowireCapableBeanFactory capableBeanFactory; @Override protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { // Call the method of the parent class Object jobInstance = super.createJobInstance(bundle); // Inject capableBeanFactory.autowireBean(jobInstance); return jobInstance; }} Then you can use Spring managed beans in the JOB
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 { someService.doSomething(); }} The following code creates a JOB:
JobDetail jobDetail = JobBuilder.newJob(((Job) Class.forName(job.getClazz()).newInstance()).getClass()) .withIdentity(job.getJobName(), job.getJobGroup()).build(); jobDetail.getJobDataMap().put("extdata", job.getExtData()); // Expression ScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()) .withMisfireHandlingInstructionDoNothing(); // Build a trigger TriggerBuilder<CronTrigger> triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerKey) .withSchedule(scheduleBuilder); if (job.getStartTime() != null) { triggerBuilder.startAt(job.getStartTime()); } if (job.getEndTime() != null) { triggerBuilder.endAt(job.getEndTime()); } CronTrigger trigger = triggerBuilder.build(); scheduler.scheduleJob(jobDetail, trigger);// Inject to management class The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.