iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >一文搞懂如何实现Java,Spring动态启停定时任务
  • 777
分享到

一文搞懂如何实现Java,Spring动态启停定时任务

2024-04-02 19:04:59 777人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录为什么需要定时任务Java定时任务的原理Timer+TimerTaskScheduledThreadPoolExecutorTimer VS ScheduledThreadPoo

为什么需要定时任务

定时任务的应用场景十分广泛,如定时清理文件、定时生成报表、定时数据同步备份等。

Java定时任务的原理

jdk自带的库中,有两种技术可以实现定时任务,一种是Timer,另一种是ScheduledThreadPoolExecutor

Timer+TimerTask

Timer是一个线程,控制执行TimerTask所需要执行的内容

public class Timer {
    
    private final TaskQueue queue = new TaskQueue();
    
    private final TimerThread thread = new TimerThread(queue);
    。。。。。。
}

其中,需要注意,Timer类有几个方法创建不同的线程执行:

延时执行

//其中的delay是延时时间,表示多少毫秒后执行一次task
public void schedule(TimerTask task, long delay) {
    if (delay < 0)
        throw new IllegalArgumentException("Negative delay.");
    sched(task, System.currentTimeMillis()+delay, 0);
}

指定时间点执行

//到达指定时间time的时候执行一次task
public void schedule(TimerTask task, Date time) {
    sched(task, time.getTime(), 0);
}

延时周期执行

//经过delay毫秒后按每period毫秒执行一次的周期执行task
public void schedule(TimerTask task, long delay, long period) {
    if (delay < 0)
        throw new IllegalArgumentException("Negative delay.");
    if (period <= 0)
        throw new IllegalArgumentException("Non-positive period.");
    sched(task, System.currentTimeMillis()+delay, -period);
}

指定时间点后周期执行

//到达指定时间firstTime之后按照每period毫秒执行一次的周期执行task
public void schedule(TimerTask task, Date firstTime, long period) {
    if (period <= 0)
        throw new IllegalArgumentException("Non-positive period.");
    sched(task, firstTime.getTime(), -period);
}

TimerTask是一个实现了Runable接口的类,所以能够放到线程去执行:

public abstract class TimerTask implements Runnable {
    
    final Object lock = new Object();
    
    。。。。。。
}

示例:

public class JavaTimerJob {
    public static void main(String[] args) {
        Timer timer = new Timer();
        Task task = new Task();
        //当前时间开始,每1秒执行一次
        timer.schedule(task, new Date(),1000);
    }
 }
class Task extends TimerTask {
    @Override
    public void run() {
        System.out.println(new Date()+":  This is my job...");
    }
}

执行结果:

Tue May 30 13:45:47 CST 2022:  This is my job...
Tue May 30 13:45:48 CST 2022:  This is my job...
Tue May 30 13:45:49 CST 2022:  This is my job...
Tue May 30 13:45:50 CST 2022:  This is my job...
。。。。

弊端:Timer是单线程的,一旦定时任务中某一过程时刻抛出异常,将会导致整体线程停止,定时任务停止。

ScheduledThreadPoolExecutor

继承了ThreadPoolExecutor,,是一个基于线程池的调度器 通过实现ScheduledExecutorService接口方法去实现任务调度,主要方法如下:

延时执行

//command是待执行的线程,delay表示延时时长,unit代表时间单位
public ScheduledFuture<?> schedule(Runnable command,
                                   long delay,
                                   TimeUnit unit) {
    if (command == null || unit == null)
        throw new NullPointerException();
    RunnableScheduledFuture<?> t = decorateTask(command,
        new ScheduledFutureTask<Void>(command, null,
                                      triggerTime(delay, unit)));
    delayedExecute(t);
    return t;
}

延时周期执行

//command是待执行的线程,initialDelay表示延时时长,period代表执行间隔时长,unit代表时间单位
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                              long initialDelay,
                                              long period,
                                              TimeUnit unit) {
    if (command == null || unit == null)
        throw new NullPointerException();
    if (period <= 0)
        throw new IllegalArgumentException();
    ScheduledFutureTask<Void> sft =
        new ScheduledFutureTask<Void>(command,
                                      null,
                                      triggerTime(initialDelay, unit),
                                      unit.toNanos(period));
    RunnableScheduledFuture<Void> t = decorateTask(command, sft);
    sft.outerTask = t;
    delayedExecute(t);
    return t;
}

