SpringBoot2x使用@Scheduled实现定时任务

在编写Spring Boot应用中经常会遇到这样的场景,比如:需要定时地发送一些短信、邮件之类的操作,也可能会定时地检查和监控一些标志、参数等。这样就需要使用到Spring Boot的定时任务了。


创建定时任务

在Spring Boot中编写定时任务是非常简单的事,下面通过实例介绍如何在Spring Boot中创建定时任务,实现每过5秒输出一下当前时间。

在Spring Boot的主类中加入@EnableScheduling注解,启用定时任务的配置

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableScheduling
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

创建定时任务实现类

1
2
3
4
5
6
7
8
9
10
11
@Component
public class ScheduledTasks {

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
log.info("现在时间:" + dateFormat.format(new Date()));
}

}

运行程序,控制台中就可以看到输出了,定时任务开始正常运作了。

@Scheduled详解

在上面的入门例子中,使用了@Scheduled(fixedRate = 5000)注解来定义每过5秒执行的任务。

对于@Scheduled的使用,从源码里看看有哪些配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {

String CRON_DISABLED = ScheduledTaskRegistrar.CRON_DISABLED;

String cron() default "";

String zone() default "";

long fixedDelay() default -1;

String fixedDelayString() default "";

long fixedRate() default -1;

String fixedRateString() default "";

long initialDelay() default -1;

String initialDelayString() default "";
}

这些具体配置信息的含义如下:

  • cron:通过cron表达式来配置执行规则
  • zone:cron表达式解析时使用的时区
  • fixedDelay:上一次执行结束到下一次执行开始的间隔时间(单位:ms)
  • fixedDelayString:上一次任务执行结束到下一次执行开始的间隔时间,使用java.time.Duration#parse解析
  • fixedRate:以固定间隔执行任务,即上一次任务执行开始到下一次执行开始的间隔时间(单位:ms),若在调度任务执行时,上一次任务还未执行完毕,会加入worker队列,等待上一次执行完成后立即执行下一次任务
  • fixedRateString:与fixedRate逻辑一致,只是使用java.time.Duration#parse解析
  • initialDelay:首次任务执行的延迟时间
  • initialDelayString:首次任务执行的延迟时间,使用java.time.Duration#parse解析
点击查看

本文标题:SpringBoot2x使用@Scheduled实现定时任务

文章作者:LiJing

发布时间:2022年03月23日 - 11:03:33

最后更新:2023年06月03日 - 10:03:24

原始链接:https://blog-next.xiaojingge.com/posts/2794098303.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------------本文结束 感谢您的阅读-------------------