Spring Boot如何实现定时任务的动态增删启停详解

我以为动态停启定时任务一般用quartz,没想到还可以通过ScheduledTaskRegistrar来拓展。但是分布式场景,建议还是用quartz吧!

在 spring boot 项目中,可以通过 @EnableScheduling 注解和 @Scheduled 注解实现定时任务,也可以通过 SchedulingConfigurer 接口来实现定时任务。但是这两种方式不能动态添加、删除、启动、停止任务。要实现动态增删启停定时任务功能,比较广泛的做法是集成 Quartz 框架。

但是本人的开发原则是:在满足项目需求的情况下,尽量少的依赖其它框架,避免项目过于臃肿和复杂。查看 spring-context 这个 jar 包中 org.springframework.scheduling.ScheduledTaskRegistrar 这个类的源代码,发现可以通过改造这个类就能实现动态增删启停定时任务功能。

定时任务列表页

定时任务执行日志

添加执行定时任务的线程池配置类

@Configuration
public class SchedulingConfig {
 @Bean
 public TaskScheduler taskScheduler() {
  ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();

  taskScheduler.setPoolSize(4);
  taskScheduler.setRemoveOnCancelPolicy(true);
  taskScheduler.setThreadNamePrefix("TaskSchedulerThreadPool-");
  return taskScheduler;
 }
}

添加 ScheduledFuture 的包装类。ScheduledFuture 是 ScheduledExecutorService 定时任务线程池的执行结果。

public final class ScheduledTask {

 volatile ScheduledFuture<?> future;

 public void cancel() {
  ScheduledFuture<?> future = this.future;
  if (future != null) {
   future.cancel(true);
  }
 }
}

添加 Runnable 接口实现类,被定时任务线程池调用,用来执行指定 bean 里面的方法。

public class SchedulingRunnable implements Runnable {

 private static final Logger logger = LoggerFactory.getLogger(SchedulingRunnable.class);

 private String beanName;

 private String methodName;

 private String params;

 public SchedulingRunnable(String beanName, String methodName) {
  this(beanName, methodName, null);
 }

 public SchedulingRunnable(String beanName, String methodName, String params) {
  this.beanName = beanName;
  this.methodName = methodName;
  this.params = params;
 }

 @Override
 public void run() {
  logger.info("定时任务开始执行 - bean:{},方法:{},参数:{}", beanName, methodName, params);
  long startTime = System.currentTimeMillis();

  try {
   Object target = SpringContextUtils.getBean(beanName);

   Method method = null;
   if (StringUtils.isNotEmpty(params)) {
    method = target.getClass().getDeclaredMethod(methodName, String.class);
   } else {
    method = target.getClass().getDeclaredMethod(methodName);
   }

   ReflectionUtils.makeAccessible(method);
   if (StringUtils.isNotEmpty(params)) {
    method.invoke(target, params);
   } else {
    method.invoke(target);
   }
  } catch (Exception ex) {
   logger.error(String.format("定时任务执行异常 - bean:%s,方法:%s,参数:%s ", beanName, methodName, params), ex);
  }

  long times = System.currentTimeMillis() - startTime;
  logger.info("定时任务执行结束 - bean:{},方法:{},参数:{},耗时:{} 毫秒", beanName, methodName, params, times);
 }

 @Override
 public boolean equals(Object o) {
  if (this == o) return true;
  if (o == null || getClass() != o.getClass()) return false;
  SchedulingRunnable that = (SchedulingRunnable) o;
  if (params == null) {
   return beanName.equals(that.beanName) &&
     methodName.equals(that.methodName) &&
     that.params == null;
  }

  return beanName.equals(that.beanName) &&
    methodName.equals(that.methodName) &&
    params.equals(that.params);
 }

 @Override
 public int hashCode() {
  if (params == null) {
   return Objects.hash(beanName, methodName);
  }

  return Objects.hash(beanName, methodName, params);
 }
}

添加定时任务注册类,用来增加、删除定时任务。

@Component
public class CronTaskRegistrar implements DisposableBean {

 private final Map<Runnable, ScheduledTask> scheduledTasks = new ConcurrentHashMap<>(16);

 @Autowired
 private TaskScheduler taskScheduler;

 public TaskScheduler getScheduler() {
  return this.taskScheduler;
 }

 public void addCronTask(Runnable task, String cronExpression) {
  addCronTask(new CronTask(task, cronExpression));
 }

 public void addCronTask(CronTask cronTask) {
  if (cronTask != null) {
   Runnable task = cronTask.getRunnable();
   if (this.scheduledTasks.containsKey(task)) {
    removeCronTask(task);
   }

   this.scheduledTasks.put(task, scheduleCronTask(cronTask));
  }
 }

 public void removeCronTask(Runnable task) {
  ScheduledTask scheduledTask = this.scheduledTasks.remove(task);
  if (scheduledTask != null)
   scheduledTask.cancel();
 }

 public ScheduledTask scheduleCronTask(CronTask cronTask) {
  ScheduledTask scheduledTask = new ScheduledTask();
  scheduledTask.future = this.taskScheduler.schedule(cronTask.getRunnable(), cronTask.getTrigger());

  return scheduledTask;
 }

 @Override
 public void destroy() {
  for (ScheduledTask task : this.scheduledTasks.values()) {
   task.cancel();
  }

  this.scheduledTasks.clear();
 }
}

添加定时任务示例类

@Component("demoTask")
public class DemoTask {
 public void taskWithParams(String params) {
  System.out.println("执行有参示例任务:" + params);
 }

 public void taskNoParams() {
  System.out.println("执行无参示例任务");
 }
}

定时任务数据库表设计

定时任务数据库表设计

添加定时任务实体类

public class SysJobPO {

 private Integer jobId;

 private String beanName;

 private String methodName;

 private String methodParams;

 private String cronExpression;

 private Integer jobStatus;

 private String remark;

 private Date createTime;

 private Date updateTime;

 public Integer getJobId() {
  return jobId;
 }

 public void setJobId(Integer jobId) {
  this.jobId = jobId;
 }

 public String getBeanName() {
  return beanName;
 }

 public void setBeanName(String beanName) {
  this.beanName = beanName;
 }

 public String getMethodName() {
  return methodName;
 }

 public void setMethodName(String methodName) {
  this.methodName = methodName;
 }

 public String getMethodParams() {
  return methodParams;
 }

 public void setMethodParams(String methodParams) {
  this.methodParams = methodParams;
 }

 public String getCronExpression() {
  return cronExpression;
 }

 public void setCronExpression(String cronExpression) {
  this.cronExpression = cronExpression;
 }

 public Integer getJobStatus() {
  return jobStatus;
 }

 public void setJobStatus(Integer jobStatus) {
  this.jobStatus = jobStatus;
 }

 public String getRemark() {
  return remark;
 }

 public void setRemark(String remark) {
  this.remark = remark;
 }

 public Date getCreateTime() {
  return createTime;
 }

 public void setCreateTime(Date createTime) {
  this.createTime = createTime;
 }

 public Date getUpdateTime() {
  return updateTime;
 }

 public void setUpdateTime(Date updateTime) {
  this.updateTime = updateTime;
 }
}

新增定时任务

新增定时任务

boolean success = sysJobRepository.addSysJob(sysJob);
if (!success)
 return OperationResUtils.fail("新增失败");
else {
 if (sysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())) {
  SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(), sysJob.getMethodParams());
  cronTaskRegistrar.addCronTask(task, sysJob.getCronExpression());
 }
}

return OperationResUtils.success();

修改定时任务,先移除原来的任务,再启动新任务

boolean success = sysJobRepository.editSysJob(sysJob);
if (!success)
 return OperationResUtils.fail("编辑失败");
else {

 if (existedSysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())) {
  SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(), existedSysJob.getMethodParams());
  cronTaskRegistrar.removeCronTask(task);
 }

 if (sysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())) {
  SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(), sysJob.getMethodParams());
  cronTaskRegistrar.addCronTask(task, sysJob.getCronExpression());
 }
}

return OperationResUtils.success();

删除定时任务

boolean success = sysJobRepository.deleteSysJobById(req.getJobId());
if (!success)
 return OperationResUtils.fail("删除失败");
else{
 if (existedSysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())) {
  SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(), existedSysJob.getMethodParams());
  cronTaskRegistrar.removeCronTask(task);
 }
}

