欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springBoot定时任务处理类的实现代码

程序员文章站 2023-12-17 11:44:34
首先在启动类上添加注解:@enablescheduling 来开启定时任务 @springbootapplication @enablescheduling...

首先在启动类上添加注解:@enablescheduling 来开启定时任务

@springbootapplication
@enablescheduling
public class application {
  public static void main(string[] args) {
   springapplication.run(application.class, args);
  }
}

然后新建定时任务类

@component
public class quartzservice {
  /**
   * 通过时间表达式执行定时任务
   */
  @scheduled(cron = "0 0/1 * * * ?")
  public void timertonow(){
    system.out.println("now time:" + new simpledateformat("yyyy-mm-dd hh:mm:ss").format(new date()));
  }
  /**
   *启动时间点之后 x毫秒秒执行一次
   */
  @scheduled(fixedrate = 5000)
  public void timertozzp(){
    system.out.println("fixedrate:" + new random().nextlong() + new simpledateformat("hh:mm:ss").format(new date()));
  }
  /**
   * 结束时间点之后 每x毫秒执行一次
   */
  @scheduled(fixeddelay = 10000)
  public void timertoreportcount(){
    system.out.println("fixeddelay:" + new random().nextlong() + new simpledateformat("hh:mm:ss").format(new date()));
  }
  /**
   * 第一次延迟 x毫秒执行,之后按照fixedrate的规则每x毫秒执行
   */
  @scheduled(initialdelay = 10000,fixedrate = 6000)
  public void timertoreport(){
    system.out.println("initialdelay:" + new random().nextlong() + new simpledateformat("hh:mm:ss").format(new date()));
  }
}

启动项目,定时任务开始

总结

以上所述是小编给大家介绍的springboot定时任务处理类的实现代码,希望对大家有所帮助

上一篇:

下一篇: