定时任务

定时任务实现的几种方式

  • 1、Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少。
  • 2、ScheduledExecutorService:也jdk自带的一个类;是基于线程池设计的定时任务类,每个调度任务都会分配到线程池中的一个线程去执行,也就是说,任务是并发执行,互不影响。
  • 3、Spring Task:Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多。
  • 4、Quartz:这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂。

项目开发中经常需要执行一些定时任务,比如需要在每天凌晨,分析一次前一天的日志信息。Spring为我们提供了异步执行任务调度的方式,提供TaskExcutor、TaskScheduler接口。

方式一:使用Timer类实现定时调度

这个目前在项目中用的较少,直接贴demo代码。具体的介绍可以查看api

package com.free.freedom.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TestTimer {
    public static void main(String[] args) {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                System.out.println("task  run"+format.format(new Date()));
            }
        };
		//设置一个定时器
        Timer timer = new Timer();
        //安排指定的任务在指定的时间开始进行重复的固定延迟执行。这是是每3秒执行一次
        //当前时间的10秒钟之后每隔3秒执行一次
        timer.schedule(timerTask,10,3000);
    }
}

执行结果

task  run2020-08-17 18:48:35
task  run2020-08-17 18:48:38
task  run2020-08-17 18:48:41
task  run2020-08-17 18:48:44
task  run2020-08-17 18:48:47
task  run2020-08-17 18:48:50
task  run2020-08-17 18:48:53
task  run2020-08-17 18:48:56
task  run2020-08-17 18:48:59
task  run2020-08-17 18:49:02
task  run2020-08-17 18:49:05
task  run2020-08-17 18:49:08
task  run2020-08-17 18:49:11

要执行的任务需要继承TimerTask,并且实现run()方法,使用Timer这个定时器将任务进行定时发送;
schedule的4种用法

  • 1、schedule(task, time)
    task(TimerTask):要执行的任务
    time(Date):执行的时间(什么时候去执行),只执行一次,如果time早于现在的时间,就会立即执行。
  • 2、schedule(task, delay)
    task(TimerTask):要执行的任务
    delay(Long):多久后去执行,只执行一次。比如如果delay的值为2000,task就会在距离当前时间2秒后去执行。
  • 3、schedule(task, time, period)
    task(TimerTask):要执行的任务
    time(Date):第一次执行任务的时间
    period(Long):每隔多久执行一次。比如period的值为2000,task就会在第一次执行之后,每隔2秒执行一次任务。
  • 4.schedule(task, delay, period)
    task(TimerTask):要执行的任务
    delay(Long):多久后去执行
    period(Long):每隔多久执行一次

可以参考https://blog.csdn.net/chsyd1028/article/details/79411687

Timer 缺点分析

Timer 类实现定时任务虽然方便,但在使用时需要注意以下问题。

  • 问题 1:任务执行时间长影响其他任务
  • 问题 2:任务异常影响其他任务

问题 1:任务执行时间长影响其他任务
当一个任务的执行时间过长时,会影响其他任务的调度,如下代码所示:

public class MyTimerTask {
    public static void main(String[] args) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //定义任务1
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                System.out.println("进入 timerTask 1:"+format.format(new Date()));
                try {
                    //休眠5秒
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Run timerTask 1:"+format.format(new Date()));
            }
        };

        //定义任务2
        TimerTask timerTask1 = new TimerTask() {
            @Override
            public void run() {
                System.out.println("Run timerTask 2:"+format.format(new Date()));
            }
        };
        //计时器
        Timer timer = new Timer();
        //添加执行任务(延迟1s执行,每3s执行一次)
        timer.schedule(timerTask,1000,3000);
        timer.schedule(timerTask1,1000,3000);
    }
  }  

执行结果

