【Spring】使用 @Schedule 完成定时任务

在Spring框架中,使用Spring Schedule可以很方便地创建定时任务。以下是一个使用Spring Schedule完成定时任务的DEMO:引入Spring Boot依赖&#x

在Spring框架中,使用Spring Schedule可以很方便地创建定时任务。以下是一个使用Spring Schedule完成定时任务的DEMO:

  1. 引入Spring Boot依赖:在pom.xml文件中添加Spring Boot Starter依赖,这会自动包含Spring Scheduling。
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 启用定时任务支持:在Spring Boot应用程序主类上添加@EnableScheduling注解。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class ScheduledTasksApplication {
    public static void main(String[] args) {
        SpringApplication.run(ScheduledTasksApplication.class, args);
    }
}
  1. 创建定时任务:创建一个带有@Scheduled注解的方法来定义任务。
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
public class ScheduledTasks {

    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void scheduleTaskWithFixedRate() {
        System.out.println("固定时间任务 :: 执行时间 - " + dateTimeFormatter.format(LocalDateTime.now()));
    }

    @Scheduled(fixedDelay = 5000)
    public void scheduleTaskWithFixedDelay() {
        System.out.println("固定延迟任务 :: 执行时间 - " + dateTimeFormatter.format(LocalDateTime.now()));
    }

    @Scheduled(cron = "0 * * * * ?")
    public void scheduleTaskWithCronExpression() {
        System.out.println("计划任务 :: 执行时间 - " + dateTimeFormatter.format(LocalDateTime.now()));
    }
}

在上面的代码中:

  • @Scheduled(fixedRate = 5000):每5秒执行一次任务,不管上一次任务是否完成。
  • @Scheduled(fixedDelay = 5000):在上一次任务完成后等待5秒再执行下一次任务。
  • @Scheduled(cron = "0 * * * * ?"):使用Cron表达式在每分钟的第0秒执行任务。

完整的示例代码如下:

pom.xml

<project xmlns="http://maven.apache/POM/4.0.0"
         xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache/POM/4.0.0 http://maven.apache/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>spring-scheduled-tasks</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

ScheduledTasksApplication.java

package com.example.scheduledtasks;

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

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

ScheduledTasks.java

package com.example.scheduledtasks;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
public class ScheduledTasks {

    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void scheduleTaskWithFixedRate() {
        System.out.println("固定时间任务 :: 执行时间 - " + dateTimeFormatter.format(LocalDateTime.now()));
    }

    @Scheduled(fixedDelay = 5000)
    public void scheduleTaskWithFixedDelay() {
        System.out.println("固定延迟任务 :: 执行时间 - " + dateTimeFormatter.format(LocalDateTime.now()));
    }

    @Scheduled(cron = "0 * * * * ?")
    public void scheduleTaskWithCronExpression() {
        System.out.println("计划任务 :: 执行时间 - " + dateTimeFormatter.format(LocalDateTime.now()));
    }
}

运行应用程序:

运行ScheduledTasksApplication主类,控制台输出定时任务的执行时间:

固定时间任务 :: 执行时间 - 2024-05-20 12:00:00
固定延迟任务 :: 执行时间 - 2024-05-20 12:00:05
计划任务 :: 执行时间 - 2024-05-20 12:01:00
...

这样完成了一个包含定时任务的程序。

发布者:admin,转转请注明出处:http://www.yc00.com/web/1755000092a5225445.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信