美文网首页
springboot定时任务自学

springboot定时任务自学

作者: 然也翰飞 | 来源:发表于2018-10-07 13:23 被阅读0次

    PrintTask 

    import org.springframework.scheduling.annotation.Scheduled;

    import org.springframework.stereotype.Component;

    import java.util.Date;

    @Component

    public class PrintTask {

    /**

    * 每小时的10分执行该方法

    */

    //    @Scheduled(cron = "0 10 * * * *")

    /**

    *每隔3秒执行该方法

    */

        @Scheduled(cron =" */3 * * * * *")

    public void cron()throws Exception{

    System.out.println("执行测试cron时间:"+new Date(System.currentTimeMillis()));

    }

    /**

    * 是上一个调用开始后再次调用的延时(不用等待上一次调用完成)

    */

        @Scheduled(fixedRate =1000 *1)

    public void fixedRate()throws Exception

    {

    Thread.sleep(2000);

    System.out.println("执行测试fixedRate时间:"+new Date(System.currentTimeMillis()));

    }

    /**

    * 上一个调用完成后再次调用的延时调用

    */

    //@Scheduled(fixedDelay = 1000 * 1)

        public void fixedDelay()throws Exception

    {

    Thread.sleep(3000);

    System.out.println("执行测试fixedDelay时间:"+new Date(System.currentTimeMillis()));

    }

    /**

    * 第一次被调用前的延时,单位毫秒

    */

        @Scheduled(initialDelay =1000 *10,fixedDelay =1000 *2)

    public void initialDelay()throws Exception

    {

    System.out.println("执行测试initialDelay时间:"+new Date(System.currentTimeMillis()));

    }

    }

    TimerApplication

    package com.example.timer;

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import org.springframework.scheduling.annotation.EnableScheduling;

    @SpringBootApplication

    @EnableScheduling

    public class TimerApplication {

    public static void main(String[] args) {

    SpringApplication.run(TimerApplication.class, args);

    }

    }

    相关文章

      网友评论

          本文标题:springboot定时任务自学

          本文链接:https://www.haomeiwen.com/subject/xldlaftx.html