进入 timerTask 1:2020-08-18 23:46:15
Run timerTask 1:2020-08-18 23:46:20
Run timerTask 2:2020-08-18 23:46:20
进入 timerTask 1:2020-08-18 23:46:20
Run timerTask 1:2020-08-18 23:46:25
进入 timerTask 1:2020-08-18 23:46:25
Run timerTask 1:2020-08-18 23:46:30
Run timerTask 2:2020-08-18 23:46:30
进入 timerTask 1:2020-08-18 23:46:30
Run timerTask 1:2020-08-18 23:46:35
进入 timerTask 1:2020-08-18 23:46:35
Run timerTask 1:2020-08-18 23:46:40
Run timerTask 2:2020-08-18 23:46:40
进入 timerTask 1:2020-08-18 23:46:40
Run timerTask 1:2020-08-18 23:46:45

从上述结果中可以看出,当任务 1 运行时间超过设定的间隔时间时,任务 2 也会延迟执行。 原本任务 1 和任务 2 的执行时间间隔都是
3s,但因为任务 1 执行了 5s,因此任务 2 的执行时间间隔也变成了 10s(和原定时间不符)。

问题 2:任务异常影响其他任务
使用 Timer 类实现定时任务时,当一个任务抛出异常,其他任务也会终止运行,如下代码所示:

public class MyTimerTaskException {
    public static void main(String[] args) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //定义任务1
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                System.out.println("进入 timerTask 1:"+format.format(new Date()));
                //模拟异常
                int num = 8/0;
                System.out.println("Run timerTask 1:"+format.format(new Date()));
            }
        };

        //定义任务2
        TimerTask timerTask1 = new TimerTask() {
            @Override
            public void run() {
                System.out.println("Run timerTask 2:"+format.format(new Date()));
            }
        };
        //定义计时器
        Timer timer = new Timer();
        //添加执行任务(延迟1s执行,每3s执行一次)
        timer.schedule(timerTask,1000,3000);
        timer.schedule(timerTask1,1000,3000);
    }

执行结果

进入 timerTask 1:2020-08-18 23:57:36
Exception in thread "Timer-0" java.lang.ArithmeticException: / by zero
	at com.free.freedom.timerlongtime.MyTimerTaskException$1.run(MyTimerTaskException.java:18)
	at java.util.TimerThread.mainLoop(Timer.java:555)
	at java.util.TimerThread.run(Timer.java:505)

Timer 小结
Timer 类实现定时任务的优点是方便,因为它是 JDK 自定的定时任务,但缺点是任务如果执行时间太长或者是任务执行异常,会影响其他任务调度,所以在生产环境下建议谨慎使用。

方式二:使用ScheduledExecutorService

ScheduledExecutorService 也是 JDK 1.5 自带的 API,我们可以使用它来实现定时任务的功能,也就是说 ScheduledExecutorService 可以实现 Timer 类具备的所有功能,并且它可以解决了 Timer 类存在的所有问题。

ScheduledExecutorService 实现定时任务的代码示例如下:

public class MyScheduledExecutorService {
    public static void main(String[] args) {
        //创建任务队列  10为线程数量
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);

        //执行任务  1s后开始执行,每3s执行一次
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            System.out.println("Run Schedule:" + format.format(new Date()));
        }, 1, 3, TimeUnit.SECONDS);
    }
}

程序执行结果

Run Schedule:2020-08-19 00:16:44
Run Schedule:2020-08-19 00:16:47
Run Schedule:2020-08-19 00:16:50
Run Schedule:2020-08-19 00:16:53
Run Schedule:2020-08-19 00:16:56
Run Schedule:2020-08-19 00:16:59
Run Schedule:2020-08-19 00:17:02
Run Schedule:2020-08-19 00:17:05

ScheduledExecutorService 可靠性测试
① 任务超时执行测试
ScheduledExecutorService 可以解决 Timer 任务之间相应影响的缺点,首先我们来测试一个任务执行时间过长,会不会对其他任务造成影响,测试代码如下:

