首页 > 其他分享 >Spring Boot应用中添加定时任务

Spring Boot应用中添加定时任务

时间:2024-06-19 15:32:58浏览次数:9  
标签:Scheduled Spring Boot springframework org 定时

下面是一个使用@EnableScheduling的完整解决方案,包括了如何在Spring Boot应用中设置定时任务以及如何编写和管理这些任务。

步骤 1: 添加依赖

确保你的pom.xmlbuild.gradle中包含了Spring Boot Starter Web和Spring Boot Starter AOP的依赖,因为@Scheduled注解的执行依赖于AOP(面向切面编程)。

pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
</dependencies>

步骤 2: 在主类上启用定时任务

在你的Spring Boot主启动类上添加@EnableScheduling注解。

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

@SpringBootApplication
@EnableScheduling
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

步骤 3: 创建定时任务

接下来,创建一个服务类,并在其中定义你的定时任务。你可以使用@Scheduled注解来标记需要定时执行的方法。

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

@Service
public class MyScheduledService {

    // 每天凌晨1点执行
    @Scheduled(cron = "0 1 * * * ?")
    public void dailyTask() {
        System.out.println("Daily task executed at 1 AM.");
    }

    // 每隔5秒执行一次
    @Scheduled(fixedRate = 5000)
    public void everyFiveSeconds() {
        System.out.println("Task executed every 5 seconds.");
    }
}

步骤 4: 测试定时任务

启动你的Spring Boot应用,然后观察控制台输出,确保定时任务按预期执行。你可以使用日志框架(如Logback或Log4j)来记录定时任务的执行情况,以便于监控和调试。

注意事项:

  • 确保你的定时任务服务类是Spring容器管理的bean,即使用了@Service@Component或其他Spring注解。
  • 调整@Scheduled注解的参数以满足你的需求,例如使用cron表达式来指定复杂的执行时间,或使用fixedDelayfixedRate来设置固定的延迟或速率。
  • 如果你的应用运行在集群环境中,记得考虑如何避免同一任务在多台服务器上重复执行的问题。

通过以上步骤,你可以在Spring Boot应用中成功实现并管理定时任务。

标签:Scheduled,Spring,Boot,springframework,org,定时
From: https://blog.csdn.net/www3300300/article/details/139803374

相关文章

  • Spring WebSocket中关于WebSocket配置类的注意事项
    情况1:如果只需要进行简单的通信,不需要消息代理和STOMP协议支持,那么只需要实现WebSocketConfigurer接口注意:实现的接口是WebSocketConfigurer,使用的注解是@EnableWebSocketimportorg.springframework.context.annotation.Configuration;importorg.springframework.web.socke......
  • spring 使用 事件机制
    概述在编写代码的时候,比如我删除一篇文章,这个时候,如果我想做些额外的逻辑,这是就需要修改删除部分的代码。spring提供了事件机制更优雅的实现这个,用户只需要实现事件监听即可。代码实现注入发布者publicclassKnowledgeBaseServiceimplementsApplicationEventPublisherAwar......
  • 封装定时器方法
    需求:查询的历史数据需要定时3分钟刷新(产品提的要求照做!!!)//周期性地执行指定的回调函数,并在组件销毁时清除该定时器,以防止内存泄漏或不必要的回调执行exportconsttimmingLoadingsTime=(callback,time)=>{constrollertimer2=ref(null);rollertimer2.value=se......
  • [com.t.extend.SpringContextLoaderListener] - generate index.html sucess,ERROR or
    错误:2024-06-1913:23:09,873INFO[com.t.extend.SpringContextLoaderListener]-generateindex.htmlsucess13:23:10.159[RMITCPConnection(3)-127.0.0.1]ERRORorg.apache.struts2.dispatcher.Dispatcher-Dispatcherinitializationfailedcom.opensymphony.xwor......
  • delphi:利用定时器读取串口返回数据
    定时器20毫秒运行一次,单字符读取,如果读取到就保存到全局变量receData中,否则就输出到文本框中,并重置receData。优点:单字符读取,解决了按长度读取的弊端,如果按长度读取,很多时候并不知道究竟要读取多长,有的时候能读取完整,有的时候只读取了部分。procedureTfrmLC.tmrReceDataTimer(S......
  • Springboot 集成 Shardingsphere-JDBC
    Springboot集成Shardingsphere-JDBCShardingsphere系列目录:背景调研前提新增依赖分表策略简单分库分表策略垂直分库广播表水平分库(单表)水平分库(多表)水平分表HINT配置逻辑代码自定义分库分表(精准定位+范围查询)配置代码精准定位数据库精准定位+范围查询表代码仓......
  • SpringData初步学习-连接MySQL数据库
    1.添加mysql驱动和spring-data-jpa依赖<dependencies><!--SpringDataJPA--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId><......
  • java spring-data-jpa 使用方法
    SpringDataJPA是Spring生态系统的一部分,它提供了对JPA(JavaPersistenceAPI)的抽象,简化了数据访问层的开发。以下是使用SpringDataJPA的基本步骤和一些核心概念:1.添加依赖        在Maven项目的pom.xml文件中添加SpringDataJPA和相关数据库驱......
  • 基于SpringBoot+Vue的高校爱心捐赠系统设计与实现(源码+lw+部署+讲解)
    文章目录前言详细视频演示具体实现截图技术可行性分析技术简介后端框架SpringBoot前端框架Vue系统开发平台系统架构设计业务流程分析为什么选择我们自己的公众号(一点毕设)海量实战案例代码参考数据库参考源码及文档获取前言......
  • springboot框架怎么用?是什么?
    SpringBoot框架是一个用于简化Spring应用初始搭建以及开发过程的全新框架,旨在通过特定的配置方式,使开发人员不再需要定义样板化的配置,从而加速应用开发。以下是关于SpringBoot框架的详细使用和介绍:1.SpringBoot框架的使用安装Java开发环境:首先,确保已经正确配置了JDK。......