SpringBoot基于数据库的定时任务统一管理的实现

定时任务1

import lombok.extern.slf4j.Slf4j;

/**
 * @author Created by niugang on 2019/12/24/15:29
 */
@Slf4j
public class TaskTest {

  public void task1() {
    log.info("反射调用测试[一]类");
  }
}

定时任务2

import lombok.extern.slf4j.Slf4j;

/**
 * @author Created by niugang on 2019/12/24/15:54
 */
@Slf4j
public class TaskTest2 {
  public void task2() {
    log.info("反射调用测试[二]类");
  }
}

配置类

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Created by niugang on 2019/12/24/15:19
 */
@Configuration
@EnableScheduling
@Slf4j
public class CompleteScheduleConfig implements SchedulingConfigurer {

  private static List<TaskRecord> taskRecordList = new ArrayList<>();

  /*
   *模拟数据库存储
   */
  static {
    TaskRecord taskRecord = new TaskRecord();
    taskRecord.setExecuteMehod("task1");
    taskRecord.setClassPath("com.example.demo.pojo.TaskTest");
    taskRecord.setCron("0/5 * * * * ?");
    taskRecordList.add(taskRecord);

    TaskRecord taskRecord2 = new TaskRecord();
    taskRecord2.setExecuteMehod("task2");
    taskRecord2.setClassPath("com.example.demo.pojo.TaskTest2");
    taskRecord2.setCron("0/10 * * * * ?");
    taskRecordList.add(taskRecord2);
  }

  @Override
  public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    // taskRegistrar.addCronTask(() -> log.info("执行定时任务,{}", LocalDateTime.now()), "0/5 * * * * ?");
/*    taskRegistrar.addCronTask(new Runnable() {
      @Override
      public void run() {
        try {
          Class<?> aClass = Class.forName("com.example.demo.pojo.TaskTest");
          Object o = aClass.newInstance();
          Method[] declaredMethods = aClass.getDeclaredMethods();
          for (Method declaredMethod : declaredMethods) {
            declaredMethod.invoke(o);
            // log.info("方法名称:{}",declaredMethod.getName());
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }, "0/5 * * * * ?");*/