每段延时间隔执行

//command是待执行的线程,initialDelay表示延时时长,delay代表每次执行线程前的延时时长,unit代表时间单位
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                 long initialDelay,
                                                 long delay,
                                                 TimeUnit unit) {
    if (command == null || unit == null)
        throw new NullPointerException();
    if (delay <= 0)
        throw new IllegalArgumentException();
    ScheduledFutureTask<Void> sft =
        new ScheduledFutureTask<Void>(command,
                                      null,
                                      triggerTime(initialDelay, unit),
                                      unit.toNanos(-delay));
    RunnableScheduledFuture<Void> t = decorateTask(command, sft);
    sft.outerTask = t;
    delayedExecute(t);
    return t;
}

示例:

public class JavaScheduledThreadPoolExecutor {
    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(8);
        //延时1秒后开始执行,每3秒执行一次
        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(new Date()+": This is my job...");
            }
        }, 1, 3, TimeUnit.SECONDS);
    }
}

执行结果:

Tue May 30 15:05:16 CST 2022: This is my job...
Tue May 30 15:05:19 CST 2022: This is my job...
Tue May 30 15:05:22 CST 2022: This is my job...
Tue May 30 15:05:25 CST 2022: This is my job...
。。。。。

Timer VS ScheduledThreadPoolExecutor

Timer

  • 是单线程,如果开启多个线程服务,将会出现竞争,一旦出现异常,线程停止,定时任务停止;
  • 兼容性更高,jdk1.3后使用

ScheduledThreadPoolExecutor

  • 基于线程池实现多线程,且自动调整线程数,线程出错并不会影响整体定时任务执行。
  • 在jdk1.5后可使用

Spring定时任务

Spring原生定时任务主要依靠@Scheduled注解实现:

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
   String CRON_DISABLED = "-";
   String cron() default "";  //类似于corn表达式,可以指定定时任务执行的延迟及周期规则
   
   String zone() default "";  //指明解析cron表达式的时区。
   long fixedDelay() default -1;  //在最后一次调用结束和下一次调用开始之间以固定周期(以毫秒为单位)执行带注解的方法。(要等待上次任务完成后)
   String fixedDelayString() default "";  //同上面作用一样,只是String类型
   long fixedRate() default -1;  //在调用之间以固定的周期(以毫秒为单位)执行带注解的方法。(不需要等待上次任务完成)
   
   String fixedRateString() default "";  //同上面作用一样,只是String类型
   long initialDelay() default -1;  //第一次执行fixedRate()或fixedDelay()任务之前延迟的毫秒数 。
   String initialDelayString() default "";  //同上面作用一样,只是String类型
}

Spring静态定时任务示例:

@Slf4j
@Component
public class TestJob {
   //每40秒执行一次
    @Scheduled(cron = "0/40 * * * * ?")
    public void logJob(){
        if(log.isDebugEnabled()){
            log.debug("现在是:{}",LocalDateTime.now());
        }
    }
}

执行结果:

现在是:2022-05-30T16:03:40.006
现在是:2022-05-30T16:04
现在是:2022-05-30T16:04:40.003

@Scheduled定时任务原理(源码)

项目启动扫描带有注解@Scheduled的所有方法信息由ScheduledAnnotationBeanPostProcessorpostProcessAfterInitialization方法实现功能:

public Object postProcessAfterInitialization(Object bean, String beanName) {
   
   if (bean instanceof aopInfrastructureBean || bean instanceof TaskScheduler ||
         bean instanceof ScheduledExecutorService) {
      // Ignore AOP infrastructure such as scoped proxies.
      return bean;
   }
   Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
   if (!this.nonAnnotatedClasses.contains(targetClass)) {
       //获取定时任务的方法
      Map<Method, Set<Scheduled>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
            (MethodIntrospector.MetadataLookup<Set<Scheduled>>) method -> {
               Set<Scheduled> scheduledMethods = AnnotatedElementUtils.getMergedRepeatableAnnotations(
                     method, Scheduled.class, Schedules.class);
               return (!scheduledMethods.isEmpty() ? scheduledMethods : null);
            });
      if (annotatedMethods.isEmpty()) {
         this.nonAnnotatedClasses.add(targetClass);
         if (logger.isTraceEnabled()) {
            logger.trace("No @Scheduled annotations found on bean class: " + targetClass);
         }
      }
      else {
         // Non-empty set of methods
         annotatedMethods.forEach((method, scheduledMethods) ->
               //调用processScheduled方法将定时任务方法存放到任务队列中
               scheduledMethods.forEach(scheduled -> processScheduled(scheduled, method, bean)));
         if (logger.isTraceEnabled()) {
            logger.trace(annotatedMethods.size() + " @Scheduled methods processed on bean '" + beanName +
                  "': " + annotatedMethods);
         }
      }
   }
   return bean;
}