public class MyScheduledExecutorServiceOne {
    public static void main(String[] args) {
        //创建任务队列
        ScheduledExecutorService service = Executors.newScheduledThreadPool(10);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //执行任务1
        service.scheduleAtFixedRate(() -> {
            System.out.println("进入 Schedule:" + format.format(new Date()));
            try {
                //休眠5秒
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Run Schedule:" + format.format(new Date()));
        }, 1, 3, TimeUnit.SECONDS); //1s后开始执行,每3s执行一次

        //执行任务2
        service.scheduleAtFixedRate(() -> {
            System.out.println("Run Schedule2:" + format.format(new Date()));
        }, 1, 3, TimeUnit.SECONDS); // 1s 后开始执行,每 3s 执行一次
    }
}

执行结果

Run Schedule2:2020-08-19 00:32:06  //1
进入 Schedule:2020-08-19 00:32:06
Run Schedule2:2020-08-19 00:32:09  //2
Run Schedule:2020-08-19 00:32:11
进入 Schedule:2020-08-19 00:32:11
Run Schedule2:2020-08-19 00:32:12  //3
Run Schedule2:2020-08-19 00:32:15	//4
Run Schedule:2020-08-19 00:32:16
进入 Schedule:2020-08-19 00:32:16
Run Schedule2:2020-08-19 00:32:18

从上述结果可以看出,当任务 1 执行时间 5s 超过了执行频率 3s 时,并没有影响任务 2 的正常执行,因此使用
ScheduledExecutorService 可以避免任务执行时间过长对其他任务造成的影响。

② 任务异常测试
接下来我们来测试一下 ScheduledExecutorService 在一个任务异常时,是否会对其他任务造成影响,测试代码如下:

public class MyScheduledExecutorServiceTwo {

    public static void main(String[] args) {
        //创建任务队列
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //执行任务1
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            System.out.println("进入 Schedule:" + format.format(new Date()));
            // 模拟异常
            int num = 8 / 0;
            System.out.println("Run Schedule:" + format.format(new Date()));
        }, 1, 3, TimeUnit.SECONDS); // 1s 后开始执行,每 3s 执行一次
        // 执行任务 2
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            System.out.println("Run Schedule2:" + format.format(new Date()));
        }, 1, 3, TimeUnit.SECONDS); // 1s 后开始执行,每 3s 执行一次
    }
}

执行结果

Run Schedule2:2020-08-19 00:36:58
进入 Schedule:2020-08-19 00:36:58
Run Schedule2:2020-08-19 00:37:01
Run Schedule2:2020-08-19 00:37:04
Run Schedule2:2020-08-19 00:37:07

从上述结果可以看出,当任务 1 出现异常时,并不会影响任务 2 的执行。

ScheduledExecutorService 小结
在单机生产环境下建议使用 ScheduledExecutorService 来执行定时任务,它是 JDK 1.5 之后自带的 API,因此使用起来也比较方便,并且使用 ScheduledExecutorService 来执行任务,不会造成任务间的相互影响。

scheduleAtFixedRate的2种用法

  • 1、scheduleAtFixedRate(task, time, period)
    task(TimerTask):要执行的任务
    time(Date):第一次执行任务的时间
    period(Long):每隔多久执行一次。比如period的值为2000,task就会在第一次执行之后,每隔2秒执行一次任务。
  • 2、scheduleAtFixedRate(task, delay, period)
    task(TimerTask):要执行的任务
    delay(Long):多久后去执行
    period(Long):每隔多久执行一次

schedule和scheduleAtFixedRate的区别

  • 1.如果第一次执行的时间被delay,比如设定的执行时间为12:00:00,但timer开始执行的时候是12:00:06,schedule会以此顺延时间,第一次执行时间就变为了12:00:06,而scheduleAtFixedRate会按照上一次开始的时间计算,为了赶上进度会多次执行任务,以此需要考虑同步。
  • 2.任务执行需要的时间如果超出时间间隔,比如这个任务执行完需要3秒,而timer中定的周期为2秒,schedule会将执行的时间以此顺延,也就是完成了一个3秒的任务后,继续完成下一个任务,并且不会间断。而scheduleAtFixedRate时间不会顺延,会有并发性。

方式三:使用Spring Task

