Quartz 的介绍
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或 EJBs。Quartz的最新版本为Quartz 2.3.0。
Quartz的使用思路
1)job-任务-做什么
2)Tigger-触发器-什么时候做
3)Scheduler-任务调度- 什么时候需要去做什么事情
添加Quart依赖
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
Demo
public class QuartzDemo implements Job {
/**
* 任务触发时所执行的方法
* @param jobExecutionContext
* @throws JobExecutionException
*/
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//1.创建Job对象,你要做什么事情?
System.out.println("执行啊啊啊 "+new Date());
}
public static void main(String[] args) throws SchedulerException {
JobDetail job= JobBuilder.newJob(QuartzDemo.class).build();
/**
* 简单的trigger触发时间,通过Quartz提供一个方法来完成简单的重复调用cron
* Trigger按照cron的 表达式来给定触发时间
*/
//2.创建trigger对象,在什么时间做
//Trigger trigger=TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever()).build();
Trigger trigger=TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("0/3 * * * * ?")).build();
Scheduler scheduler= StdSchedulerFactory.getDefaultScheduler();
//3.赋予任务,什么时间做什么
scheduler.scheduleJob(job,trigger);
//启动
scheduler.start();
}
}
SpringBoot整合quartz定时任务框架
- 依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!--Quartz坐标-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--Spring tx依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
编写Quartz的配置类
@Configuration
public class QuartzConfig {
/**
* 1.创建Job对象
*/
@Bean
public JobDetailFactoryBean jobDetailFactoryBean() {
JobDetailFactoryBean factory = new JobDetailFactoryBean();
factory.setJobClass(QuartzDemo2.class);
return factory;
}
/**
* 2.创建Trigger对象
* 创建简单的Trigger
*/
// @Bean
// public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
// SimpleTriggerFactoryBean factory=new SimpleTriggerFactoryBean();
// //关联JobDetail对象
// factory.setJobDetail(jobDetailFactoryBean.getObject());
// //该参数表示一个执行的毫秒数
// factory.setRepeatInterval(2000);
// //重复次数
// factory.setRepeatCount(5);
// return factory;
// }
/**
* Cron Trigger
*/
@Bean
public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) {
CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
factory.setJobDetail(jobDetailFactoryBean.getObject());
//设置触发时间
factory.setCronExpression("0/2 * * * * ?");
return factory;
}
/**
* 3.创建Scheduler对象
*/
//
@Bean
public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean) {
//public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean){
SchedulerFactoryBean factory = new SchedulerFactoryBean();
//关联trigger
// factory.setTriggers(simpleTriggerFactoryBean.getObject());
factory.setTriggers(cronTriggerFactoryBean.getObject());
return factory;
}
}
启动类
添加@EnableScheduling
job对象注入
直接在job接口实现类中使用 对象的自动注入会报错,主要是因为job接口实现类并不在Spring容器中,无法进行依赖注入,其主要使用的反射方法进行操作所以我们需要手动将其注入到spring容器中主要实现一个继承自AdaptableJobFactory的类并重写其createJobInstance(TriggerFiredBundle bundle) 方法,具体如下:
@Component("myAdaptableJobFactory")
public class MyAdaptableJobFactory extends AdaptableJobFactory {
/**
* AutowireCapableBeanFactory 可以将一个对象添加到springIOC容器中,并且完成该对象注入
*/
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
/**
* 该方法需要将实例化的任务对象手动的添加到springIOC容器中并且完成对象的注入
* @param bundle
* @return
* @throws Exception
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object obj=super.createJobInstance(bundle);
//将obj对象添加到Spring IOC 容器中,并完成注入
this.autowireCapableBeanFactory.autowireBean(obj);
return obj;
}
}
之后修改QuartzConfig
为schedulerFactoryBean方法增加参数MyAdaptableJobFactory ,及手动添加注入的命令 factory.setJobFactory(myAdaptableJobFactory);即可实现job实现类中对象的自动注入。
@Bean
public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean,MyAdaptableJobFactory myAdaptableJobFactory) {
//public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean){
SchedulerFactoryBean factory = new SchedulerFactoryBean();
//关联trigger
// factory.setTriggers(simpleTriggerFactoryBean.getObject());
factory.setTriggers(cronTriggerFactoryBean.getObject());
//注入添加
factory.setJobFactory(myAdaptableJobFactory);
return factory;
}