②调用processScheduled方法将定时任务方法存放到任务队列中

protected void processScheduled(Scheduled scheduled, Method method, Object bean) {
   try {
       //创建任务线程
      Runnable runnable = createRunnable(bean, method);
      boolean processedSchedule = false;
      String errORMessage =
            "Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required";
      Set<ScheduledTask> tasks = new LinkedHashSet<>(4);
      //解析任务执行初始延迟
      long initialDelay = scheduled.initialDelay();
      String initialDelayString = scheduled.initialDelayString();
      if (StringUtils.hasText(initialDelayString)) {
         Assert.isTrue(initialDelay < 0, "Specify 'initialDelay' or 'initialDelayString', not both");
         if (this.embeddedValueResolver != null) {
            initialDelayString = this.embeddedValueResolver.resolveStringValue(initialDelayString);
         }
         if (StringUtils.hasLength(initialDelayString)) {
            try {
               initialDelay = parseDelayAsLong(initialDelayString);
            }
            catch (RuntimeException ex) {
               throw new IllegalArgumentException(
                     "Invalid initialDelayString value \"" + initialDelayString + "\" - cannot parse into long");
            }
         }
      }
      //解析cron表达式
      String cron = scheduled.cron();
      if (StringUtils.hasText(cron)) {
         String zone = scheduled.zone();
         if (this.embeddedValueResolver != null) {
            cron = this.embeddedValueResolver.resolveStringValue(cron);
            zone = this.embeddedValueResolver.resolveStringValue(zone);
         }
         if (StringUtils.hasLength(cron)) {
            Assert.isTrue(initialDelay == -1, "'initialDelay' not supported for cron triggers");
            processedSchedule = true;
            if (!Scheduled.CRON_DISABLED.equals(cron)) {
               TimeZone timeZone;
               if (StringUtils.hasText(zone)) {
                  timeZone = StringUtils.parseTimeZoneString(zone);
               }
               else {
                  timeZone = TimeZone.getDefault();
               }
               tasks.add(this.reGIStrar.scheduleCronTask(new CronTask(runnable, new CronTrigger(cron, timeZone))));
            }
         }
      }
      // At this point we don't need to differentiate between initial delay set or not anymore
      if (initialDelay < 0) {
         initialDelay = 0;
      }
      //解析fixedDelay参数
      long fixedDelay = scheduled.fixedDelay();
      if (fixedDelay >= 0) {
         Assert.isTrue(!processedSchedule, errorMessage);
         processedSchedule = true;
         
         //存放任务到任务队列中
         tasks.add(this.registrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay)));
      }
      
      String fixedDelayString = scheduled.fixedDelayString();
      if (StringUtils.hasText(fixedDelayString)) {
         if (this.embeddedValueResolver != null) {
            fixedDelayString = this.embeddedValueResolver.resolveStringValue(fixedDelayString);
         }
         if (StringUtils.hasLength(fixedDelayString)) {
            Assert.isTrue(!processedSchedule, errorMessage);
            processedSchedule = true;
            try {
               fixedDelay = parseDelayAsLong(fixedDelayString);
            }
            catch (RuntimeException ex) {
               throw new IllegalArgumentException(
                     "Invalid fixedDelayString value \"" + fixedDelayString + "\" - cannot parse into long");
            }
            tasks.add(this.registrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay)));
         }
      }
      //解析fixedRate参数
      long fixedRate = scheduled.fixedRate();
      if (fixedRate >= 0) {
         Assert.isTrue(!processedSchedule, errorMessage);
         processedSchedule = true;
         tasks.add(this.registrar.scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay)));
      }
      String fixedRateString = scheduled.fixedRateString();
      if (StringUtils.hasText(fixedRateString)) {
         if (this.embeddedValueResolver != null) {
            fixedRateString = this.embeddedValueResolver.resolveStringValue(fixedRateString);
         }
         if (StringUtils.hasLength(fixedRateString)) {
            Assert.isTrue(!processedSchedule, errorMessage);
            processedSchedule = true;
            try {
               fixedRate = parseDelayAsLong(fixedRateString);
            }
            catch (RuntimeException ex) {
               throw new IllegalArgumentException(
                     "Invalid fixedRateString value \"" + fixedRateString + "\" - cannot parse into long");
            }
            tasks.add(this.registrar.scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay)));
         }
      }
      // 断言检查
      Assert.isTrue(processedSchedule, errorMessage);
      //并发控制将任务队列存入注册任务列表
      synchronized (this.scheduledTasks) {
         Set<ScheduledTask> regTasks = this.scheduledTasks.computeIfAbsent(bean, key -> new LinkedHashSet<>(4));
         regTasks.addAll(tasks);
      }
   }
   catch (IllegalArgumentException ex) {
      throw new IllegalStateException(
            "Encountered invalid @Scheduled method '" + method.getName() + "': " + ex.getMessage());
   }
}