如果使用的是 Spring 或 Spring Boot 框架,可以直接使用 Spring Framework 自带的定时任务,使用上面两种定时任务的实现方式,很难实现设定了具体时间的定时任务,比如当我们需要每周五来执行某项任务时,但如果使用 Spring Task 就可轻松的实现此需求。

以 Spring Boot 为例,实现定时任务只需两步:

  • 1、开启定时任务;
  • 2、添加定时任务。

① 开启定时任务
开启定时任务只需要在 Spring Boot 的启动类上声明 @EnableScheduling 即可,实现代码如下:

@SpringBootApplication
@EnableScheduling // 开启定时任务
public class DemoApplication {
    // do someing
}

② 添加定时任务
定时任务的添加只需要使用 @Scheduled 注解标注即可,如果有多个定时任务可以创建多个 @Scheduled 注解标注的方法,示例代码如下:

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component // 把此类托管给 Spring,不能省略
public class TaskUtils {
    // 添加定时任务
    @Scheduled(cron = "59 59 23 0 0 5") // cron 表达式,每周五 23:59:59 执行
    public void doTask(){
        System.out.println("我是定时任务~");
    }
}

注意:定时任务是自动触发的无需手动干预,也就是说 Spring Boot 启动后会自动加载并执行定时任务

简单的定时任务
SpringBoot项目中,我们可以很优雅的使用注解来实现定时任务,首先创建项目,导入依赖:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

创建任务类

@Slf4j
@Component
public class ScheduledService {
    @Scheduled(cron = "0/5 * * * * *")
    public void scheduled(){
        log.info("=====>>>>>使用cron  {}",System.currentTimeMillis());
    }
    
    @Scheduled(fixedRate = 5000)
    public void scheduled1() {
        log.info("=====>>>>>使用fixedRate{}", System.currentTimeMillis());
    }

    @Scheduled(fixedDelay = 5000)
    public void scheduled2() {
        log.info("=====>>>>>fixedDelay{}",System.currentTimeMillis());
    }
}

在主类上使用@EnableScheduling注解开启对定时任务的支持,然后启动项目
在这里插入图片描述
可以看到三个定时任务都已经执行,并且使同一个线程中串行执行,如果只有一个定时任务,这样做肯定没问题,当定时任务增多,如果一个任务卡死,会导致其他任务也无法执行。

多线程执行
在传统的Spring项目中,我们可以在xml配置文件添加task的配置,而在SpringBoot项目中一般使用config配置类的方式添加配置,所以新建一个AsyncConfig类

@Configuration
@EnableAsync
public class AsyncConfig {
     /*
    此处成员变量应该使用@Value从配置中读取
     */
    private int corePoolSize = 10;
    private int maxPoolSize = 200;
    private int queueCapacity = 10;

    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.initialize();
        return executor;
    }
}

@Configuration:表明该类是一个配置类@EnableAsync:开启异步事件的支持 然后在定时任务的类或者方法上添加@Async 。最后重启项目,每一个任务都是在不同的线程中
在这里插入图片描述
执行时间的配置
在上面的定时任务中,我们在方法上使用@Scheduled注解来设置任务的执行时间,并且使用三种属性配置方式:

  • fixedRate:定义一个按一定频率执行的定时任务
  • fixedDelay:定义一个按一定频率执行的定时任务,与上面不同的是,改属性可以配合initialDelay, 定义该任务延迟执行时间。
  • cron:通过表达式来配置任务执行时间

cron表达式详解
cron表达式在线生成地址:https://cron.qqe2.com/

格式: [秒] [分] [小时] [日] [月] [周] [年]

字段允许值允许的特殊字符
0-59, - * /
0-59, - * /
小时0-23, - * /
日期1-31, - * ? / L W
月份1-12 or JAN-DEC, - * /
1-7 or SUN-SAT, - * ? / L #
empty 或 1970-2099, - * /

