当前位置: 首页 > news >正文

Spring框架自带的定时任务:Spring Task详解

文章目录

  • 一、基本使用
    • 1、配置:@EnableScheduling
    • 2、触发器:@Scheduled
  • 二、拓展
    • 1、修改默认的线程池
    • 2、springboot配置
  • 三、源码分析
  • 参考资料

一、基本使用

1、配置:@EnableScheduling

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
// 可以省略
@EnableAsync
// 开启定时任务
@EnableScheduling
public class SchedulingConfiguration {
}

2、触发器:@Scheduled

注意:
1、要调度的方法必须有void返回,并且不能接受任何参数。
2、@Scheduled可用作可重复的注释。如果在同一个方法上发现了几个@Scheduled注解,那么它们中的每一个都将被独立处理,并为它们中的每一个触发一个单独的触发器。

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableAsync
@EnableScheduling
public class SchedulingConfiguration {


    /**
     * 上一次调用结束和下一次调用开始之间的固定时间内执行
     * 也可以指定时间类型,默认是毫秒
     * @Scheduled(fixedDelay = 5, timeUnit = TimeUnit.SECONDS)
     */
    @Scheduled(fixedDelay = 5000)
    public void doSomething() {
        System.out.println("每5秒触发一次");
    }


    /**
     * 以固定的时间间隔执行
     */
    @Scheduled(fixedRate = 5, timeUnit = TimeUnit.SECONDS)
    public void doSomething2() {
        System.out.println("每5秒执行一次2");
    }

    /**
     * 第一次延迟1秒执行,之后每隔5秒执行一次
     */
    @Scheduled(initialDelay = 1000, fixedRate = 5000)
    public void doSomething3() {
        // something that should run periodically
    }

    /**
     * 延迟1秒执行,一次性任务
     */
    @Scheduled(initialDelay = 1000)
    public void doSomething4() {
        // something that should run only once
    }

    /**
     * cron表达式
     */
    @Scheduled(cron="*/5 * * * * MON-FRI")
    public void doSomething5() {
        // something that should run on weekdays only
    }


}

二、拓展

1、修改默认的线程池

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configuration
public class TaskSchedulerConfig {

	// bean名称一定要是taskScheduler
    @Bean
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        // 设置线程池大小
        scheduler.setPoolSize(5); 
        // 设置线程名称前缀
        scheduler.setThreadNamePrefix("my-scheduler-"); 
        // 设置任务拒绝策略
        scheduler.setRejectedExecutionHandler((r, executor) -> {
            System.err.println("Task " + r.toString() + " rejected from " + executor.toString());
        });
        // 初始化调度器
        scheduler.initialize(); 
        return scheduler;
    }
}

2、springboot配置

springboot的配置:修改线程池大小等

spring.task.scheduling.pool.size=5
spring.task.scheduling.thread-name-prefix=config-scheduler-

三、源码分析

springboot默认会自动配置,创建一个ThreadPoolTaskScheduler:
但是默认的线程池的PoolSize是1!!!这是个坑,需要注意。
在这里插入图片描述
在这里插入图片描述

参考资料

https://docs.spring.io/spring-framework/reference/integration/scheduling.html

相关文章:

  • 【力扣】5.最长回文子串
  • C++初阶——入门基础2
  • Observability:使用 Elastic Agent 跟踪你的 Steam Deck 游戏
  • [杂学笔记] 封装、继承、多态,堆和栈的区别,堆和栈的区别 ,托管与非托管 ,c++的垃圾回收机制 , 实现一个单例模式 注意事项
  • 从零开始创建一个浏览器:技术挑战与实现路径
  • vmware centos 挂载windows 文件目录
  • Windows 10 下 SIBR Core (i.e. 3DGS SIBR Viewers) 的编译
  • 【对话推荐系统综述】A Survey on Conversational Recommender Systems
  • MySQL执行更新SQL流程
  • 如何将飞书多维表格与DeepSeek R1结合使用:效率提升的完美搭档
  • 电子电气架构 --- AI在整车产品领域的应用
  • springboot + mybatis-plus + druid
  • 【随手笔记】利尔达NB模组
  • 服务器配置-从0到分析4:ssh免密登入
  • 软考网络安全口诀
  • 紫光无人机AI飞控平台2.0——航线管理模块
  • Qt上位机编程命名规范-执行版
  • 1.从0搭建前端Vue项目工程
  • C++设计一:日期类Date实现
  • 使用Python或R语言重新拟合模型
  • 太原网站制作哪家好/最近新闻大事件
  • 做爰午夜福利全过程视频网站/免费b2b网站推广
  • 电子商务网站建设试题答案/常州seo
  • 青海哪家做网站的公司最大/上海seo推广
  • 培训网站开发流程/网页模板免费下载
  • 网站ip域名查询/搜索引擎yandex入口