springboot跑定时任务

使用@Scheduled注解实现

1、在启动类上加上@EnableScheduling 开启定时任务

2、新建一个任务类,在方法上添加@Scheduled注解

@Component
public class CostStateTask {

@Scheduled(cron="0 19 9 * * ?")
public void test(){
System.out.println("我是定时任务");
}

}
解析:@Scheduled注解有多种使用方式
常用的是
  1、fixedRate(5000)-------------------5000毫秒执行一次
  2、cron="0 19 9 * * ?" -----------------每天9点19分执行一次
其中

一个cron表达式有多个

按顺序依次为

  •  秒(0~59)
  •  分钟(0~59)
  •  小时(0~23)
  •  天(月)(0~31,但是你需要考虑你月的天数)
  •  月(0~11)
  •  天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
  •  年份(1970-2099)
至于其他的时间表达式,可阅读下面的连接
参考:http://www.cnblogs.com/eoooxy/p/7099320.html
 

转载于:https://www.cnblogs.com/memoa/p/10314025.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot中注册定时任务有多种方式。一种方式是通过使用注解的方式来定义定时任务。首先,在主启动类上添加@EnableScheduling注解,开启定时任务的功能。然后在需要执行定时任务的方法上使用@Scheduled注解,设定任务执行的频度,例如cron表达式。这样,在指定的时间点,该方法就会被自动执行。可以参考如下代码示例: ```java @Component public class MyTask { // 每月每日每小时分钟每20秒执行一次 @Scheduled(cron = "0/20 * * * * ?") public void testMyTask(){ System.out.println("SpringBoot Task 定时任务执行了..."); } } ``` 另一种方式是通过配置文件来注册定时任务。在application.yml或application.properties中添加相关配置来设置任务调度器的属性,包括核心线程数、线程名称前缀等。具体的配置示例可以参考。 总结来说,通过注解或配置文件的方式,都可以在SpringBoot中注册定时任务。具体选择哪种方式取决于项目的需求和个人喜好。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot 整合定时任务 Quartz、SpringBoot 内置定时任务 Task 代码详解](https://blog.csdn.net/qq_38132105/article/details/126198913)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [SpringBoot:使用自定义cron表达式注册定时任务](https://blog.csdn.net/2301_76607156/article/details/130315606)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [springboot写的定时邮箱提醒系统](https://download.csdn.net/download/DCABQ/88017152)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值