quartz配置类
package com.examplequartz.demoquartz.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
@Configuration
public class QuartzConfig {
@Autowired
private MyAdaptableJobFactory myAdaptableJobFactory;
/**
* 1.创建job对象 做什么
*/
@Bean
public JobDetailFactoryBean jobDetailFactoryBean() {
JobDetailFactoryBean factory = new JobDetailFactoryBean();
//关联自己的job类
factory.setJobClass(DemoJob.class);
return factory;
}
/**
* 2.创建Trigger对象 什么时候做
* <p>
* 简单Trigger
*/
@Bean
public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) {
SimpleTriggerFactoryBean factory = new SimpleTriggerFactoryBean();
//关联JobDetail对象
factory.setJobDetail(jobDetailFactoryBean.getObject());
//设置触发时间 该参数表示毫秒,2000表示2秒
factory.setRepeatInterval(5);
//设置重复次数
factory.setRepeatCount(1000);
return factory;
}
/**
* Cron Trigger
*/
@Bean
public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
//关联JobDetail
factory.setJobDetail(jobDetailFactoryBean.getObject());
// 秒 分钟 小时 天 月 星期
factory.setCronExpression("10,15,20 34 11 * * ?");
//表示每年每月每天的11点34分的10,15,20秒都会执行一次
return factory;
}
/**
* 3.创建Scheduler对象
*/
@Bean
public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean) {
SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
//关联trigger
factoryBean.setTriggers(simpleTriggerFactoryBean.getObject());
factoryBean.setJobFactory(myAdaptableJobFactory);
return factoryBean;
}
}
如果自己写的一个类作为job类执行需要继承AdaptableJobFactory 重写createJobInstance方法,否则spring容器无法加载我们的job
package com.examplequartz.demoquartz.service;
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.Service;
@Service
public class MyAdaptableJobFactory extends AdaptableJobFactory {
//AutowireCapableBeanFactory 可以将一个对象添加到Spring的IOC容器中,并且完成该对象的注入
@Autowired
private AutowireCapableBeanFactory autowiredCaptableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
autowiredCaptableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
job的执行
package com.examplequartz.demoquartz.service;
import com.alibaba.fastjson.JSON;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class DemoJob implements Job {
//UserJob 是我们具体需要实现的job任务,这里加载进来
@Autowired
private UserJob userJob;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println(JSON.toJSONString(context.getJobDetail()));
userJob.userJob();
}
}
自定义job类
package com.examplequartz.demoquartz.service;
import org.springframework.stereotype.Service;
@Service
public class UserJob {
public void userJob(){
System.out.println("Start:"+Thread.currentThread().getName());
for(int i=0;i<10;i++){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}//{ System.out.println(Thread.currentThread().getName());}
System.out.println("End:"+Thread.currentThread().getName());
}
}