定时任务 quartz 组件实战使用分析 今天分享,这是一款比较老的定时任务框架,目前市场上流行的其他定时任务框架大多是基于此组件演变而来的。
1、springBoot框架引入pom相关jar
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
2、相关配置
package com.quartz.config;
import com.quartz.job.BusinessJob;
import com.quartz.job.XXXService;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.quartz.*;
@Configuration
public class QuartzConfig {
/**
* 耦合业务
* 将调度环境信息传递给业务方法,如:调度时间,批次等
* @return
*/
@Bean(name = "businessJobDetail")
public JobDetailFactoryBean businessJobDetail() {
JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
jobDetailFactoryBean.setJobClass(BusinessJob.class);//业务bean是多例的还是单例的?
//将参数封装传递给job
JobDataMap jobDataMap =new JobDataMap();
jobDataMap.put("time",System.currentTimeMillis());
jobDetailFactoryBean.setJobDataAsMap(jobDataMap);
return jobDetailFactoryBean;
}
/**
* 普通业务类
* @param serviceBean
* @return
*/
@Bean(name = "serviceBeanDetail")
public MethodInvokingJobDetailFactoryBean serviceBeanDetail(XXXService serviceBean) {//业务bean,单例的
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
// 是否并发执行
jobDetail.setConcurrent(false);
// 需要执行的实体bean
jobDetail.setTargetObject(serviceBean);
// 需要执行的方法
jobDetail.setTargetMethod("business");
return jobDetail;
}
// 简单触发器
@Bean(name = "simpleTrigger")
public SimpleTriggerFactoryBean simpleTrigger(JobDetail businessJobDetail) {
SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
trigger.setJobDetail(businessJobDetail);
// 设置任务启动延迟
trigger.setStartDelay(0);
// 每5秒执行一次
trigger.setRepeatInterval(3000);
return trigger;
}
//cron触发器
@Bean(name = "cronTrigger")
public CronTriggerFactoryBean cronTrigger(JobDetail serviceBeanDetail) {
CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
triggerFactoryBean.setJobDetail(serviceBeanDetail);
triggerFactoryBean.setCronExpression("0/6 * * * * ?");
return triggerFactoryBean;
}
/**
* 调度工厂,将所有的触发器引入
* @return
*/
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactory(Trigger simpleTrigger, Trigger cronTrigger) {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
// 延时启动,应用启动1秒后
bean.setStartupDelay(1);
// 注册触发器
bean.setTriggers(simpleTrigger,cronTrigger);
return bean;
}
}
3、Job配置
package com.quartz.job;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class BusinessJob implements Job{
int i = 0;
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String name = dataMap.get("time").toString();
business(name);
}
//重型任务,1000W数据统计,把任务敲碎 -- E-job
private void business(String time){
//
i++;
System.out.println("BusinessJob start --- time:"+time+", thread:" + Thread.currentThread().getName() );
}
}
4、业务伪代码
package com.quartz.job;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class XXXService {
public void business(){
System.out.println("业务方法执行...." + ",线程号:"+Thread.currentThread().getName());
}
}
5、简单启动类
package com.quartz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class QuartzApp {
public static void main(String[] args) {
SpringApplication.run(QuartzApp.class,args);
}
}
启动后直接执行相关定时任务,操作也是比较简单的,也是比较原始的。如果你要是想研究定时任务的源码,我建议你研究quartz 组件的源码,你会收获很多的。
到此,该定时任务分析完毕。