继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

spring boot 任务调度

holdtom
关注TA
已关注
手记 1703
粉丝 240
获赞 991


1.依赖文件

    文件名:pom.xml

    文件内容:

        <?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>com.huinongtx.springboot</groupId>

            <artifactId>demoschedulingtasks</artifactId>

            <version>1.0-SNAPSHOT</version>

            <parent>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-parent</artifactId>

                <version>2.0.5.RELEASE</version>

            </parent>

            <dependencies>

                <dependency>

                    <groupId>org.springframework.boot</groupId>

                    <artifactId>spring-boot-starter</artifactId>

                </dependency>

            </dependencies>

            <build>

                <plugins>

                    <plugin>

                        <groupId>org.springframework.boot</groupId>

                        <artifactId>spring-boot-maven-plugin</artifactId>

                    </plugin>

                </plugins>

            </build>

        </project>

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.项目目录结构截图

4.png

5.运行结果截图

1.png

QQ截图20181206153448.png

©著作权归作者所有:来自51CTO博客作者素颜猪的原创作品,如需转载,请注明出处,否则将追究法律责任

\


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP