SpringBoot(十):整合SpringTask

郎家岭伯爵 2023年07月19日 260次浏览

前言

SpringBoot 整合 SpringTask

实现

理论

SpringTask 是 Spring 提供的轻量级定时任务工具,也就意味着不需要再添加第三方依赖了,相比其他第三方类库更加方便易用。

SpringBoot

我们创建一个新的 SpringBoot 项目。

由于 SpringTask 是 Spring 提供的工具,我们无需在 pom.xml 文件中添加其它依赖。

config配置类

新建配置类 SpringTaskConfig,并添加 @EnableScheduling 注解开启 Spring Task。

package com.langjialing.springtaskdemo.task;

import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author 郎家岭伯爵
 * @time 2023/7/19 9:46
 */
@Slf4j
@Component
public class CronTask {
    @Scheduled(cron = "0/5 * * ? * ?")
    public void cron() {
        log.info("定时执行,时间{}", DateUtil.now());
    }
}

也可以不新建这个配置类,直接在启动类上添加 @EnableScheduling 注解。

package com.langjialing.springtaskdemo;

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

@SpringBootApplication
@EnableScheduling
public class SpringtaskDemoApplication {

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

}

定时任务类

新建定时任务类 CronTask,使用 @Scheduled 注解注册 Cron 表达式执行定时任务。

package com.langjialing.springtaskdemo.task;

import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author 郎家岭伯爵
 * @time 2023/7/19 9:46
 */
@Slf4j
@Component
public class CronTask {
    @Scheduled(cron = "0/5 * * ? * ?")
    public void cron() {
        log.info("定时执行,时间{}", DateUtil.now());
    }
}

启动项目,发现每隔 5 秒钟会打印一次日志,证明 SpringTaskCron 表达式形式已经起效了。

启动定时任务

增加线程

默认情况下,@Scheduled 创建的线程池大小为 1。如果想增加线程池大小的话,可以让 SpringTaskConfig 类实现 SchedulingConfigurer 接口,通过 setPoolSize 增加线程池大小。

package com.langjialing.springtaskdemo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * @author 郎家岭伯爵
 * @time 2023/7/19 9:45
 */
@Configuration
@EnableScheduling
public class SpringTaskConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();

        threadPoolTaskScheduler.setPoolSize(10);
        threadPoolTaskScheduler.setThreadNamePrefix("my-scheduled-task-pool-");
        threadPoolTaskScheduler.initialize();

        taskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
    }
}

重启项目后,控制台的日志输出:

多线程执行任务

其它类型的定时任务

SpringTask 除了支持 Cron 表达式,还有 fixedRate(固定速率执行)fixedDelay(固定延迟执行)initialDelay(初始延迟) 三种用法。

这三种定时任务的应用场景比较少,这里我们就不多说了。

总结

SpringBoot 整合 SpringTask

赞助页面示例