主要设计以下元素:
其中trigger和job是任务调度的元数据,scheduler是实际执行调度的控制器。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
package com.example.demo.job;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
@Slf4j
public class BusinessJob implements Job {
int i = 0;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();
String time = dataMap.get("time").toString();
business(time);
}
public void business(String time) {
i++;
log.info("time:{},threadName:{},i:{}", time, Thread.currentThread().getName(), i);
}
}
package com.example.demo.job;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class JobService {
int i = 0;
public void business() {
i++;
log.info("定时任务,业务方法执行,thread:{},i:{}", Thread.currentThread().getName(), i);
}
}
package com.example.demo.config;
import com.example.demo.job.BusinessJob;
import com.example.demo.job.JobService;
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.quartz.*;
import java.time.LocalDateTime;
@Configuration
public class QuartzConfig {
//配置任务,多例的业务bean,耦合业务类,需要实现Job接口
@Bean(name = "businessJobDetail")
public JobDetailFactoryBean businessJobDetail() {
LocalDateTime localDateTime = LocalDateTime.now();
JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
jobDetailFactoryBean.setJobClass(BusinessJob.class);
//将参数传给job
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("time", localDateTime);
jobDetailFactoryBean.setJobDataAsMap(jobDataMap);
return jobDetailFactoryBean;
}
//配置任务,单例的业务bean
@Bean(name = "jobServiceBeanDetail")
public MethodInvokingJobDetailFactoryBean jobServiceBeanDetail(JobService jobService) {
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
//是否并发执行
jobDetail.setConcurrent(false);
//需要执行的实体bean
jobDetail.setTargetObject(jobService);
//需要执行的方法
jobDetail.setTargetMethod("business");
return jobDetail;
}
//配置简单触发器
@Bean(name = "simpleTrigger")
public SimpleTriggerFactoryBean simpleTrigger(JobDetail businessJobDetail) {//businessJobDetail
SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
trigger.setJobDetail(businessJobDetail);
//设置启动延迟
trigger.setStartDelay(0);
//每隔5s执行一次
trigger.setRepeatInterval(5000);
return trigger;
}
//配置cron触发器
@Bean(name = "cronTrigger")
public CronTriggerFactoryBean cronTrigger(JobDetail jobServiceBeanDetail) {//目标任务jobServiceBeanDetail
CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
triggerFactoryBean.setJobDetail(jobServiceBeanDetail);
//每隔6s执行一次
triggerFactoryBean.setCronExpression("0/6 * * * * ?");
return triggerFactoryBean;
}
//配置调用工厂,将所有的触发器引入
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactory(Trigger cronTrigger, Trigger simpleTrigger) {//需要管理的触发器cronTrigger,simpleTrigger
SchedulerFactoryBean bean = new SchedulerFactoryBean();
//延迟1s启动
bean.setStartupDelay(1);
//注册触发器,可以注册多个
bean.setTriggers(cronTrigger, simpleTrigger);
return bean;
}
}
package com.example.demo.schedule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
@EnableScheduling
public class TaskService {
//每隔1s执行一次
@Scheduled(fixedRate = 1000)
public void fixMethod() {
try {
log.info("fixMethod,thread:{}",Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//前一个任务执行完5s后执行
@Scheduled(fixedDelay = 5000)
public void delayMethod() {
log.info("delayMethod,thread:{}",Thread.currentThread().getName());
}
//每隔10s执行一次
@Scheduled(cron = "0/10 * * * * ?")
public void cronMethod() {
log.info("cronMethod,thread:{}",Thread.currentThread().getName());
}
}
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}
//配置线程池--触发器和任务共用
@Bean
public Executor taskExecutor(){
return Executors.newScheduledThreadPool(10);
}
}
https://gitee.com/fisher3652/exportDemo