有勇气的牛排博客

SpringBoot (八) 定时任务 @Scheduled

有勇气的牛排 560 Java 2023-02-25 21:08:02

文章目录

前言

@Scheduled注解是spring boot提供的用于定时任务控制的注解,主要用于控制任务在某个指定时间执行,或者每隔一段时间执行.注意需要配合@EnableScheduling使用,配置@Scheduled主要有三种配置执行时间的方式,cron,fixedRate,fixedDelay。它的配置一共有8个参数。

哈喽,大家好,我是<有勇气的牛排>(全网同名)🐮🐮🐮

有问题的小伙伴欢迎在文末<评论,点赞、收藏>是对我最大的支持!!!。

官网:https://www.couragesteak.com/

1 ScheduledTasks.java

/* * @Author : 有勇气的牛排(全网同名) * @FileName: ScheduledTasks.java * desc : 注解 定时任务 * */ package com.couragesteak.task; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @Slf4j public class ScheduledTasks { // @Scheduled(fixedRate = 3000) // 每3s执行一次 @Scheduled(cron = "1/2 * * * 8 ?") // cron表达式 public void taskService() { log.info("定时任务被执行: " + System.currentTimeMillis()); } }

2 APP.java

package com.couragesteak; /* * @Author : 有勇气的牛排 * @FileName: APP.java * desc : 启动 * */ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling // 开启定时任务 public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }

image.png


留言

专栏
文章
加入群聊