加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 19:48:05 所属栏目:Java 来源:网络整理
导读:首先在启动类上添加注解:@EnableScheduling 来开启定时任务 @SpringBootApplication@EnableSchedulingpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); }} 然后新建定时任务类 @Compo

首先在启动类上添加注解:@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定时任务处理类的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

您可能感兴趣的文章:

  • spring-boot通过@Scheduled配置定时任务及定时任务@Scheduled注解的方法
  • SpringBoot 定时任务遇到的坑
  • 详解SpringBoot 创建定时任务(配合数据库动态执行)
  • springboot整合Quartz实现动态配置定时任务的方法
  • 详解Spring Boot 定时任务的实现方法
  • springboot整合quartz实现定时任务示例
  • SpringBoot定时任务两种(Spring Schedule 与 Quartz 整合 )实现方法
  • 详解Spring Boot中使用@Scheduled创建定时任务
  • 详解SpringBoot定时任务说明

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读