美文网首页
5.spring boot schedule定时任务

5.spring boot schedule定时任务

作者: 会灰的大飞狼 | 来源:发表于2017-04-12 09:03 被阅读0次

    1. spring boot的入口类Application.java中加入@EnableScheduling

    package com.yunchuang;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;
    @SpringBootApplication
    @EnableScheduling
    public class Demo8Application {
        public static void main(String[] args) {
            SpringApplication.run(Demo8Application.class, args);
        }
    }
    

    2. 定时任务类

    package com.yunchuang.schedule;
     
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
     
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
     
    /**
    * 定时器任务
    *
    * @author 尹冬飞
    * @create 2017-04-12 8:24
    */
    @Component
    public class Jobs {
     
        @Scheduled(fixedDelay = 1000)
        public void job1(){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
            System.out.println(sdf.format(new Date()));
        }
        @Scheduled(cron = "0 10 3 ? * 1#3")
        public void job2(){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
            System.out.println(sdf.format(new Date()));
        }
    }
    

    相关文章

      网友评论

          本文标题:5.spring boot schedule定时任务

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