SpringBoot中使用动态定时任务+CRON+多线程(线程池)

本文介绍了如何解决SpringBoot中定时任务单线程导致的执行延迟问题,通过重写SchedulingConfigurer接口的configureTasks方法,利用线程池实现多线程并行执行定时任务。此外,还涉及到启动类中的定时任务注解配置、相关依赖、定时任务定义、数据库表结构以及VO类的设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       问题: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='自动任务表';

 VO类

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 + '}';
    }
}

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值