1.依赖文件

文件名:pom.xml

文件内容:

         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">

    4.0.0


    com.huinongtx.springboot

    demoschedulingtasks

    1.0-SNAPSHOT


   

        org.springframework.boot

        spring-boot-starter-parent

        2.0.5.RELEASE

   


   

       

            org.springframework.boot

            spring-boot-starter

       

   


   

       

           

                org.springframework.boot

                spring-boot-maven-plugin

           

       

   

2.业务类

包名:com.huinong.springboot.hello

类名:ScheduledTasks

类内容:

package com.huinong.springboot.hello;

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;

/**

* Created by dengdashuai on 2018/12/6.

*/

@Component

public class ScheduledTasks {

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


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


    // 5秒执行一次该方法

    @Scheduled(fixedRate = 5000)

    public void reportCurrentTime(){

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

    }

}

3.应用入口文件

包名:com.huinong.springboot.hello

类名:Application

类内容:

package com.huinong.springboot.hello;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.scheduling.annotation.EnableScheduling;

/**

* Created by dengdashuai on 2018/12/6.

*/

@SpringBootApplication

// 开启任务调度

@EnableScheduling

public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class);

    }

}

4.项目目录结构截图

spring boot 任务调度_第1张图片

5.运行结果截图

spring boot 任务调度_第2张图片

spring boot 任务调度_第3张图片