return OperationResUtils.success();

定时任务启动 / 停止状态切换

if (existedSysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())) {
 SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(), existedSysJob.getMethodParams());
 cronTaskRegistrar.addCronTask(task, existedSysJob.getCronExpression());
} else {
 SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(), existedSysJob.getMethodParams());
 cronTaskRegistrar.removeCronTask(task);
}

添加实现了 CommandLineRunner 接口的 SysJobRunner 类,当 spring boot 项目启动完成后,加载数据库里状态为正常的定时任务。

@Service
public class SysJobRunner implements CommandLineRunner {

 private static final Logger logger = LoggerFactory.getLogger(SysJobRunner.class);

 @Autowired
 private ISysJobRepository sysJobRepository;

 @Autowired
 private CronTaskRegistrar cronTaskRegistrar;

 @Override
 public void run(String... args) {

  List<SysJobPO> jobList = sysJobRepository.getSysJobListByStatus(SysJobStatus.NORMAL.ordinal());
  if (CollectionUtils.isNotEmpty(jobList)) {
   for (SysJobPO job : jobList) {
    SchedulingRunnable task = new SchedulingRunnable(job.getBeanName(), job.getMethodName(), job.getMethodParams());
    cronTaskRegistrar.addCronTask(task, job.getCronExpression());
   }

   logger.info("定时任务已加载完毕...");
  }
 }
}

工具类 SpringContextUtils,用来从 spring 容器里获取 bean

@Component
public class SpringContextUtils implements ApplicationContextAware {

 private static ApplicationContext applicationContext;

 @Override
 public void setApplicationContext(ApplicationContext applicationContext)
   throws BeansException {
  SpringContextUtils.applicationContext = applicationContext;
 }

 public static Object getBean(String name) {
  return applicationContext.getBean(name);
 }

 public static <T> T getBean(Class<T> requiredType) {
  return applicationContext.getBean(requiredType);
 }

 public static <T> T getBean(String name, Class<T> requiredType) {
  return applicationContext.getBean(name, requiredType);
 }

 public static boolean containsBean(String name) {
  return applicationContext.containsBean(name);
 }

 public static boolean isSingleton(String name) {
  return applicationContext.isSingleton(name);
 }

 public static Class<? extends Object> getType(String name) {
  return applicationContext.getType(name);
 }
}

总结

到此这篇关于Spring Boot如何实现定时任务的动态增删启停的文章就介绍到这了,更多相关SpringBoot定时任务的动态增删启停内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

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

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

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

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

  • 详解SpringBoot 创建定时任务(配合数据库动态执行)

    序言:创建定时任务非常简单,主要有两种创建方式:一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer). 前者相信大家都很熟悉,但是实际使用中我们往往想从数据库中读取指定时间来动态执行定时任务,这时候基于接口的定时任务就大派用场了. 一.静态定时任务(基于注解) 基于注解来创建定时任务非常简单,只需几行代码便可完成. @Scheduled 除了支持灵活的参数表达式cron之外,还支持简单的延时操作,例如 fixedDelay ,fixedRate 填写相应

  • springboot schedule 解决定时任务不执行的问题

    @schedule 注解 是springboot 常用的定时任务注解,使用起来简单方便,但是如果定时任务非常多,或者有的任务很耗时,会影响到其他定时任务的执行,因为schedule 默认是单线程的,一个任务在执行时,其他任务是不能执行的.解决办法是重新配置schedule,改为多线程执行.只需要增加下面的配置类就可以了. import org.springframework.boot.autoconfigure.batch.BatchProperties; import org.springfr

  • springboot集成schedule实现定时任务

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

  • SpringBoot实现动态定时任务

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

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

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

  • spring-boot通过@Scheduled配置定时任务及定时任务@Scheduled注解的方法

    串行的定时任务 @Component public class ScheduledTimer { private Logger logger = Logger.getLogger(this.getClass()); /** * 定时任务,1分钟执行1次,更新潜在客户超时客户共享状态 */ @Scheduled(cron="0 0/1 8-20 * * ?") public void executeUpdateCuTask() { Thread current = Thread.curr

  • SpringBoot 定时任务遇到的坑

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

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

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

随机推荐