    for (TaskRecord taskRecord : taskRecordList) {
      String classPath = taskRecord.getClassPath();
      String cron = taskRecord.getCron();
      String executeMehod = taskRecord.getExecuteMehod();
      Runnable runnable = () -> {
        Class<?> aClass;
        try {
          aClass = Class.forName(classPath);
          Object o = aClass.newInstance();
          Method[] declaredMethods = aClass.getDeclaredMethods();
          for (Method declaredMethod : declaredMethods) {
            if (declaredMethod.getName().equals(executeMehod)) {
              /// log.info("方法名称:{}",declaredMethod.getName());
              declaredMethod.invoke(o);
            }
          }
        } catch (Exception e1) {
          e1.printStackTrace();
        }
      };
      CronTask cronTask = new CronTask(runnable, cron);
      ScheduledTask scheduledTask = taskRegistrar.scheduleCronTask(cronTask);
      //scheduledTask.cancel(); 取消定时任务

    }

  }

  @Data
  private static class TaskRecord {

    private String classPath;

    private String executeMehod;

    private String cron;

    //可以在增加一个type 执行其他类型的定时任务
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • springboot整合quartz实现定时任务示例

    在做项目时有时候会有定时器任务的功能,比如某某时间应该做什么,多少秒应该怎么样之类的. spring支持多种定时任务的实现.我们来介绍下使用spring的定时器和使用quartz定时器 1.我们使用spring-boot作为基础框架,其理念为零配置文件,所有的配置都是基于注解和暴露bean的方式. 2.使用spring的定时器: spring自带支持定时器的任务实现.其可通过简单配置来使用到简单的定时任务. @Component @Configurable @EnableScheduling p

  • SpringBoot 定时任务遇到的坑

    前言 springboot已经支持了定时任务Schedule模块,一般情况已经完全能够满足我们的实际需求.今天就记录一下我使用 schedule 时候踩的坑吧. 想要使用定时,我们首先要开启支持,其实就是在启动类上面加个注解就 Ok. @SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(A

  • SpringBoot定时任务两种(Spring Schedule 与 Quartz 整合 )实现方法

    前言 最近在项目中使用到定时任务,之前一直都是使用Quartz 来实现,最近看Spring 基础发现其实Spring 提供 Spring Schedule 可以帮助我们实现简单的定时任务功能. 下面说一下两种方式在Spring Boot 项目中的使用. Spring Schedule 实现定时任务 Spring Schedule 实现定时任务有两种方式 1. 使用XML配置定时任务, 2. 使用 @Scheduled 注解. 因为是Spring Boot 项目 可能尽量避免使用XML配置的形式,

  • Springboot整个Quartz实现动态定时任务的示例代码

    简介 Quartz是一款功能强大的任务调度器,可以实现较为复杂的调度功能,如每月一号执行.每天凌晨执行.每周五执行等等,还支持分布式调度.本文使用Springboot+Mybatis+Quartz实现对定时任务的增.删.改.查.启用.停用等功能.并把定时任务持久化到数据库以及支持集群. Quartz的3个基本要素 Scheduler:调度器.所有的调度都是由它控制. Trigger: 触发器.决定什么时候来执行任务. JobDetail & Job: JobDetail定义的是任务数据,而真正的

  • 详解Spring Boot 定时任务的实现方法

    最近在用SpringBoot写一个关于定时项目的时候遇到一个问题,就是客户端访问服务器的结果实际上是每个一段时间发生一次变化,并且在服务器在每天的某个固定的时间点都要触发一次事件. 我们当然可以在遇到每一个请求时都重新计算结果,但是为了提高效率,我们显然可以让服务器每隔一段时间计算一次结果,并且把这个结果进行保存,对在下一个时间段内的每个请求都直接返回计算后的结果.这样就能较好的提高了服务器的性能. 那么问题就在于如何处理定时任务.其实SpringBoot早就提供了非常方便的接口,但是网上的介绍

  • springboot整合Quartz实现动态配置定时任务的方法

    前言 在我们日常的开发中,很多时候,定时任务都不是写死的,而是写到数据库中,从而实现定时任务的动态配置,下面就通过一个简单的示例,来实现这个功能. 一.新建一个springboot工程,并添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency

  • spring boot整合quartz实现多个定时任务的方法

    最近收到了很多封邮件,都是想知道spring boot整合quartz如何实现多个定时任务的,由于本人生产上并没有使用到多个定时任务,这里给个实现的思路. 1.新建两个定时任务,如下: public class ScheduledJob implements Job{ @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("sched

  • springboot集成schedule实现定时任务

    背景 在项目开发过程中,我们经常需要执行具有周期性的任务.通过定时任务可以很好的帮助我们实现. 我们拿常用的几种定时任务框架做一个比较: 从以上表格可以看出,Spring Schedule框架功能完善,简单易用.对于中小型项目需求,Spring Schedule是完全可以胜任的. 1.springboot集成schedule 1.1 添加maven依赖包 由于Spring Schedule包含在spring-boot-starter基础模块中了,所有不需要增加额外的依赖. <dependenci

  • 详解SpringBoot定时任务说明

    1. 定时任务实现方式 定时任务实现方式: Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行.一般用的较少,这篇文章将不做详细介绍. 使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,有空介绍. SpringBoot自带的Scheduled,可以将它看成一个轻量级的Quartz,而且使用起来比Q

  • SpringBoot实现动态定时任务

    项目情况: 在当前项目中需要一个定时任务来清除过期的校验码,如果使用数据库存储过程的话不方便维护.因此采用SpringBoot自带的方式来设置定时任务. 技术说明: SpringBoot自带的方式有两种可以实现: 一种是使用@Scheduled注解的方式,只需要在启动类或者它所在的类上添加@EnableScheduling注解允许执行定时任务,并且设置Schecduled注解的参数,诸如: 1.cron是设置定时执行的表达式,如 0 0/5 * * * ?每隔五分钟执行一次 2.zone表示执行

  • Spring boot定时任务的原理及动态创建详解

    v一.前言 定时任务一般是项目中都需要用到的,可以用于定时处理一些特殊的任务.这篇文章主要给大家介绍了关于Spring boot定时任务的原理及动态创建的相关内容,下面来一起看看详细的介绍吧 上周工作遇到了一个需求,同步多个省份销号数据,解绑微信粉丝.分省定时将销号数据放到SFTP服务器上,我需要开发定时任务去解析文件.因为是多省份,服务器.文件名规则.数据规则都不一定,所以要做成可配置是有一定难度的.数据规则这块必须强烈要求统一,服务器.文件名规则都可以从配置中心去读.每新增一个省份的配置,后

随机推荐