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

Springboot 定时任务 @EnableScheduling @Scheduled

`@EnableScheduling` 是Spring框架中的一个注解,它用于开启基于注解的任务调度支持。当你在你的Spring应用程序中使用这个注解时,它允许你通过`@Scheduled`注解来配置和执行定时任务。

以下是如何使用 `@EnableScheduling` 的基本步骤:

1. **添加@EnableScheduling注解**:
   在你的Spring Boot启动类或者配置类上添加`@EnableScheduling`注解,以开启定时任务的支持。
   ```java
   import org.springframework.scheduling.annotation.EnableScheduling;
   import org.springframework.boot.SpringApplication;
   import org.springframework.boot.autoconfigure.SpringBootApplication;

   @SpringBootApplication
   @EnableScheduling
   public class Application {

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

2. **创建定时任务方法**:
   在你的组件中创建方法,并使用`@Scheduled`注解来配置任务的执行计划。
   ```java
   import org.springframework.scheduling.annotation.Scheduled;
   import org.springframework.stereotype.Component;

   @Component
   public class ScheduledTasks {

       // 这个任务将每5秒执行一次
       @Scheduled(fixedRate = 5000)
       public void reportCurrentTime() {
           System.out.println("当前时间: " + new Date());
       }
   }
   ```

3. **配置定时任务**:
   `@Scheduled`注解有多个参数可以用来配置任务的执行计划,包括:
   - `fixedRate`:在指定的时间间隔后运行。
   - `initialDelay`:在指定的延迟之后开始执行。
   - `cron`:使用cron表达式配置执行计划。

   ```java
   // 使用cron表达式配置任务
   @Scheduled(cron = "0 * * * * *") // 每秒执行一次
   public void scheduledTaskWithCronExpression() {
       // 任务逻辑
   }
   ```

4. **配置任务执行器**(可选):
   如果你需要自定义任务执行器(例如,指定线程池大小),你可以定义一个`TaskExecutor`的Bean,并使用`@Configuration`注解。
   ```java
   import org.springframework.context.annotation.Bean;
   import org.springframework.context.annotation.Configuration;
   import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

   @Configuration
   public class TaskExecutorConfig {

       @Bean
       public ThreadPoolTaskExecutor taskExecutor() {
           ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
           executor.setCorePoolSize(5);
           executor.setMaxPoolSize(10);
           executor.setQueueCapacity(25);
           executor.setThreadNamePrefix("Scheduled-Executor-");
           return executor;
       }
   }
   ```

5. **运行应用程序**:
   运行你的Spring Boot应用程序,定时任务将根据你的配置开始执行。

请注意,使用`@EnableScheduling`时,确保应用程序有足够的权限来执行定时任务,并且在生产环境中,合理配置线程池大小以避免资源耗尽。此外,`@Scheduled`注解的任务默认是在应用程序的主线程中执行的,如果任务执行时间较长,可能需要异步执行以避免阻塞主线程。
 

http://www.lryc.cn/news/421263.html

相关文章:

  • STM32F407ZET6使用LCD(9341)
  • 动手学深度学习7.3 网络中的网络(NiN)-笔记练习(PyTorch)
  • SQL语言-select的使用方法
  • 深入理解Python中的排序算法:快速排序与归并排序实现
  • Linux基础命令 ② 未完成
  • 怎么加密文件?分享文件加密四个方法,2024新版操作教程,教你搞定!
  • cesium加载魔方立方体
  • unity 粒子系统学习
  • CogVideoX环境搭建推理测试
  • STL—容器—list【list的介绍和基本使用】【list的迭代器失效问题】
  • 【面试宝典】MySQL 面试问题
  • 【Cpp筑基】三、对象和类
  • 数据库原理面试-核心概念-问题理解
  • 【JavaScript】JavaScript里的“先斩后奏”之王 shift()方法
  • Python面试宝典第32题:课程表
  • 简单介绍BTC的Layer2项目RGB
  • 跨境电商卖家必看:搭建安全稳定测评自养号环境系统
  • 如何对open62541.h/open62541.c的UA_Client进行状态(在线/掉线)监控
  • 高等数学 第九讲 一元函数积分学的应用
  • django如何更新数据库字段并与数据库保持同步?
  • jenkins插件 SSH Publishers
  • Kafka Client客户端操作详解
  • 【HarmonyOS NEXT星河版开发学习】小型测试案例15-博客列表
  • go-zero中统一返回前端数据格式的几种方式
  • 【向量数据库】Ubuntu编译安装FAISS
  • 制造知识普及(九)--企业内部物料编码(IPN)与制造商物料编码(MPN)
  • 【整数规划】+【0—1规划】解决优化类问题(Matlab代码)
  • Linux下如何使用Curl进行网络请求
  • PostgreSQL 触发器
  • LeetCode——3131.找出与数组相加的整数I