通配符说明:

  • , 表示指定多个值,例如在周字段上设置 “MON,WED,FRI” 表示周一,周三和周五触发
  • - 表示区间。例如 在小时上设置 “10-12”,表示 10,11,12点都会触发
  • * 表示所有值. 例如:在分的字段上设置 “*”,表示每一分钟都会触发。
  • / 用于递增触发。如在秒上面设置"5/15" 表示从5秒开始,每增15秒触发(5,20,35,50)。在月字段上设置’1/3’所示每月1号开始,每隔三天触发一次。
  • ? 表示不指定值。使用的场景为不需要关心当前设置这个字段的值。例如:要在每月的10号触发一个操作,但不关心是周几,所以需要周位置的那个字段设置为"?" 具体设置为 0 0 0 10* ?
  • L 表示最后的意思。在日字段设置上,表示当月的最后一天(依据当前月份,如果是二月还会依据是否是润年[leap]), 在周字段上表示星期六,相当于"7"或"SAT"。如果在"L"前加上数字,则表示该数据的最后一个。例如在周字段上设置"6L"这样的格式,则表示“本月最后一个星期五"
  • W 表示离指定日期的最近那个工作日(周一至周五). 例如在日字段上设置"15W",表示离每月15号最近的那个工作日触发。如果15号正好是周六,则找最近的周五(14号)触发, 如果15号是周未,则找最近的下周一(16号)触发.如果15号正好在工作日(周一至周五),则就在该天触发。如果指定格式为 “1W”,它则表示每月1号往后最近的工作日触发。如果1号正是周六,则将在3号下周一触发。(注,“W"前只能设置具体的数字,不允许区间”-").
  • # 序号(表示每月的第几个周几),例如在周字段上设置"6#3"表示在每月的第三个周六.注意如果指定"#5",正好第五周没有周六,则不会触发该配置(用在母亲节和父亲节再合适不过了)

注意:'L’和 'W’可以一组合使用。如果在日字段上设置"LW",则表示在本月的最后一个工作日触发(一般指发工资 )
周字段的设置,若使用英文字母是不区分大小写的 MON 与mon相同.

常用示例:

0 0 12 * * ?每天12点触发
0 15 10 ? * *每天10点15分触发
0 15 10 * * ?每天10点15分触发
0 15 10 * * ? *每天10点15分触发
0 15 10 * * ? 20052005年每天10点15分触发
0 * 14 * * ?每天下午的 2点到2点59分每分触发
0 0/5 14 * * ?每天下午的 2点到2点59分(整点开始,每隔5分触发)
0 0/5 14,18 * * ?每天下午的 2点到2点59分(整点开始,每隔5分触发)每天下午的 18点到18点59分(整点开始,每隔5分触发)
0 0-5 14 * * ?每天下午的 2点到2点05分每分触发
0 10,44 14 ? 3 WED3月分每周三下午的 2点10分和2点44分触发
0 15 10 ? * MON-FRI从周一到周五每天上午的10点15分触发
0 15 10 15 * ?每月15号上午10点15分触发
0 15 10 L * ?每月最后一天的10点15分触发
0 15 10 ? * 6L每月最后一周的星期五的10点15分触发
0 15 10 ? * 6L 2002-2005从2002年到2005年每月最后一周的星期五的10点15分触发
0 15 10 ? * 6#3每月的第三周的星期五开始触发
0 0 12 1/5 * ?每月的第一个中午开始每隔5天触发一次
0 11 11 11 11 ?每年的11月11号 11点11分触发(光棍节)

https://blog.csdn.net/xinyuan_java/article/details/51602088
https://blog.csdn.net/java_2017_csdn/article/details/78060204

方式四:(分布式定时任务)整合Quartz

https://mp.weixin.qq.com/s/xXNlsBtt-IzVETCg-NYomw
https://mp.weixin.qq.com/s/tMk3IxcWWkDcCBhLVreFeg

参考地址:
http://blog.itpub.net/31561269/viewspace-2285477/
https://mp.weixin.qq.com/s/xWacm-lYSooh09Jp0BhIPA
https://blog.csdn.net/chsyd1028/article/details/79411687
https://www.cnblogs.com/51kata/p/5128745.html
https://blog.csdn.net/weixin_42591674/article/details/88237599
https://blog.51cto.com/10926470/1953952

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值