之前学习过spring的定时任务 :https://www.cnblogs.com/slimshady/p/10112515.html
本文主要学习记录下springboot使用quartz
1. pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.quartz-scheduler</groupId> <!-- Quartz -->
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <!-- lombok @Slf4j注解用于日志输出-->
<version>1.16.22</version>
<scope>provided</scope>
</dependency>
2. 代码结构
3. SimpleTriggerJob.java
package com.example.service; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import java.text.SimpleDateFormat;
import java.util.Date; /**
* 任务一 使用simpleTrigger
* Created by Administrator on 2018/12/20.
*/
@Component
@Slf4j
public class SimpleTriggerJob {
public void printMessage() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ");
log.info("SimpleTriggerFactoryBean任务在"+sdf.format(new Date())+"执行......");
}
}
4.CronTriggerJob.java
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import java.text.SimpleDateFormat;
import java.util.Date; /**
* 任务二 使用cronTrigger * Created by Administrator on 2018/12/20.
*/
@Component("jobCornBean")
@Slf4j
public class CronTriggerJob {
public void printAnotherMessage(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ");
log.info("CronTriggerFactoryBean任务在"+sdf.format(new Date())+"执行.....");
}
}
5. JobConfig.java
import com.example.bean.JobConfigBean;
import com.example.service.CronTriggerJob;
import com.example.service.ScheduledJob;
import com.example.service.SimpleTriggerJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.quartz.*; import java.util.ArrayList;
import java.util.List; /**
* Created by Administrator on 2018/12/20.
*/
@Configuration
public class JobConfig { @Autowired
private SimpleTriggerJob jobBean; @Autowired
private ScheduledJob scheduledJob; @Autowired
private CronTriggerJob jobCornBean; /**
* 配置调度器第一种方式
* @return
*/
@Bean("methodInvokingJobDetailFactoryBean")
public MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean(){
MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
bean.setTargetObject(jobBean);
bean.setTargetMethod("printMessage");
bean.setConcurrent(false);
return bean;
} /**
* 配置调度器第二种方式 可以传递额外的参数给定时job
* @return
*/
@Bean("jobDetailFactoryBean")
public JobDetailFactoryBean jobDetailFactoryBean(){
JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
jobDetailFactoryBean.setJobClass(scheduledJob.getClass());
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("jobCornBean",jobCornBean);
jobDetailFactoryBean.setJobDataMap(jobDataMap);
jobDetailFactoryBean.setDurability(true);
return jobDetailFactoryBean;
} /**
* 触发器 simpleTriggerFactoryBean
* @param methodInvokingJobDetailFactoryBean
* @return
*/
@Bean("simpleTriggerFactoryBean")
public SimpleTriggerFactoryBean simpleTriggerFactoryBean(MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean){
SimpleTriggerFactoryBean simpleTriggerFactoryBean = new SimpleTriggerFactoryBean();
simpleTriggerFactoryBean.setJobDetail(methodInvokingJobDetailFactoryBean.getObject());
simpleTriggerFactoryBean.setStartDelay(1000);
simpleTriggerFactoryBean.setRepeatInterval(1000);//每秒执行一次 return simpleTriggerFactoryBean;
} /**
* 触发器 cronTriggerFactoryBean
* @param jobDetailFactoryBean
* @return
*/
@Bean("cronTriggerFactoryBean")
public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
cronTriggerFactoryBean.setCronExpression("0/5 * * ? * *");//每5秒执行一次
return cronTriggerFactoryBean;
} /**
* 将上面的jobDetails和triggers都配置到一个类中,这样便于后续定时任务的添加
* @return
*/
@Bean("jobConfigBean")
public JobConfigBean jobConfigBean(){
JobConfigBean jobConfigBean = new JobConfigBean();
List<JobDetail> jobDetails = new ArrayList<>();
jobDetails.add(methodInvokingJobDetailFactoryBean().getObject());
jobDetails.add(jobDetailFactoryBean().getObject()); JobDetail[] jobDetailarr = createJobDetail(jobDetails); List<Trigger> triggers = new ArrayList<>();
triggers.add(simpleTriggerFactoryBean(methodInvokingJobDetailFactoryBean()).getObject());
triggers.add(cronTriggerFactoryBean(jobDetailFactoryBean()).getObject()); Trigger[] triggerarr = createTriggers(triggers); jobConfigBean.setJobDetails(jobDetailarr);
jobConfigBean.setTriggers(triggerarr); return jobConfigBean;
} @Bean
public SchedulerFactoryBean schedulerFactoryBean(JobConfigBean jobConfigBean){ //一个定时任务
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setJobDetails(jobConfigBean.getJobDetails());
schedulerFactoryBean.setTriggers(jobConfigBean.getTriggers()); return schedulerFactoryBean;
} public static JobDetail[] createJobDetail(List<JobDetail> jobDetails){
JobDetail[] jobDetailArr = new JobDetail[jobDetails.size()];
for (int i = 0; i < jobDetails.size(); i++) {
jobDetailArr[i] = jobDetails.get(i);
} return jobDetailArr; } public static Trigger[] createTriggers(List<Trigger> triggers){
Trigger[] triggerarr = new Trigger[triggers.size()]; for (int i = 0; i <triggers.size() ; i++) {
triggerarr[i] = triggers.get(i);
} return triggerarr;
}
}
6. JobConfigBean.java
import org.quartz.JobDetail;
import org.quartz.Trigger; public class JobConfigBean { private JobDetail[] jobDetails; private Trigger[] triggers; public JobDetail[] getJobDetails() {
return jobDetails;
} public void setJobDetails(JobDetail[] jobDetails) {
this.jobDetails = jobDetails;
} public Trigger[] getTriggers() {
return triggers;
} public void setTriggers(Trigger[] triggers) {
this.triggers = triggers;
}
}
7. ScheduledJob.java
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component; @Component
public class ScheduledJob extends QuartzJobBean { private CronTriggerJob jobCornBean; @Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
jobCornBean.printAnotherMessage();
} public void setJobCornBean(CronTriggerJob jobCornBean) {
this.jobCornBean = jobCornBean;
}
}
8.启动springboot 执行结果如下: