-
集成Schedule
-
SpringBoot Schedule 实现原理
- 通过 JUC 的 ScheduledThreadPoolExecutor 实现 Spring Boot 的 Schedule
- Quartz 也是通过 ScheduledThreadPoolExecutor 来实现定时任务的执行与调度
-
SpringBoot 使用 @Scheduled创建 定时任务 , SpringBoot Runner的应用
// ApplicationRunner (应用程序运行器) 是一个用于在Spring Boot应用程序启动后执行代码的接口 // CommandLineRunner (控制台窗口运行器) 是一个接口 @SpringBootApplication @EnableScheduling public class DemoApplication implements ApplicationRunner { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(ApplicationArguments args) throws Exception { System.out.println("Hello World from Application Runner"); } }
/* Seconds : 可出现", - * /"四个字符,有效范围为0-59的整数 Minutes : 可出现", - * /"四个字符,有效范围为0-59的整数 Hours : 可出现", - * /"四个字符,有效范围为0-23的整数 DayofMonth : 可出现", - * / ? L W C"八个字符,有效范围为0-31的整数 Month : 可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc DayofWeek : 可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推 Year : 可出现", - * /"四个字符,有效范围为1970-2099年 "0 0 12 * * ?" 每天中午十二点触发 "0 15 10 ? * *" 每天早上10:15触发 "0 15 10 * * ?" 每天早上10:15触发 "0 15 10 * * ? *" 每天早上10:15触发 "0 15 10 * * ? 2005" 2005年的每天早上10:15触发 "0 * 14 * * ?" 每天从下午2点开始到2点59分每分钟一次触发 "0 0/5 14 * * ?" 每天从下午2点开始到2:55分结束每5分钟一次触发 "0 0/5 14,18 * * ?" 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 "0 0-5 14 * * ?" 每天14:00至14:05每分钟一次触发 "0 10,44 14 ? 3 WED" 三月的每周三的14:10和14:44触发 "0 15 10 ? * MON-FRI" 每个周一、周二、周三、周四、周五的10:15触发 */ @Component public class ScheduledTasks { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) public void reportCurrentTime() { System.out.println("现在时间:" + dateFormat.format(new Date())); } @Scheduled(cron = "0 */1 * * * ?") // 每1分钟触发一次 @Transactional(rollbackFor = Exception.class) public void doWaterExecute() { // ... } //月 当月最后一天23:59:55结算 @Scheduled(cron = "55 59 23 28-31 * ?") @Transactional(rollbackFor = Exception.class) @Async public void doStarMonthHitRankTask() { Date now = new Date(); boolean dayOfMonth = DateUtils.isLastDayOfMonth(now); if (dayOfMonth) { // ... } } }
-
Spring Boot 集成Schedule
最新推荐文章于 2024-04-17 10:53:18 发布