SpringBoot配置定时任务:@Schedule注解的使用

直接演示:分为两步:一、编写定时任务方法,并加上注解@Schedule

二、启动类开启@EnableScheduling注解。测试:

定时任务的方法:

@Component
public class ScheduledTest {

    //使用固定间隔执行任务
    //每两秒钟执行一次
    @Scheduled(fixedRate = 2000)
    public void reportCurrentTime(){
        System.out.println("每2秒更新一次");
    }

    public long secondTime(){
        return System.currentTimeMillis();
    }

    //使用固定延迟执行任务
    //每两秒钟执行一次
    @Scheduled(fixedDelay = 2000)
    public void performDelayedTask(){
        System.out.println("固定延迟执行任务:");
    }

    //Cron表达式
    //每分钟执行一次
    @Scheduled(cron = "0 0/1 * * * ?")
    public void performCron(){
        System.out.println("Cron间隔是");
    }
}

启动类:

@SpringBootApplication
@EnableScheduling
public class Run {
    public static void main(String[] args) {
        SpringApplication.run(Run.class);
    }
}

测试:

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值