③将任务解析并添加到任务队列后,交由ScheduledTaskRegistrar类的scheduleTasks方法添加(注册)定时任务到环境中:

protected void scheduleTasks() {
   if (this.taskScheduler == null) {
       //获取ScheduledExecutorService对象,实际上都是使用ScheduledThreadPoolExecutor执行定时任务调度
      this.localExecutor = Executors.newSingleThreadScheduledExecutor();
      this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);
   }
   if (this.triggerTasks != null) {
      for (TriggerTask task : this.triggerTasks) {
         addScheduledTask(scheduleTriggerTask(task));
      }
   }
   if (this.cronTasks != null) {
      for (CronTask task : this.cronTasks) {
         addScheduledTask(scheduleCronTask(task));
      }
   }
   if (this.fixedRateTasks != null) {
      for (IntervalTask task : this.fixedRateTasks) {
         addScheduledTask(scheduleFixedRateTask(task));
      }
   }
   if (this.fixedDelayTasks != null) {
      for (IntervalTask task : this.fixedDelayTasks) {
         addScheduledTask(scheduleFixedDelayTask(task));
      }
   }
}
private void addScheduledTask(@Nullable ScheduledTask task) {
   if (task != null) {
      this.scheduledTasks.add(task);
   }
}

由上述源码可以看出,Spring原生定时任务的大概步骤如下:

1.扫描带@Scheduled注解的类和方法(ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(........))

2.将定时任务解析完成后加入任务队列(ScheduledAnnotationBeanPostProcessor.processScheduled(........))

3.将定时任务注册到当前运行环境,等待执行(ScheduledTaskRegistrar.scheduleTasks(.......)) 且@Scheduled的底层调度实现是ScheduledThreadPoolExecutor

以上就是一文搞懂如何实现Java,Spring动态启停定时任务的详细内容,更多关于Java Spring启停定时任务的资料请关注编程网其它相关文章!

--结束END--

本文标题: 一文搞懂如何实现Java,Spring动态启停定时任务

