项目目录
pom文件引入的依赖
springboot2.0之后集成了quartz
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
自定义工厂类
@Component
public class SchedulerFactory extends AdaptableJobFactory {
//spring bean 对象管理工厂
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
//调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
//自动注入
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
定义quartz配置
使用自己的quartz.properties文件,当然也可以使用默认配置
org.quartz.scheduler.instanceName = MyScheduler // 调度器名字
org.quartz.threadPool.threadCount = 10 //配置线程池大小
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore // 使用内存保存
配置类
@Configuration
public class QuartzConfiguration {
// 注入service需要配置工厂类
@Autowired
private SchedulerFactory schedulerFactory;
@Bean(name="SchedulerFactory")
public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
// 注入service需要配置工厂类
factory.setJobFactory(schedulerFactory);
// 注入service需要配置工厂类
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
// 注释下面这行代码将使用默认的配置
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
//在quartz.properties中的属性被读取并注入后再初始化对象
propertiesFactoryBean.afterPropertiesSet();
Properties properties = propertiesFactoryBean.getObject();
factory.setQuartzProperties(properties);
return factory;
}
/*
* quartz初始化监听器
*/
@Bean
public QuartzInitializerListener executorListener() {
return new QuartzInitializerListener();
}
/*
* 通过SchedulerFactoryBean获取Scheduler的实例
*/
@Bean(name = "Scheduler")
public Scheduler scheduler() throws IOException {
return schedulerFactoryBean().getScheduler();
}
}
编写工作类实现Job类
public class SampleJob implements Job {
@Autowired
@Qualifier(value = "Scheduler")
private Scheduler scheduler;
public SampleJob() {
}
@Override
public void execute(JobExecutionContext context) {
String id = context.getJobDetail().getJobDataMap().get("jobname").toString();
id = id + ">";
System.out.println("执行方法。。。。。" + id);
context.getJobDetail().getJobDataMap().put("jobname", id);
try {
scheduler.addJob(context.getJobDetail(), true, true);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
编写任务添加接口
@Autowired
@Qualifier(value = "Scheduler")
private Scheduler scheduler;
//创建一个新的job任务
@GetMapping("/add/{jobName}")
public void addJob(@PathVariable(value = "jobName") String jobName) {
String groupName = "group_one"; //定义job所在组名称
String cronExpression = "0 * * * * ? *";//执行时间表达式
try {
//构建一个新的任务规范,执行特定任务,任务执行的时间
JobDetail jobDetail = JobBuilder.newJob(SampleJob.class).withIdentity(jobName, groupName)
.usingJobData("jobSays", "Hello World!")
.build();
//执行的任务中传入参数
jobDetail.getJobDataMap().put("jobname", jobName);
//创建corn表达式,创建执行任务的时间规范
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
//创建一个触发器,加入上面创建的时间规范
CronTrigger trigger =
TriggerBuilder.newTrigger().withIdentity(jobName, groupName).withSchedule(scheduleBuilder)
.build();
//把执行的job任务和创建时间trigger绑定一起
scheduler.scheduleJob(jobDetail, trigger);
//启动任务调度器,准备添加任务相关信息操作
scheduler.start();
} catch (SchedulerException e) {
LOGGER.info("scheduler start or shutdown error ...");
}
}
源码获取
gitee仓库地址:https://gitee.com/Junior-L/quartz.git
评论区