springBoot配置定时任务 @Scheduled

 

 

 

新知识点,@Scheduled,

作用:可以通过@Scheduled配置定时任务。

 

 1 package com.example.demo.scheduled;
 2 
 3 import java.time.LocalDateTime;
 4 import java.time.format.DateTimeFormatter;
 5 
 6 import org.springframework.scheduling.annotation.EnableScheduling;
 7 import org.springframework.scheduling.annotation.Scheduled;
 8 import org.springframework.stereotype.Component;
 9 
10 @Component
11 @EnableScheduling
12 public class scheduleTask {
13 
14     private DateTimeFormatter ofPattern;
15 
16     @Scheduled(cron = "0/5 * * * * ?")
17     public void task() {
18         LocalDateTime now = LocalDateTime.now();
19         ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
20         System.out.println(now.format(ofPattern)+"it is task");
21     }
22 }

展示的结果:

 

备注:

@Component,把类注入spring的上下文。

@EnableScheduling,开启定时任务

@Scheduled,配置需要定时执行的任务,除了支持灵活的参数表达式cron之外,还支持简单的延时操作,例如 fixedDelay ,fixedRate 填写相应的毫秒数即可。

其中cron参数详细信息如下:

Cron表达式参数分别表示:

  • 秒(0~59) 例如0/5表示每5秒
  • 分(0~59)
  • 时(0~23)
  • 日(0~31)的某天,需计算
  • 月(0~11)
  • 周几( 可填1-7 或 SUN/MON/TUE/WED/THU/FRI/SAT)

转载于:https://www.cnblogs.com/smartFox/p/11349246.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值