Spring Boot文档阅读笔记-Scheduling Tasks

Posted IT1995

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot文档阅读笔记-Scheduling Tasks相关的知识,希望对你有一定的参考价值。

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

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

首先在Maven中增加awaitility依赖包

<dependency>
  <groupId>org.awaitility</groupId>
  <artifactId>awaitility</artifactId>
  <version>3.1.2</version>
  <scope>test</scope>
</dependency>

整体Maven如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>SchedulingTasks</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.awaitility</groupId>
            <artifactId>awaitility</artifactId>
            <version>3.1.2</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

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

package cn.it1995.component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class ScheduledTasks 

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedDelay = 5000)
    public void reportCurrentTime()

        log.info("The time is now ", dateFormat.format(new Date()));
    

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

package cn.it1995;

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

@SpringBootApplication
@EnableScheduling
public class Main 

    public static void main(String[] args)

        SpringApplication.run(Main.class, args);
    

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

代码如下:Main.java

package cn.it1995;

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

@SpringBootApplication
@EnableScheduling
public class Main 

    public static void main(String[] args)

        SpringApplication.run(Main.class, args);
    

运行截图如下:

代码打包如下:

Java/SchedulingTasks at master · fengfanchen/Java · GitHub

 

以上是关于Spring Boot文档阅读笔记-Scheduling Tasks的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot文档阅读笔记-Scheduling Tasks

Spring Boot文档阅读笔记-exception handling–@ExceptionHandler解析与实例

Spring Boot文档阅读笔记-CORS Support

Spring Boot文档阅读笔记-CORS Support

Spring Boot文档阅读笔记-CORS Support

Spring Boot文档阅读笔记-how-to-implement-2-way-ssl-using-spring-boot