springboot定时任务自学之路-02(使用cron表达式完成复杂的定时任务)

目录

1.应用代码

2.cron表达式简单介绍


1.应用代码

package com.test.springboot_schedule;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@EnableScheduling
@SpringBootApplication
public class SpringbootScheduleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootScheduleApplication.class, args);
    }

    //    @Scheduled(fixedRate = 30 * 1000)
    @Scheduled(cron = "0/30 * * * * ?")
    public void saySth() {

        System.out.println(Thread.currentThread().getName() + "--say hello world");

    }

    //    @Scheduled(fixedRate = 60 * 1000)
    @Scheduled(cron = "0 0/1 * * * ?")
    public void doSth() {

        System.out.println(Thread.currentThread().getName() + "--do hello world");

    }
}

2.cron表达式简单介绍

cron表达式总共有七位分别是 秒 分 时 日 月 周 年

但是spring只支持到6位 秒 分 时 日 月 周 不支持年,所以我们如果使用spring写cron表达式的时候只能写6位;

下面说一下cron表达式,如果比较简单的,可以直接在网上找一个在线生成器生成:在线Cron表达式生成器

下面简单说说cron表达式的几个常用符号

,  表示枚举,表示在这几个中的时间才会发生定时 比如:0 20,30 * * * ? 表示每天的每个小时的20分和30分时候回执行定时

- 表示从几顺序到几的时间才会发生定时 比如:0 20-30 * * * ? 表示每天每个小时的20分到30分直接每分钟都会执行定时

/ 表示每隔几个时间才会发生定时 比如:0 0/30 * * * ? 表示每天每个小时每隔30分钟执行定时

* 表示匹配该领域的任务时间值 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值