springboot定时任务实现的几种方式,spring boot动态定时任务

  springboot定时任务实现的几种方式,spring boot动态定时任务

  之前写过文章记录怎么在跳羚项目中简单使用定时任务,不过由于要借助时间单位表达式且都提前定义好放在配置文件里,不能在项目运行中动态修改任务执行时间,实在不太灵活。

  经过网上搜索学习后,特此记录如何在跳羚项目中实现动态定时任务。

  因为只是一个演示,所以只引入了需要的依赖:

  依赖项依赖项groupIdorg.springframework.boot/groupId artifact id spring-boot-starter-web/artifact id/依赖关系依赖项groupIdorg.springframework.boot/groupId artifact id spring-boot-starter-log4j 2/artifact id option true/optional/dependency!-弹簧靴2.3版本后,如果需要使用校验,需手动导入确认包-依赖groupIdorg.springframework.boot/groupId artifact id spring-boot-starter-验证/artifact id/依赖关系依赖groupIdorg.projectlombok/groupId artifact id lombok/artifact id选项true/optional/dependency/dependencies启动类:

  包com。西城。演示;导入org。spring框架。靴子。春季申请;导入org。spring框架。靴子。自动配置。弹簧启动应用程序;导入org。spring框架。日程安排。注释。启用计划;/* * * * @作者wl * @ date 2022/3/22 */@启用调度@ spring boot应用程序公共类演示应用程序{ public static void main(String[]args){ spring应用程序。运行(演示应用程序。class,args);System.out.println((*^^*)启动成功!());}}配置文件应用程序. yml只定义了服务端口:

  服务器:端口: 8089定时任务执行时间配置文件:任务配置.ini:

  printTime.cron=0/10 * * * *?

  定时任务执行类:

  包com。西城。演示。任务;进口龙目岛。数据;导入龙目岛。外部人员。SLF 4j。SLF 4j;导入org。spring框架。豆子。工厂。注释。价值;导入org。spring框架。语境。注释。财产来源;导入org。spring框架。日程安排。触发器;导入org。spring框架。日程安排。触发上下文;导入org。spring框架。日程安排。收件人

  ation.SchedulingConfigurer;import org.springframework.scheduling.config.ScheduledTaskRegistrar;import org.springframework.scheduling.support.CronTrigger;import org.springframework.stereotype.Component; import java.time.LocalDateTime;import java.util.Date; /** * 定时任务 * @author wl * @date 2022/3/22 */@Data@Slf4j@Component@PropertySource("classpath:/task-config.ini")public class ScheduleTask implements SchedulingConfigurer { @Value("${printTime.cron}") private String cron; @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { // 动态使用cron表达式设置循环间隔 taskRegistrar.addTriggerTask(new Runnable() { @Override public void run() { log.info("Current time: {}", LocalDateTime.now()); } }, new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { // 使用CronTrigger触发器,可动态修改cron表达式来操作循环规则 CronTrigger cronTrigger = new CronTrigger(cron); Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext); return nextExecutionTime; } }); }}编写一个接口,使得可以通过调用接口动态修改该定时任务的执行时间:

  

package com.wl.demo.controller; import com.wl.demo.task.ScheduleTask;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController; /** * @author wl * @date 2022/3/22 */@Slf4j@RestController@RequestMapping("/test")public class TestController { private final ScheduleTask scheduleTask; @Autowired public TestController(ScheduleTask scheduleTask) { this.scheduleTask = scheduleTask; } @GetMapping("/updateCron") public String updateCron(String cron) { log.info("new cron :{}", cron); scheduleTask.setCron(cron); return "ok"; }}

启动项目,可以看到任务每10秒执行一次:

 

  

 

  访问接口,传入请求参数cron表达式,将定时任务修改为15秒执行一次:

  

 

  可以看到任务变成了15秒执行一次

  

 

  除了上面的借助cron表达式的方法,还有另一种触发器,区别于CronTrigger触发器,该触发器可随意设置循环间隔时间,不像cron表达式只能定义小于等于间隔59秒。

  

package com.wl.demo.task; import lombok.Data;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.PropertySource;import org.springframework.scheduling.Trigger;import org.springframework.scheduling.TriggerContext;import org.springframework.scheduling.annotation.SchedulingConfigurer;import org.springframework.scheduling.config.ScheduledTaskRegistrar;import org.springframework.scheduling.support.CronTrigger;import org.springframework.scheduling.support.PeriodicTrigger;import org.springframework.stereotype.Component; import java.time.LocalDateTime;import java.util.Date; /** * 定时任务 * @author wl * @date 2022/3/22 */@Data@Slf4j@Component@PropertySource("classpath:/task-config.ini")public class ScheduleTask implements SchedulingConfigurer { @Value("${printTime.cron}") private String cron; private Long timer = 10000L; @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { // 动态使用cron表达式设置循环间隔 taskRegistrar.addTriggerTask(new Runnable() { @Override public void run() { log.info("Current time: {}", LocalDateTime.now()); } }, new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { // 使用CronTrigger触发器,可动态修改cron表达式来操作循环规则// CronTrigger cronTrigger = new CronTrigger(cron);// Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext); // 使用不同的触发器,为设置循环时间的关键,区别于CronTrigger触发器,该触发器可随意设置循环间隔时间,单位为毫秒 PeriodicTrigger periodicTrigger = new PeriodicTrigger(timer); Date nextExecutionTime = periodicTrigger.nextExecutionTime(triggerContext); return nextExecutionTime; } }); }}

增加一个修改时间的接口:

 

  

package com.wl.demo.controller; import com.wl.demo.task.ScheduleTask;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController; /** * @author wl * @date 2022/3/22 */@Slf4j@RestController@RequestMapping("/test")public class TestController { private final ScheduleTask scheduleTask; @Autowired public TestController(ScheduleTask scheduleTask) { this.scheduleTask = scheduleTask; } @GetMapping("/updateCron") public String updateCron(String cron) { log.info("new cron :{}", cron); scheduleTask.setCron(cron); return "ok"; } @GetMapping("/updateTimer") public String updateTimer(Long timer) { log.info("new timer :{}", timer); scheduleTask.setTimer(timer); return "ok"; }}

测试结果:

 

  

 

  到此这篇关于SpringBoot设置动态定时任务的方法详解的文章就介绍到这了,更多相关SpringBoot设置动态定时任务内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: