zoukankan      html  css  js  c++  java
  • 定时任务-----Springboot中使用Scheduled做定时任务----http://www.cnblogs.com/lirenqing/p/6596557.html

    Springboot中使用Scheduled做定时任务---http://www.cnblogs.com/lirenqing/p/6596557.html

    已经验证的方案:

    pom文件加入依赖

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
    </dependency>

    ExampleTimer.java

    复制代码
    package com.example;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ExampleTimer {
         SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    
            @Scheduled(fixedRate = 10000)
            public void timerRate() {
                System.out.println(dateFormat.format(new Date()));
            }
            
            //第一次延迟1秒执行,当执行完后2秒再执行
            @Scheduled(initialDelay = 1000, fixedDelay = 2000)
            public void timerInit() {
                System.out.println("init : "+dateFormat.format(new Date()));
            }
    
            //每天20点16分50秒时执行
            @Scheduled(cron = "50 16 20 * * ?")
            public void timerCron() {
                System.out.println("current time : "+ dateFormat.format(new Date()));
            }
    }

    3、启动应用程序 

    启动程序,需要增加@EnableScheduling注解.

    SpringBootScheduledApplication.java

  • 相关阅读:
    如何搭建企业级中台系统
    Linux上安装git
    Jenkins的CI持续集成
    docker安装jenkins
    在线思维导图网站
    K8s容器编排
    MySQL存储引擎
    tomcat8 进入不了Manager App 界面 403 Access Denied
    IdeaVim-常用操作
    Node.js 安装及环境配置之 Windows 篇
  • 原文地址:https://www.cnblogs.com/czlovezmt/p/9042373.html
Copyright © 2011-2022 走看看