本文链接: https://www.lsjlt.com/news/152379.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 一文搞懂如何实现Java,Spring动态启停定时任务
    目录为什么需要定时任务Java定时任务的原理Timer+TimerTaskScheduledThreadPoolExecutorTimer VS ScheduledThreadPoo...
    99+
    2024-04-02
  • Spring Boot集成Quartz实现定时任务的动态创建、启动、暂停、恢复、删除
    一、整个 Quartz 的代码流程基本基本如下: 首先需要创建我们的任务(Job),比如取消订单、定时发送短信邮件之类的,这是我们的任务主体,也是写业务逻辑的地方。 创建任务调度器(Scheduler),这是用来调度任务的,主要用于启动...
    99+
    2023-09-10
    spring boot java spring Quartz Powered by 金山文档
  • SpringBoot动态定时任务如何实现
    这篇文章主要介绍了SpringBoot动态定时任务如何实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot动态定时任务如何实现文章都会有所收获,下面我们一起来看看吧。 执行定时任务的...
    99+
    2023-07-05
  • Spring动态添加定时任务的实现方法
    本篇内容主要讲解“Spring动态添加定时任务的实现方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring动态添加定时任务的实现方法”吧!一、背景在工作中,有些时候我们有些定时任务的执行...
    99+
    2023-06-20
  • Spring动态添加定时任务的实现思路
    一、背景 在工作中,有些时候我们有些定时任务的执行可能是需要动态修改的,比如: 生成报表,有些项目配置每天的8点生成,有些项目配置每天的10点生成,像这种动态的任务执行时间,在不考虑...
    99+
    2024-04-02
  • Java如何实现定时任务
    今天小编给大家分享一下Java如何实现定时任务的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。一、TimerTimer是JAV...
    99+
    2023-07-02
  • 如何通过RabbitMq实现动态定时任务详解
    目录一、需求背景二、方案思考(1)需求大致分析(2)可尝试的方案三、通过RabbitMQ实现延时任务并间接实现动态定时任务。(1)通过死信的方式实现延时信息消费(2)通过MQ延时插件...
    99+
    2024-04-02
  • 一文搞懂MySQL XA如何实现分布式事务
    目录前言XA 协议如何通过MySQL XA实现分布式事务前言 MySQL支持单机事务的良好表现毋庸置疑,那么在分布式系统中,涉及多个节点,MySQL又是如何实现分布式事务的呢?比如开...
    99+
    2024-04-02
  • 如何使用spring-task定时任务动态配置修改执行时间
    小编给大家分享一下如何使用spring-task定时任务动态配置修改执行时间,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!spring-task定时任务动态配置修改执行时间因项目需要,几个定时任务需要人为动态设置执行时间,...
    99+
    2023-06-25
  • 定时任务如何在Spring Boot 项目中实现
    定时任务如何在Spring Boot 项目中实现?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。创建定时服务一般来说定时服务会写在一个Component里,方便管理。对于定时任务...
    99+
    2023-05-31
    springboot boot 定时任务
  • 如何搭建基于Spring Task实现动态管理任务
    这篇文章主要讲解了“如何搭建基于Spring Task实现动态管理任务”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何搭建基于Spring Task实现动态管理任务”吧!什么是定时任务定时...
    99+
    2023-06-15
  • java web如何实现每天定时执行任务
    小编给大家分享一下java web如何实现每天定时执行任务,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!package com.eh.util;import java.util.Calendar;imp...
    99+
    2023-05-30
  • php如何实现定时任务,php定时任务方法,最佳解决方案,php自动任务处理
    定时任务对于php来说一直都是很多朋友的一个难题,但却很多地方都遇到了。 比如说: 游戏开发程序中,每隔10分钟给玩家发兵一次 sns社区中每隔20秒检测一下是否有人给我发消息 自动采集程序,每隔5分钟采集一次最新内容 微博数据同步,每隔1...
    99+
    2023-09-04
    php 服务器 开发语言
  • java spring+mybatis整合如何实现今日头条搞笑动态图片的爬取
    java spring+mybatis整合如何实现今日头条搞笑动态图片的爬取,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。java spring+mybatis整合实现爬虫之今...
    99+
    2023-06-19
  • 如何在MariaDB中实现自动化任务和定时作业
    在MariaDB中实现自动化任务和定时作业可以使用以下两种方法: 使用事件调度器(Event Scheduler):MariaDB...
    99+
    2024-04-09
    MariaDB
  • 在springboot项目中使用quartz如何实现一个定时任务
    今天就跟大家聊聊有关在springboot项目中使用quartz如何实现一个定时任务,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。spring支持多种定时任务的实现。我们来介绍下使用...
    99+
    2023-05-31
    springboot art quartz
  • 如何在springboot中利用Quartz实现一个定时任务功能
    如何在springboot中利用Quartz实现一个定时任务功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、新建一个springboot工程,并添加依赖<depen...
    99+
    2023-05-31
    springboot art quartz
  • linux定时任务crontab如何实现每秒执行一次的方法
    这篇文章给大家分享的是有关linux定时任务crontab如何实现每秒执行一次的方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。linux crontab 命令,最小的执行时间是一分钟。如需要在小于一分钟内重复...
    99+
    2023-06-09
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作