logo头像

From zero to HERO

Spring Boot 中使用 Spring Task 实现定时任务

本文于 1406 天之前发表,文中内容可能已经过时。

1. 前言

在日常项目开发中我们经常要使用定时任务。比如在凌晨进行统计结算,开启策划活动等等。今天我们就来看看如何在 Spring Boot 中使用 Spring 内置的定时任务。

2. 开启定时任务

Spring Boot 默认在无任何第三方依赖的情况下使用 spring-context 模块下提供的定时任务工具 Spring Task。我们只需要使用 @EnableScheduling 注解就可以开启相关的定时任务功能。如:

package cn.felord.schedule;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * @author felord.cn
 */
@SpringBootApplication
@EnableScheduling
public class SpringbootScheduleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootScheduleApplication.class, args);
    }

}

然后我们就可以通过注解的方式实现自定义定时任务,下面我将详细介绍如何使用注解实现定时任务。

3. @Scheduled注解实现定时任务

只需要定义一个 Spring Bean ,然后定义具体的定时任务逻辑方法并使用 @Scheduled 注解标记该方法即可。

package cn.felord.schedule.task;

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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author felord.cn
 * @since 11:02
 **/
@Component
public class TaskService {

    @Scheduled(fixedDelay = 1000)
    public void task() {
        System.out.println("Thread Name : "
                    + Thread.currentThread().getName() + "  i am a task : date ->  " 
                    + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

    }
}

请注意:@Scheduled 注解中一定要声明定时任务的执行策略 cronfixedDelayfixedRate 三选一。

我们来认识一下 @Scheduled 提供了四个属性。

3.1 cron 表达式

cron。这个我们已经在上一篇文章 详解定时任务中的 CRON 表达式 中详细介绍,这里不再赘述。

3.2 fixedDelay

fixedDelay。它的间隔时间是根据上次的任务结束的时候开始计时的,只要盯紧上一次执行结束的时间即可,跟任务逻辑的执行时间无关,两个轮次的间隔距离是固定的。

3.3 fixedRate

fixedRate。 这个相对难以理解一些。在理想情况下,下一次开始和上一次开始之间的时间间隔是一定的。但是默认情况下 Spring Boot 定时任务是单线程执行的。 当下一轮的任务满足时间策略后任务就会加入队列,也就是说当本次任务开始执行时下一次任务的时间就已经确定了,由于本次任务的“超时”执行,下一次任务的等待时间就会被压缩甚至阻塞,算了画张图就明白了。

3.4 initialDelay

  • initialDelay 初始化延迟时间,也就是第一次延迟执行的时间。这个参数对 cron 属性无效,只能配合 fixedDelayfixedRate 使用。如 @Scheduled(initialDelay=5000,fixedDelay = 1000) 表示第一次延迟 5000 毫秒执行,下一次任务在上一次任务结束后 1000 毫秒后执行。

4. Spring Task 的弊端

Spring Task 在实际应用中如果不明白一些机制会出现一些问题的,所以下面的一些要点十分重要。

4.1 单线程阻塞执行

3.3 章节 我们知道 Spring 的定时任务默认是单线程执行,多任务情况下,如果使用多线程会影响定时策略。我们来演示一下:

package cn.felord.schedule.task;

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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * The type Task service.
 *
 * @author felord.cn
 * @since 11 :02
 */
@Component
public class TaskService {


