在浴室里做的网站网页游戏开服表265

张小明 2026/1/3 8:52:34
在浴室里做的网站,网页游戏开服表265,禄劝网络推广外包,建筑工程网教之前写过文章记录怎么在SpringBoot项目中简单使用定时任务#xff0c;不过由于要借助cron表达式且都提前定义好放在配置文件里#xff0c;不能在项目运行中动态修改任务执行时间#xff0c;实在不太灵活。 经过一番研究之后#xff0c;特此记录如何在SpringBoot项目中实现…之前写过文章记录怎么在SpringBoot项目中简单使用定时任务不过由于要借助cron表达式且都提前定义好放在配置文件里不能在项目运行中动态修改任务执行时间实在不太灵活。经过一番研究之后特此记录如何在SpringBoot项目中实现动态定时任务。因为只是一个demo所以只引入了需要的依赖dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-log4j2/artifactId optionaltrue/optional /dependency !-- spring boot 2.3版本后如果需要使用校验需手动导入validation包-- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-validation/artifactId /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency /dependencies启动类package com.wl.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; /** * author wl */ EnableScheduling SpringBootApplication publicclass DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); System.out.println((*^▽^*)启动成功!!!(〃▽〃)); } }配置文件application.yml只定义了服务端口server: port: 8089定时任务执行时间配置文件task-config.ini:printTime.cron0/10 * * * * ?定时任务执行类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.stereotype.Component; import java.time.LocalDateTime; import java.util.Date; /** * 定时任务 * author wl */ Data Slf4j Component PropertySource(classpath:/task-config.ini) publicclass 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 */ Slf4j RestController RequestMapping(/test) publicclass TestController { privatefinal 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); returnok; } }启动项目可以看到任务每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 */ Data Slf4j Component PropertySource(classpath:/task-config.ini) publicclass 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 */ Slf4j RestController RequestMapping(/test) publicclass TestController { privatefinal 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); returnok; } GetMapping(/updateTimer) public String updateTimer(Long timer) { log.info(new timer :{}, timer); scheduleTask.setTimer(timer); returnok; } }测试结果
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

课程建设网站广告制作公司开票范围

用nRF24L01打造无线麦克风:从电路到代码的完整实践你有没有遇到过这样的场景?想在教室、工厂或户外部署一个拾音设备,但拖着长长的音频线既不安全也不美观;或者做智能语音项目时,发现蓝牙延迟太高、Wi-Fi功耗太大。其实…

张小明 2026/1/2 18:25:38 网站建设

网站建设类的职位免费咨询皮肤科专家

macOS百度网盘极速下载全解析:突破SVIP限制的技术方案 【免费下载链接】BaiduNetdiskPlugin-macOS For macOS.百度网盘 破解SVIP、下载速度限制~ 项目地址: https://gitcode.com/gh_mirrors/ba/BaiduNetdiskPlugin-macOS 在当今数字资源获取日益重要的时代&a…

张小明 2026/1/2 23:42:04 网站建设

吉林省建设厅网站评职称系统wordpress制作主题调用编辑器

第一章:你还在忍受触控不跟手?Open-AutoGLM动态补偿技术揭秘在高帧率设备普及的今天,触控延迟仍是影响用户体验的关键瓶颈。尤其在快速滑动或精准绘图场景下,“触控不跟手”问题尤为突出。Open-AutoGLM 通过引入动态预测补偿机制&…

张小明 2026/1/2 18:17:30 网站建设

租用阿里云做网站idea做一个自己的网站教程

当Nature封面讲述中国AI故事,我们已经在定义未来 原创 云鹏 智东西 2025年12月19日 18:01 北京 从杭州走向世界,中国AI正重塑全球竞争格局。 作者 | 云鹏 编辑 | 漠影 今天,中国科技正加速走向世界,从追赶走向引领&#xff…

张小明 2026/1/2 17:17:14 网站建设

苏州区网站建设google网站优化工具

Wan2.2-T2V-A14B如何理解复杂文本描述生成情节完整视频? 在短视频内容爆炸式增长的今天,一个品牌可能需要每天产出上百条广告素材,一部电影前期预演要耗费数周绘制分镜和动画草稿,而教育机构为了制作一段三分钟的情景教学视频&…

张小明 2026/1/2 17:52:06 网站建设

有做国际网站生意吗网站设计师与网站开发工程师

Keil头文件配置实战指南:从入门到精通的嵌入式C工程实践 你有没有遇到过这样的场景?刚接手一个Keil项目,打开 main.c 第一行就报错:“ fatal error: ‘stm32f4xx_hal.h’ file not found ”。明明文件就在工程目录里&#xff…

张小明 2026/1/3 3:28:07 网站建设