赞
踩
问题:springboot中的自动任务是单线程的,即同时有多个定时任务的时候,如果任务1的执行时间过长(执行时间超过任务周期),会影响到任务2的执行。
我们需要重写SchedulingConfigurer接口的configureTasks方法,在线程池中开启多个线程。
- import org.springframework.context.annotation.Configuration;
- import org.springframework.scheduling.annotation.SchedulingConfigurer;
- import org.springframework.scheduling.config.ScheduledTaskRegistrar;
-
- import java.util.concurrent.Executors;
-
- /**
- * 多线程执行定时任务
- *
- * @author xx
- */
- @Configuration
- //所有的定时任务都放在一个线程池中,定时任务启动时使用不同都线程。
- public class ScheduleConfig implements SchedulingConfigurer {
-
- @Override
- public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
- //设定一个长度10的定时任务线程池,根据需要自己设置长度
- taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
- }
- }

@EnableScheduling // 开启定时任务功能
- //这个依赖是判断cron表达式是否合法时用
- <dependency>
- <groupId>org.quartz-scheduler</groupId>
- <artifactId>quartz</artifactId>
- <version>2.2.1</version>
- <type>jar</type>
- </dependency>
然后一个定时任务要写一个类,继承ScheduleConfig 类。
- import org.quartz.CronExpression;
- import org.springframework.scheduling.config.ScheduledTaskRegistrar;
- import org.springframework.scheduling.support.CronTrigger;
- import org.springframework.stereotype.Component;
- import org.springframework.beans.factory.annotation.Autowired;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
-
- /**
- * 测试定时任务的类
- *
- * @author xx
- */
- @Component
- public class Sheduler extends ScheduleConfig {
-
- // autoTask存的是数据库中 存储的定时任务,字段描述见《数据库表结构》
- List<AutoTaskVO> autoTask = new ArrayList();
- String taskid = "test";//定时任务ID,
- String cron = "";
-
- private void schedulerMethod() {
- // 这里调用需要周期执行的定时任务代码,字段描述见《数据库表结构》
- System.out.println("线程名1111:" + Thread.currentThread().getName() + "====" + SimpleDateFormat.getDateTimeInstance().format(new Date()));
- }
-
- //每次执行完定时任务,都会重新执行一次configureTasks方法,根据参数重新设定定时器的执行时间
- @Override
- public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
- taskRegistrar.addTriggerTask(
- //1.添加任务内容(Runnable)
- () -> {
- if ("".equals(cron) || autoTask.isEmpty() || (!autoTask.isEmpty() && "0".equals(autoTask.get(0).getType()))) {
- System.out.println(taskid + ":任务不执行。");
- return;
- }
-
- System.out.println(taskid + ":开始执行任务。");
- System.out.println(taskid + ":" + Thread.currentThread().getName());
- schedulerMethod();
- System.out.println(taskid + ":执行任务结束。");
- },
- //2.设置执行周期(Trigger)
- triggerContext -> {
- System.out.println(taskid + ":获取任务执行周期!");
- //2.1 从数据库获取执行周期(对应的cron属性)
- // 根据taskid获取任务的cron表达式(cron)和是否执行的状态(type)
- // 字段描述见《数据库表结构》
- autoTask = CornTaskImpl.getCronByTaskid(taskid);
- if (autoTask.isEmpty()) {
- System.out.println(taskid + ":cron参数为空,设置默认参数,每分钟扫描一次。");
- //如果为空则赋默认值,每一分钟执行一次
- cron = "0 0/1 * * * ?";
- } else {
- cron = autoTask.get(0).getCron();
- // 这个判断cron表达式是否合法
- if (!CronExpression.isValidExpression(cron)) {
- System.out.println(taskid + ":cron参数不合法,设置默认参数,每分钟扫描一次。");
- //如果不合法则赋默认值,每一分钟执行一次
- cron = "0 0/1 * * * ?";
- }
- }
- //2.3 返回执行周期(Date)
- return new CronTrigger(cron).nextExecutionTime(triggerContext);
- }
- );
- }
- }

- CREATE TABLE `vp_auto_task` (
- `TASKID` varchar(50) COLLATE utf8_bin NOT NULL COMMENT '任务ID',
- `CRON` varchar(50) COLLATE utf8_bin NOT NULL COMMENT 'cron表达式',
- `TYPE` varchar(1) COLLATE utf8_bin NOT NULL DEFAULT '0' COMMENT '是否启用(1:启用;0:不启用)',
- `REMARK` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '备注',
- PRIMARY KEY (`TASKID`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='自动任务表';
- import java.io.Serializable;
-
- /**
- *
- * @author xx
- */
- public class AutoTaskVO implements Serializable {
-
- private String taskid;//任务ID
- private String cron;//cron表达式
- private String type;//是否启用(1:启用;0:不启用)
- private String remark;//备注
-
- public String getTaskid() {
- return taskid;
- }
-
- public void setTaskid(String taskid) {
- this.taskid = taskid;
- }
-
- public String getCron() {
- return cron;
- }
-
- public void setCron(String cron) {
- this.cron = cron;
- }
-
- public String getType() {
- return type;
- }
-
- public void setType(String type) {
- this.type = type;
- }
-
- public String getRemark() {
- return remark;
- }
-
- public void setRemark(String remark) {
- this.remark = remark;
- }
-
- @Override
- public String toString() {
- return "AutoTask{" + "taskid=" + taskid + ", cron=" + cron + ", type=" + type + ", remark=" + remark + '}';
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。