我们经常需要使用到定时器任务,现在spring一统天下的局面,spring-boot几乎都是我们工作中必须使用的项目框架模式。今天就来介绍一篇使用spring-boot项目编写定时器任务并且动态更新执行时间的案例。
一、新建一个springboot项目,引入maven的依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>8.2.1</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> </dependencies>
二、编写启动的application类,并且引入注解@EnableScheduling
package com.minio.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
三、编写一个schedule类
package com.minio.demo.schedule; import java.util.Date; 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.stereotype.Component; import lombok.Data; import lombok.extern.slf4j.Slf4j; @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("定时任务被执行了,当前线程记录是:{}", Thread.currentThread().getName()); } }, new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { // 使用CronTrigger触发器,可动态修改cron表达式来操作循环规则 CronTrigger cronTrigger = new CronTrigger(cron); Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext); return nextExecutionTime; } }); } }
四、在src/main/resources下面编写定时器的配置文件:task-config.ini
printTime.cron=0/10 * * * * ?
此时我们的定时器任务就编写完了,然后我们启动下application类,可以看到执行结果。
10秒钟执行一次,然后我们再创建一个controller,通过controller更改下执行的crontab。
五、编写一个ConfigScheduleController.java的类
package com.minio.demo.controller; 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; import com.minio.demo.config.BaseResponse; import com.minio.demo.schedule.ScheduleTask; import lombok.extern.slf4j.Slf4j; @Slf4j @RestController @RequestMapping("/schedule") public class ConfigScheduleController { @Autowired private ScheduleTask scheduleTask; @GetMapping("/config") public BaseResponse updateCron(String cron) { log.info("准备配置新的执行contab是 {}", cron); scheduleTask.setCron(cron); return BaseResponse.success(); } }
再启动项目,并且调用接口:http://localhost:8088/schedule/config?cron=0/5 * * * * ?
我们可以看到crontab被更新了。
到这里就完成了一个简易的springboot项目执行定时器任务,并且动态更新crontab的案例。
最后附上完整代码下载:点击下载
还没有评论,来说两句吧...