所有分类
  • 所有分类
  • 未分类

Spring定时任务–手动执行定时任务(替代@Scheduled)

简介

本文介绍SpringBoot如何手动执行定时任务。

之前此文已经介绍过,直接用@Scheduled即可使用Spring的定时任务,但有时需要手动去提交定时任务,比如:

  1. 从其他位置获取cron配置,无法写到注解。
  2. 有不确定个数的任务,不能在代码中写死。

方案1:实现 SchedulingConfigurer 接口

本处从数据库读取配置,然后提交定时任务。

创建定时器

编写定时任务,这里添加的是TriggerTask,循环读取我们在数据库设置好的执行周期,以及执行相关定时任务的内容。

@Configuration
@EnableScheduling
public class DynamicScheduleTask implements SchedulingConfigurer {
    @Autowired
    private CronMapper cronMapper;
    
    /**
     * 执行定时任务.
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

        taskRegistrar.addTriggerTask(
                //1.添加任务内容(Runnable)
                () -> System.out.println("执行动态定时任务: " + LocalDateTime.now().toLocalTime()),

                //2.设置执行周期(Trigger)
                triggerContext -> {
                    //2.1 从数据库获取执行周期
                    String cron = cronMapper.getCron();
                    //2.2 合法性校验.
                    if (StringUtils.isEmpty(cron)) {
                        // Omitted Code ..
                    }
                    //2.3 返回执行周期(Date)
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                }
        );
    }

}

表结构与数据

开启本地数据库mysql,随便打开查询窗口,然后执行脚本内容,如下

DROP DATABASE IF EXISTS `socks`;
CREATE DATABASE `socks`;
USE `SOCKS`;
DROP TABLE IF EXISTS `cron`;
CREATE TABLE `cron`  (
  `cron_id` varchar(30) NOT NULL PRIMARY KEY,
  `cron` varchar(30) NOT NULL  
);
INSERT INTO `cron` VALUES ('1', '0/5 * * * * ?');

项目中的application.yml 添加数据源

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/socks
    username: root
    password: 123456

启动测试

启动应用后,查看控制台,打印时间是我们预期的每10秒一次:

打开Navicat ,将执行周期修改为每6秒执行一次,如图

如果在数据库修改时格式出现错误,则定时任务会停止,即使重新修改正确;此时只能重新启动项目才能恢复。

方案2:ThreadPoolTaskScheduler

上边的实例的定时任务在SprinBoot在启动时就启动,也可以在代码中任意启动/关闭/调整时间。如下:

package com.example.demo.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.ScheduledFuture;

@RestController
@Component
public class TestScheduler {
    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    private ScheduledFuture<?> future;

    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        return new ThreadPoolTaskScheduler();
    }

    @RequestMapping("/startCron")
    public String startCron() {

        future = threadPoolTaskScheduler.schedule(new MyRunnable(), new CronTrigger("0/5 * * * * *"));
        System.out.println("DynamicTask.startCron()");
        return "startCron";
    }

    @RequestMapping("/stopCron")
    public String stopCron() {

        if (future != null) {
            future.cancel(true);
        }
        System.out.println("DynamicTask.stopCron()");
        return "stopCron";
    }

    @RequestMapping("/changeCron10")
    public String startCron10() {

        stopCron();// 先停止,在开启.
        future = threadPoolTaskScheduler.schedule(new MyRunnable(), new CronTrigger("*/10 * * * * *"));
        System.out.println("DynamicTask.startCron10()");
        return "changeCron10";
    }

    private class MyRunnable implements Runnable {

        @Override
        public void run() {
            System.out.println("DynamicTask.MyRunnable.run()," + new Date());
        }
    }
}

浏览器分别输入:http://localhost:8080/startCronhttp://localhost:8080/stopCron 

执行结果

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.0.RELEASE)

2020-05-27 19:48:02.370  INFO 6804 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on liu-PC with PID 6804 (E:\work\idea_proj\test-SpringBoot\target\classes started by liu in E:\work\idea_proj\test-SpringBoot)
2020-05-27 19:48:02.373  INFO 6804 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-05-27 19:48:03.675  INFO 6804 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-05-27 19:48:03.684  INFO 6804 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-05-27 19:48:03.684  INFO 6804 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.35]
2020-05-27 19:48:03.941  INFO 6804 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-05-27 19:48:03.941  INFO 6804 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1523 ms
2020-05-27 19:48:04.001  INFO 6804 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'threadPoolTaskScheduler'
2020-05-27 19:48:04.405  INFO 6804 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-27 19:48:04.419  INFO 6804 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 3.328 seconds (JVM running for 8.111)
2020-05-27 19:48:39.380  INFO 6804 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-05-27 19:48:39.380  INFO 6804 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-05-27 19:48:39.405  INFO 6804 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 25 ms
DynamicTask.startCron()
DynamicTask.MyRunnable.run(),Wed May 27 19:48:50 CST 2020
DynamicTask.MyRunnable.run(),Wed May 27 19:48:55 CST 2020
DynamicTask.MyRunnable.run(),Wed May 27 19:49:00 CST 2020
DynamicTask.MyRunnable.run(),Wed May 27 19:49:05 CST 2020
DynamicTask.MyRunnable.run(),Wed May 27 19:49:10 CST 2020
DynamicTask.stopCron()

0

评论0

请先

显示验证码
没有账号?注册  忘记密码?

社交账号快速登录