参考文章: springboot自带定时器实现定时任务的开启关闭以及定时时间可以配置

一. 序言

最近项目需要用到定时任务,需要完成一个定时功能。经过了解,项目中目前实现定时任务,一般有三种选择:

  • 一是用Java自带的timer类。稍微看了一下,可以实现大部分的指定频率的任务的调度(timer.schedule()),也可以实现关闭和开启(timer.cancle)。但是用其来实现某天的某个时间或者某月的某一天调度任务有点不方便。
  • 二是采用Quartz 调度器实现。这是一个功能很强大的开源的专门用于定时任务调度的框架,也很好的和springboot整合,缺点:配置复杂,需要花费一定的时间去了解和研究。
  • 三是spring3.0以后自带的scheduletask任务调度,可以实现quartz的大部分功能,不需要额外引用jar,也不需要另外配置。而且支持注解和配置文件两种。

我选择的是最直接简单的第三种。 下面简单介绍下springboot自带的定时器来实现定时任务

二. 开关任务代码

1. controller层代码

package com.left.controller;
import com.left.Result;
import com.left.runnable.MyRunnable1;
import com.left.runnable.MyRunnable2;
import com.left.util.ResultUtils;
import com.left.utils.YouXinConfiguration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.concurrent.ScheduledFuture;
@Slf4j
@RestController
@Api(description = "定时任务")
@RequestMapping("/quartz/task")
public class DynamicTaskController {
    @Autowired
    private YouXinConfiguration youXinConfiguration;
    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;
    private ScheduledFuture<?> future1;
    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        return new ThreadPoolTaskScheduler();
    @PostMapping("/startCron1")
    @ApiOperation("开始定时任务1")
    public Result startCron1() {
        future1 = threadPoolTaskScheduler.schedule(new MyRunnable1(),new Trigger(){
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext){
                return new CronTrigger(youXinConfiguration.getCorn1()).nextExecutionTime(triggerContext);
        System.out.println("DynamicTask.startCron1()");
        return ResultUtils.success();
    @PostMapping("/stopCron1")
    @ApiOperation("关闭定时任务1")
    public Result stopCron1() {
        if (future1 != null) {
            future1.cancel(true);
        System.out.println("DynamicTask.stopCron1()");
        return ResultUtils.success();

代码中所引用类的介绍:

  • ThreadPoolTaskScheduler:线程池任务调度类,能够开启线程池进行任务调度。
  • ThreadPoolTaskScheduler.schedule()方法会创建一个定时计划ScheduledFuture,在这个方法需要添加两个参数,Runnable(线程接口类) 和CronTrigger(定时任务触发器)
  • YouXinConfiguration:自定义读取yml文件中数据的类,通过该类读取yml文件中cron时间表达式,从而可以达到定时时间可配置的效果。
  • MyRunnable1与MyRunnable2类:这两个类都是实现了Runnable接口,重写了run方法,定时任务的逻辑代码就是在其中实现。

2. YouXinConfiguration类代码

package com.left.utils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "youxing")
@EqualsAndHashCode(callSuper = false)
public class YouXinConfiguration {
    private String corn1;
    private String corn2;

3. yml文件(定时时间的配置的在最下面的youxing字段)

server:
  port: 8002
spring:
    application:
      name: dubbo-client-orange
youxing:
  corn1: 0/10 * * * * ?
  corn2: 0/5 * * * * ?

4. MyRunnable1与MyRunnable2基本一样

// ============== MyRunnable1 =============== //
package com.left.runnable;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyRunnable1 implements Runnable {
    @Override
    public void run() {
        System.out.println("first DynamicTask,"
                + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
// ============== MyRunnable2 =============== //
package com.left.runnable;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyRunnable2 implements Runnable {
    @Override
    public void run() {
        System.out.println("second DynamicTask,"
                + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

5. 启动SpringBoot项目,访问 http://localhost:8080/swagger-ui.html

6. 控制台打印效果

参考文章:springboot自带定时器实现定时任务的开启关闭以及定时时间可以配置一. 序言最近项目需要用到定时任务,需要完成一个定时功能。经过了解,项目中目前实现定时任务,一般有三种选择:一是用Java自带的timer类。稍微看了一下,可以实现大部分的指定频率的任务的调度(timer.schedule()),也可以实现关闭和开启(timer.cancle)。但是用其来实现某天的某个时...
定时任务是开发中比较常见的功能之一,如定时统计订单数、数据库备份、定时统计博客访客等,简单的定时任务可以直接通过Spring中的@Scheduled注解来实现,复杂的定时任务则可以通过集成Quartz来实现。 Scheduled @Scheduled是由Spring提供的定时任务注解,使用方便,配置简单,可以解决工作中大部分的定时任务需求。使用方式如下: (1)、创建工程 首先创建一个普通的Spring Boot Web工程,添加Web依赖即可。 (2)、开启定时任务项目启动中添加@EnableSche
一、序言: 最近项目需要用到定时任务,需要完成一个定时功能。经过了解,项目中目前实现定时任务,一般有三种选择,一是用Java自带的timer类。稍微看了一下,可以实现大部分的指定频率的任务的调度(timer.schedule()),也可以实现关闭开启(timer.cancle)。但是用其来实现某天的某个时间或者某月的某一天调度任务有点不方便。 二是采用Quartz 调度器实现。这是一个功能很...
312-用定时器T0的中断实现时间定时(51单片机C语言实例Proteus仿真和代码)312-用定时器T0的中断实现时间定时(51单片机C语言实例Proteus仿真和代码)312-用定时器T0的中断实现时间定时(51单片机C语言实例Proteus仿真和代码)312-用定时器T0的中断实现时间定时(51单片机C语言实例Proteus仿真和代码)312-用定时器T0的中断实现时间定时(51单片机C语言实例Proteus仿真和代码)312-用定时器T0的中断实现时间定时(51单片机C语言实例Proteus仿真和代码)312-用定时器T0的中断实现时间定时(51单片机C语言实例Proteus仿真和代码)312-用定时器T0的中断实现时间定时(51单片机C语言实例Proteus仿真和代码)312-用定时器T0的中断实现时间定时(51单片机C语言实例Proteus仿真和代码)312-用定时器T0的中断实现时间定时(51单片机C语言实例Proteus仿真和代码)312-用定时器T0的中断实现时间定时(51单片机C语言实例Proteus仿真和代码)312-用定时器T0的中断实现
使用SpringBoot定时任务注解@Scheduled,编写定时任务代码,其中包括任务执行方法、cron表达式等配置。 3. 将定时任务信息保存到数据库 在定时任务执行前,将定时任务的相关信息保存到数据库中,包括任务名称、任务组名、任务类名、执行方法名、cron表达式等信息。 4. 动态添加/修改定时任务 通过Mybatis-plus提供的动态SQL,实现动态添加/修改定时任务的功能。在添加/修改定时任务时,需要更新定时任务表,并重新加载定时任务。 5. 动态删除定时任务 通过Mybatis-plus提供的动态SQL,实现动态删除定时任务的功能。在删除定时任务时,需要同时从定时任务表中删除定时任务,并停止正在执行的定时任务。 总体来说,基于Mybatis-plus实现定时器任务管理是一种可行的方案,可以通过数据库配置和动态SQL实现定时任务的添加、修改、删除等功能,同时也方便管理和维护定时任务