    /**
     * 上一次任务结束后 1 秒,执行下一次任务,任务消耗 5秒 
     *
     * @throws InterruptedException the interrupted exception
     */
    @Scheduled(fixedDelay = 1000)
    public void task() throws InterruptedException {
        System.out.println("Thread Name : "
                + Thread.currentThread().getName()
                + "  i am a task : date ->  "
                + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        Thread.sleep(5000);
    }

    /**
     * 下轮任务在本轮任务开始2秒后执行. 执行时间可忽略不计
     */
    @Scheduled(fixedRate = 2000)
    public void task2() {
        System.out.println("Thread Name : "
                + Thread.currentThread().getName()
                + "  i am a task2 : date ->  "
                + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }

}

上面定义了两个定时任务(策略参见注释),运行结果如下:

 Thread Name : scheduling-1  i am a task2 : date ->  2020-01-13 17:16:19
 Thread Name : scheduling-1  i am a task : date ->  2020-01-13 17:16:19
 Thread Name : scheduling-1  i am a task2 : date ->  2020-01-13 17:16:24
 Thread Name : scheduling-1  i am a task2 : date ->  2020-01-13 17:16:24
 Thread Name : scheduling-1  i am a task2 : date ->  2020-01-13 17:16:25
 Thread Name : scheduling-1  i am a task : date ->  2020-01-13 17:16:25

转换为图形比较好理解上面日志的原因:

也就是说因为单线程阻塞发生了“连锁反应”,导致了任务执行的错乱。如果你准备用定时任务打算开启 “11.11” 活动,岂不是背锅的节奏。为了不背锅我们就需要改造定时任务的机制。
@EnableScheduling 注解引入了 ScheduledAnnotationBeanPostProcessorsetScheduler(Object scheduler) 有以下的注释:

如果 TaskScheduler 或者 ScheduledExecutorService 没有定义为该方法的参数,该方法将在 Spring IoC 中寻找唯一的 TaskScheduler 或者 名称为 taskSchedulerBean 作为参数,当然你按照查找 TaskScheduler 的方法找一个ScheduledExecutorService 也可以。
要是都找不到那么只能使用本地单线程调度器了。

Spring Task 的调用顺序关系为:任务调度线程 调度 任务执行线程 执行 定时任务 所以我们按照上面定义一个 TaskSchedulerSpring Boot 自动配置中提供了 TaskScheduler 的自动配置:

@ConditionalOnClass(ThreadPoolTaskScheduler.class)
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(TaskSchedulingProperties.class)
@AutoConfigureAfter(TaskExecutionAutoConfiguration.class)
public class TaskSchedulingAutoConfiguration {

    @Bean
    @ConditionalOnBean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
    @ConditionalOnMissingBean({ SchedulingConfigurer.class, TaskScheduler.class, ScheduledExecutorService.class })
    public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) {
        return builder.build();
    }

    @Bean
    @ConditionalOnMissingBean
    public TaskSchedulerBuilder taskSchedulerBuilder(TaskSchedulingProperties properties,
            ObjectProvider<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
        TaskSchedulerBuilder builder = new TaskSchedulerBuilder();
        builder = builder.poolSize(properties.getPool().getSize());
        Shutdown shutdown = properties.getShutdown();
        builder = builder.awaitTermination(shutdown.isAwaitTermination());
        builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
        builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
        builder = builder.customizers(taskSchedulerCustomizers);
        return builder;
    }

}

该配置的自定义配置以 spring.task.scheduling 开头。 同时它需要在任务执行器配置 TaskExecutionAutoConfiguration 配置后才生效。我们只需要在中对其配置属性 spring.task.execution 相关属性配置即可。

Spring Bootapplication.properties 中相关的配置说明:

# 任务调度线程池

# 任务调度线程池大小 默认 1 建议根据任务加大
spring.task.scheduling.pool.size=1
# 调度线程名称前缀 默认 scheduling-
spring.task.scheduling.thread-name-prefix=scheduling-
# 线程池关闭时等待所有任务完成
spring.task.scheduling.shutdown.await-termination=
# 调度线程关闭前最大等待时间,确保最后一定关闭
spring.task.scheduling.shutdown.await-termination-period=


# 任务执行线程池配置

# 是否允许核心线程超时。这样可以动态增加和缩小线程池
spring.task.execution.pool.allow-core-thread-timeout=true
#  核心线程池大小 默认 8
spring.task.execution.pool.core-size=8
# 线程空闲等待时间 默认 60s
spring.task.execution.pool.keep-alive=60s
# 线程池最大数  根据任务定制
spring.task.execution.pool.max-size=
#  线程池 队列容量大小
spring.task.execution.pool.queue-capacity=
# 线程池关闭时等待所有任务完成
spring.task.execution.shutdown.await-termination=true
# 执行线程关闭前最大等待时间,确保最后一定关闭
spring.task.execution.shutdown.await-termination-period=
# 线程名称前缀 
spring.task.execution.thread-name-prefix=task-

配置完后你就会发现定时任务可以并行异步执行了。

4.2 默认不支持分布式

Spring Task 并不是为分布式环境设计的,在分布式环境下,这种定时任务是不支持集群配置的,如果部署到多个节点上,各个节点之间并没有任何协调通讯机制,集群的节点之间是不会共享任务信息的,每个节点上的任务都会按时执行,导致任务的重复执行。
我们可以使用支持分布式的定时任务调度框架,比如 Quartz、XXL-Job、Elastic Job。当然你可以借助 zookeeperredis 等实现分布式锁来处理各个节点的协调问题。或者把所有的定时任务抽成单独的服务单独部署。

5. 总结

今天我们对 Spring Task 在 Spring Boot 中的应用进行简单的了解。分析了定时任务的策略机制、对多任务串行引发的问题的分析以及如何使得多任务并行异步执行。还对分布式下定时任务的一些常用解决方案进行了列举。希望对你在使用 Spring Task 的过程中有所帮助, 原创技术干货请认准:felord.cn

评论系统未开启,无法评论!