Spring Boot集成Quartz注入Spring管理的类的方法

网友投稿 414 2023-02-08


Spring Boot集成Quartz注入Spring管理的类的方法

在Spring Boot中使用Quartz时,在JOB中一般需要引用Spring管理的Bean,通过定义Job Factory实现自动注入。

Spring有自己的Schedule定时任务,在Spring boot中使用的时候,不能动态管理JOB,于是就使用Quartz来实现。

在Spring Boot中配置Quartz:

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

@EnableScheduling

public class QuartzSchedule {

@Autowired

private MyJobFactory myJobFactory;

@Bean

public SchedulerFactoryBean schedulerFactoryBean() throws IOException {

SchedulerFactoryBean factory = newQknmsnoWvv SchedulerFactoryBean();

factory.setOverwriteExistingJobs(true);

// 延时启动

factory.setStartupDelay(20);

// 加载quartz数据源配置

factory.setQuartzProperties(quartzProperties());

// 自定义Job Factory,用于Spring注入

factory.setJobFactory(myJobFactory);

return factory;

}

/**

* 加载quartz数据源配置

*

* @return

* @throws IOException

*/

@Bean

public Properties quartzProperties() throws IOException {

PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();

propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));

propertiesFactoryBean.afterPropertiesSet();

return propertiesFactoryBean.getObject();

}

}

为了在JOB中使用Spring管理的Bean,需要重新定义一个Job Factory:

@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;

}

}

然后在JOB中就可以使用Spring管理的Bean了

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();

}

}

下面代码是创建JOB:

JobDetail jobDetail = JobBuilder.newJob(((Job) Class.forName(job.getClazz()).newInstance()).getClass())

.withIdentity(job.getJobName(), job.getJobGroup()).build();

jobDetail.getJobDataMap().put("extdata", job.getExtData());

// 表达式调度构建器

CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression())

.withMisfireHandlingInstructionDoNothing();

// 构建一个trigger

TriggerBuilder triggerBuilder = TriggerBuilder.newTrigger().withIdentity(triggerKey)

.withSchedule(scheduleBuilder);

if (job.getStartTime() != null) {

triggerBuhttp://ilder.startAt(job.getStartTime());

}

if (job.getEndTime() != null) {

triggerBuilder.endAt(job.QknmsnoWvvgetEndTime());

}

CronTrigger trigger = triggerBuilder.build();

scheduler.scheduleJob(jobDetail, trigger);// 注入到管理类


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解vue 计算属性与方法跟侦听器区别(面试考点)
下一篇:vue移动端实现下拉刷新
相关文章

 发表评论

暂时没有评论,来抢沙发吧~