@scheduled 执行一次_Spring 系列之定时任务——Scheduled

生有涯,学海无涯

Spring 的定时任务想必大家多多少少都用过,经过 Spring 团队的封装,大家使用起来非常的方便和简洁,那关于 定时任务的真正使用还有哪些你不知道的事呢?下面我们一起来看一下吧。

官方条件

在使用Scheduled 注解前,我们先看下,官方给出了使用条件

An annotation that marks a method to be scheduled. Exactly one of the cron(), fixedDelay(), or fixedRate() attributes must be specified.

The annotated method must expect no arguments. It will typically have a void return type; if not, the returned value will be ignored when called through the scheduler.

Processing of @Scheduled annotations is performed by registering a ScheduledAnnotationBeanPostProcessor. This can be done manually or, more conveniently, through the element or @EnableScheduling annotation.

意思是说,在使用 Scheduled 注解的时候,必须设置 cron(),fixedDelay(), fixedRate() 三个中的一个属性。该注解使用的方法不期望接收任何参数,并且要使用 void 返回类型,即使设置了返回值,也会在回调的时候被忽略。第三句是说,我们在使用 Scheduled 注解的时候需要注册 ScheduledAnnotationBeanPostProcessor ,可以通过手动,或者配置 或者在 Springboot 项目中,直接在启动类上添加 @EnableScheduling 注解都可以。总结一下就是

  1. 设置定时策略;
  2. 不设置返回类型;
  3. 注册 processor。

使用案例

在平时使用的时候我们基本上只要加一个注解,然后再配置上相应的 cron 参数就可以了。如下所示:

7a14750209204d7182746c8690529cfc
Scheduled 参数配置说明
  1. cron() 配置cron 是一个六位或者七位的字符串,每一位都对应的相应的含义,如上0 0 0 * * ? 表示的是每天 0 点执行。表达式从左往右依次代表的含义是Seconds Minutes Hours DayofMonth Month DayofWeek Year 或 Seconds Minutes Hours DayofMonth Month DayofWeek 详细的配置方式,读者感兴趣可以自行研究,而且网上也有根据条件自动生产表达式的,可以参考。
  2. fixedDelay() 方式,执行完毕后调用官方解释Execute the annotated method with a fixed period in milliseconds between the end of the last invocation and the start of the next.意思是说在上一次执行完毕和下一次开始调用的中间,延迟一段时间,以毫秒为单位。
  3. fixedRate() 方式,开始执行后延迟调用官方解释Execute the annotated method with a fixed period in milliseconds between invocations.固定一个时间间隔进行前后方法的调用。

这几种的配置方式还是比较简单的,可以根据自己的场景以及需求来设置。可以说都还是比较常用的。

注意

这里结合自身项目,跟大家介绍几个在使用的过程中容易遇到的一些问题。

单线程问题

Spring 的任务调用默认使用的是单线程模式,这个如果大家没有在生产上真正遇到过可能不会发现有问题,毕竟本地和测试环境数据少,不容易出现,但是在真正复杂的生产环境中,还是要十分注意的。

78ce5c7b44dd48a2ac34e2ebb0fab9b5

由于 Spring 的默认实现是单线程,所以在生产的时候如果任务一旦多了,而且耗时长了,就会出现很多意想不到的情况,比如会发现某个任务不执行,或者没有按照指定的时间运行。这种情况往往是很恐怖的,如果说对实时性要求不高的话,影响不大,如果实时性要求很高的话,那绝对不能容忍。

这个时候我们需要自定义taskScheduler ,通过如下方式

import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.SchedulingConfigurer;import org.springframework.scheduling.config.ScheduledTaskRegistrar;/** * 
* 功能:
* 作者:@author 子悠
* 日期:2019-08-22 17:51
* 详细说明:
*/@Configurationpublic class ScheduledThreadPoolConfig implements SchedulingConfigurer { @Value("${thread.pool.corePoolSize:10}") private int corePoolSize; @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(corePoolSize)); }}

通过实现 SchedulingConfigurer 接口中的 configureTasks 方法,来创建一个 ScheduledTask 并注入到 ScheduledTaskRegistrar 中。

这样就可以自定义线程数,从而避免上述问题的存在。

多实例问题

另外还有一个问题就是如果一个模块定时任务很多,最好可以封装为一个独立的任务调度模块,对于负责场景的任务调度可以采用一些开源的调度框架,比如 XXL-JOB,Azkaban。

如果只是一些简单的定时任务,不需要引入开源框架的时候,那么自己开发后在部署的时候需要注意多实例会重复运行的情况,意思是说一个定时任务模块,如果部署了两个实例就会导致任务重复执行。这种时候只能采用单实例部署,单实例部署的时候需要加守护进程和程序监控,避免程序出问题。

如果是在需要高可用,可以自己开发调度模块或者采用开源的框架实现。大部分场景 XXL-JOB 应该都可以满足。

总结

今天给大家介绍了 Spring 框架中的 Scheduled 组件的简单使用,以及在使用过程中注意的一些事项,希望对大家有帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,我们可以使用`@Scheduled`注解配置定时任务。默认情况下,这个注解支持固定的时间表达式,例如:`@Scheduled(cron = "0 0 12 * * ?")`表示每天中午12点执行任务。 如果想要动态调整定时任务执行时间,可以使用以下步骤: 1. 创建一个定时任务管理器类,用于管理所有的定时任务。这个类可以维护一个任务列表,每个任务都有一个唯一的ID和对应的执行时间表达式。 ``` @Component public class TaskManager { private Map<String, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>(); public void addTask(String id, Runnable task, String cronExpression) { ScheduledFuture<?> future = taskScheduler().schedule(task, new CronTrigger(cronExpression)); tasks.put(id, future); } public void removeTask(String id) { ScheduledFuture<?> future = tasks.get(id); if (future != null) { future.cancel(true); tasks.remove(id); } } public void updateTask(String id, String cronExpression) { removeTask(id); // 重新添加任务 // ... } private ThreadPoolTaskScheduler taskScheduler() { ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.setPoolSize(10); scheduler.setThreadNamePrefix("TaskScheduler-"); scheduler.setWaitForTasksToCompleteOnShutdown(true); scheduler.setAwaitTerminationSeconds(60); scheduler.initialize(); return scheduler; } } ``` 2. 在需要执行的任务上添加`@Component`和`@Scope("prototype")`注解,这样每次调用`addTask`方法时都会创建一个新的任务实例。 ``` @Component @Scope("prototype") public class MyTask implements Runnable { private String id; public MyTask(String id) { this.id = id; } @Override public void run() { // 任务执行逻辑 } } ``` 3. 在需要动态调整执行时间的地方调用`TaskManager`的相关方法,例如: ``` @Autowired private TaskManager taskManager; @Autowired private ApplicationContext context; // 添加任务 MyTask task = context.getBean(MyTask.class, "task1"); taskManager.addTask("task1", task, "0 0/5 * * * ?"); // 修改任务执行时间 taskManager.updateTask("task1", "0 0/10 * * * ?"); // 删除任务 taskManager.removeTask("task1"); ``` 这样我们就可以动态调整定时任务执行时间了。需要注意的是,修改任务执行时间时需要先删除旧的任务再添加新的任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值