• Spring Boot文档阅读笔记-Scheduling Tasks


    这篇博文将说明,如何一步一步完成Spring的任务调度功能。

    主要是使用@Scheduled注解,完成每5秒打印当前时间。

    首先在Maven中增加awaitility依赖包

    1. <dependency>
    2. <groupId>org.awaitilitygroupId>
    3. <artifactId>awaitilityartifactId>
    4. <version>3.1.2version>
    5. <scope>testscope>
    6. dependency>

    整体Maven如下:

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <groupId>org.examplegroupId>
    7. <artifactId>SchedulingTasksartifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <dependencies>
    10. <dependency>
    11. <groupId>org.springframework.bootgroupId>
    12. <artifactId>spring-boot-starter-webartifactId>
    13. <version>2.1.10.RELEASEversion>
    14. dependency>
    15. <dependency>
    16. <groupId>org.awaitilitygroupId>
    17. <artifactId>awaitilityartifactId>
    18. <version>3.1.2version>
    19. <scope>testscope>
    20. dependency>
    21. dependencies>
    22. project>

    创建调度调度任务的类,ScheduleTasks.java

    1. package cn.it1995.component;
    2. import org.slf4j.Logger;
    3. import org.slf4j.LoggerFactory;
    4. import org.springframework.scheduling.annotation.Scheduled;
    5. import org.springframework.stereotype.Component;
    6. import java.text.SimpleDateFormat;
    7. import java.util.Date;
    8. @Component
    9. public class ScheduledTasks {
    10. private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
    11. private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    12. @Scheduled(fixedDelay = 5000)
    13. public void reportCurrentTime(){
    14. log.info("The time is now {}", dateFormat.format(new Date()));
    15. }
    16. }

    @Scheduled注解说明了在合适调用,比如fixedDelay说明每多少毫秒调用一次。这里还有其他选项,比如使用corn,这里全称应该是crontab,也就是定时任务,如@Scheduled(cron="...")

    1. package cn.it1995;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.scheduling.annotation.EnableScheduling;
    5. @SpringBootApplication
    6. @EnableScheduling
    7. public class Main {
    8. public static void main(String[] args){
    9. SpringApplication.run(Main.class, args);
    10. }
    11. }

    最后在@SpringBootApplication注解的地方,也就是main方法的地方,加上@EnableScheduling注解,这个注解的作用是使得后台任务调用执行被创建。

    代码如下:Main.java

    1. package cn.it1995;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.scheduling.annotation.EnableScheduling;
    5. @SpringBootApplication
    6. @EnableScheduling
    7. public class Main {
    8. public static void main(String[] args){
    9. SpringApplication.run(Main.class, args);
    10. }
    11. }

    运行截图如下:

    代码打包如下:

    Java/SchedulingTasks at master · fengfanchen/Java · GitHub

     

  • 相关阅读:
    USB Type-C详细介绍
    redis的知识
    走进HBase
    世界互联网大会|美创科技新一代 灾备一体化平台(DRCC v3.0)重磅亮相
    nginx.conf配置
    Python模拟登录豆瓣:轻松探索海量文化资源!
    [网鼎杯 2020 青龙组]AreUSerialz
    Servlet API Cookie和Session
    【无标题】
    【Mysql】Mysql基本概念和操作
  • 原文地址:https://blog.csdn.net/qq78442761/article/details/128180636