首页IT科技定时任务cron表达式详解 nohup(定时任务)

定时任务cron表达式详解 nohup(定时任务)

时间2025-05-04 22:26:34分类IT科技浏览3213
导读:定时任务 文档...

定时任务

文档

1.实现方案

1            、Timer

2                、线程池

3      、mq的延时队列

4         、QUARTZ

1.搭配cron表达式使用 2.支持年           ,spring不支持年 3.在周几的位置                  ,quartz的周1是2     ,spring的周1是1 4.

spring框架的定时任务

springboot默认定时任务框架不是QUARTZ        ,如果需要使用引入即可

2.cron表达式

1.语法

语法:秒 分 时 日 月 周 年 (spring 不支持年                  ,所以可以不写) http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html A cron expression is a string comprised of 6 or 7 fields separated by white space. Fields can contain any of the allowed values, along with various combinations of the allowed special characters for that field. The fields are as follows: Field Name Mandatory Allowed Values Allowed Special Characters Seconds YES 0-59 , - * / Minutes YES 0-59 , - * / Hours YES 0-23 , - * / Day of month YES 1-31 , - * ? / L W Month YES 1-12 or JAN-DEC , - * / Day of week YES 1-7 or SUN-SAT , - * ? / L # 1:周末 7:周一 Year NO empty, 1970-2099 , - * / 特殊字符: ,:枚举; (cron="7,9,23****?"):任意时刻的7,9        ,23秒启动这个任务; -:范围: (cron="7-20****?""):任意时刻的7-20秒之间     ,每秒启动一次 *:任意; 指定位置的任意时刻都可以 /:步长; (cron="7/5****?"):第7秒启动                  ,每5秒一次; (cron="*/5****?"):任意秒启动           ,每5秒一次; ?:(出现在日和周几的位置):为了防止日和周冲突  ,在周和日上如果要写通配符使用? (cron="***1*?"):每月的1号                 ,而且必须是周二然后启动这个任务; L:(出现在日和周的位置)           ”              , last:最后一个 (cron="***?*3L"):每月的最后一个周二(1:周末 2:周一 3:周二 4:周三 5:周四 6:周五 7:周六) W:Work Day:工作日 (cron="***W*?"):每个月的工作日触发 (cron="***LW*?"):每个月的最后一个工作日触发 #:第几个 (cron="***?*5#2"):每个月的 第2个周4

2.在线Cron表达式生成器

3.cron示例

常用cron表达式例子   (1)0/2 * * * * ? 表示每2秒 执行任务   (1)0 0/2 * * * ? 表示每2分钟 执行任务   (1)0 0 2 1 * ? 表示在每月的1日的凌晨2点调整任务   (2)0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业   (3)0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作   (4)0 0 10,14,16 * * ? 每天上午10点,下午2点              ,4点   (5)0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时   (6)0 0 12 ? * WED 表示每个星期三中午12点   (7)0 0 12 * * ? 每天中午12点触发   (8)0 15 10 ? * * 每天上午10:15触发   (9)0 15 10 * * ? 每天上午10:15触发   (10)0 15 10 * * ? 每天上午10:15触发   (11)0 15 10 * * ? 2005 2005年的每天上午10:15触发   (12)0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发   (13)0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发   (14)0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发(15)0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发   (16)0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发   (17)0 15 10 ? * MON-FRI 周一至周五的上午10:15触发   (18)0 15 10 15 * ? 每月15日上午10:15触发   (19)0 15 10 L * ? 每月最后一日的上午10:15触发   (20)0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发   (21)0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发   (22)0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发

3.SpringBoot整合

开启定时任务

整合步骤: 1.@EnableScheduling【spring 默认是使用自己的定时任务                 ,如果想整合Quartz  ,参考官方】 2.@Scheduled 3.定时任务配置类:TaskSchedulingAutoConfiguration package com.atguigu.gulimall.seckill.scheduled; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * 定时任务 * @EnableScheduling 开启定时任务 * @Scheduled 开启一个定时任务 * 自动配置类 TaskSchedulingAutoConfiguration * * 异步任务 * @EnableAsync 开启异步任务 * @Async 给希望异步执行的方法上标注 * 自动配置类:TaskExecutionAutoConfiguration 属性 TaskExecutionProperties */ @Component @EnableScheduling @Slf4j @EnableAsync public class HelloSchedule { /** * 1                、Spring中6位组成           ,不允许第7位的年 * 2         、在周几的位置:1-7代表周一到周日;Mon-SUN * 3      、定时任务不应该阻塞                  ,默认是阻塞的 * 1                、可以让业务运行以异步的方式     ,自己提交到线程池 * 2            、支持定时任务线程池:通过设置 TaskSchedulingProperties * 3   、让定时任务异步执行 * 异步任务 * 解决:定时任务加上异步任务来完成定时任务不阻塞的功能 */ @Async @Scheduled(cron = "* * * ? * 6") public void Hello() throws InterruptedException { log.info("hello...."); Thread.sleep(3000); } }

定时任务+异步任务

定时任务默认情况排队执行        ,所以前一个任务超时后面任务也会超时 解决方案: 方案1:业务方法自己作异步编排【CompletableFuture.runAsync】 方案2:修改定时任务线程池的线程个数【spring.task.scheduling.pool.size=5】(不一定生效                  ,有BUG) 方案3:让定时任务异步执行 方案3:让定时任务异步执行 整合步骤: 1.@EnableAsync 2.@Async 3.异步配置类:TaskExecutionAutoConfiguration 配置线程池: # 核心线程数 spring.task.execution.pool.core-size=5 # 最大线程数 spring.task.execution.pool.max-size=50 @Slf4j @Component @EnableAsync// 普通service方法也可以标注异步执行 @EnableScheduling public class HelloScheduled { @Async @Scheduled(cron = "*/5 * * ? * 4")// 周四的任意秒启动        ,每隔五秒执行一次 public void hello() { log.info("hello..."); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } }

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
局域网app游戏(局域网游戏需要IPX协议该怎么办)