-
2022-06-28 15:07:30
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; 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.net.URL; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; /** * (SchedulerTask)数据同步 *0/10 * * * * ? */ @Component @Configuration //1.主要用于标记配置类,兼备Component的效果。 public class SchedulerTask implements SchedulingConfigurer { @Mapper public interface CronMapper { @Select("select * from access_work_shift_time") List<AccessWorkShiftTimeVO> getCron(); } @Autowired @SuppressWarnings("all") CronMapper cronMapper; //记录日志 private Logger logger = LoggerFactory.getLogger(SchedulerTask.class); @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { //获取定时任务执行信息 List<AccessWorkShiftTimeVO> tasks = getAllTasks(null); if (tasks.size() > 0) { for (int i = 0; i < tasks.size(); i++) { try { // 注册计时任务到Scheduling接口 taskRegistrar.addTriggerTask(getRunnable(tasks.get(i)), getTrigger(tasks.get(i))); } catch (Exception e) { logger.error("定时任务启动错误:" + e.getMessage()); } } } } //从数据库里取得所有要执行的定时任务 private List<AccessWorkShiftTimeVO> getAllTasks(String id) { List<AccessWorkShiftTimeVO> list = new ArrayList<>(); try { list=cronMapper.CronMapper(); }catch (Exception e){ logger.error(e.getLocalizedMessage()); } return list; } //执行业务逻辑 private Runnable getRunnable(AccessWorkShiftTimeVO SchedulerConfig) { return new Runnable() { @Override public void run() { //业务代码 } }; } //获取下次执行时间 public Trigger getTrigger(AccessWorkShiftTimeVO schedulerConfig) { return new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { AccessWorkShiftTimeVO cpSyncCron = new AccessWorkShiftTimeVO(); cpSyncCron = getAllTasks(schedulerConfig.getId()).get(0); String cron = ""; if (cpSyncCron != null) { cron = cpSyncCron.getCron(); } if (cron == null || "".equals(cron)) { return null; } CronTrigger cronTrigger = new CronTrigger(cron); Date nextExec = cronTrigger.nextExecutionTime(triggerContext); return nextExec; } }; } }
更多相关内容 -
SpringBoot 动态添加定时任务
2022-02-24 17:11:31springboot 手动添加/删除 定时任务最近的需求有一个自动发布的功能, 需要做到每次提交都要动态的添加一个定时任务
代码结构
1. 配置类
package com.orion.ops.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; /** * 调度器配置 * * @author Jiahang Li * @version 1.0.0 * @since 2022/2/14 9:51 */ @EnableScheduling @Configuration public class SchedulerConfig { @Bean public TaskScheduler taskScheduler() { ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.setPoolSize(4); scheduler.setRemoveOnCancelPolicy(true); scheduler.setThreadNamePrefix("scheduling-task-"); return scheduler; } }
2. 定时任务类型枚举
package com.orion.ops.handler.scheduler; import com.orion.ops.consts.Const; import com.orion.ops.handler.scheduler.impl.ReleaseTaskImpl; import com.orion.ops.handler.scheduler.impl.SchedulerTaskImpl; import lombok.AllArgsConstructor; import java.util.function.Function; /** * 任务类型 * * @author Jiahang Li * @version 1.0.0 * @since 2022/2/14 10:16 */ @AllArgsConstructor public enum TaskType { /** * 发布任务 */ RELEASE(id -> new ReleaseTaskImpl((Long) id)) { @Override public String getKey(Object params) { return Const.RELEASE + "-" + params; } }, /** * 调度任务 */ SCHEDULER_TASK(id -> new SchedulerTaskImpl((Long) id)) { @Override public String getKey(Object params) { return Const.TASK + "-" + params; } }, ; private final Function<Object, Runnable> factory; /** * 创建任务 * * @param params params * @return task */ public Runnable create(Object params) { return factory.apply(params); } /** * 获取 key * * @param params params * @return key */ public abstract String getKey(Object params); }
这个枚举的作用是生成定时任务的 runnable 和 定时任务的唯一值, 方便后续维护
3. 实际执行任务实现类
package com.orion.ops.handler.scheduler.impl; import com.orion.ops.service.api.ApplicationReleaseService; import com.orion.spring.SpringHolder; import lombok.extern.slf4j.Slf4j; /** * 发布任务实现 * * @author Jiahang Li * @version 1.0.0 * @since 2022/2/14 10:25 */ @Slf4j public class ReleaseTaskImpl implements Runnable { protected static ApplicationReleaseService applicationReleaseService = SpringHolder.getBean(ApplicationReleaseService.class); private Long releaseId; public ReleaseTaskImpl(Long releaseId) { this.releaseId = releaseId; } @Override public void run() { log.info("定时执行发布任务-触发 releaseId: {}", releaseId); applicationReleaseService.runnableAppRelease(releaseId, true); } }
4. 定时任务包装器
package com.orion.ops.handler.scheduler; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.Trigger; import java.util.Date; import java.util.concurrent.ScheduledFuture; /** * 定时 任务包装器 * * @author Jiahang Li * @version 1.0.0 * @since 2022/2/14 10:34 */ public class TimedTask { /** * 任务 */ private Runnable runnable; /** * 异步执行 */ private volatile ScheduledFuture<?> future; public TimedTask(Runnable runnable) { this.runnable = runnable; } /** * 提交任务 一次性 * * @param scheduler scheduler * @param time time */ public void submit(TaskScheduler scheduler, Date time) { this.future = scheduler.schedule(runnable, time); } /** * 提交任务 cron表达式 * * @param trigger trigger * @param scheduler scheduler */ public void submit(TaskScheduler scheduler, Trigger trigger) { this.future = scheduler.schedule(runnable, trigger); } /** * 取消定时任务 */ public void cancel() { if (future != null) { future.cancel(true); } } }
这个类的作用是包装实际执行任务, 以及提供调度器的执行方法
5. 任务注册器 (核心)
package com.orion.ops.handler.scheduler; import com.orion.ops.consts.MessageConst; import com.orion.utils.Exceptions; import com.orion.utils.collect.Maps; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.Date; import java.util.Map; /** * 任务注册器 * * @author Jiahang Li * @version 1.0.0 * @since 2022/2/14 10:46 */ @Component public class TaskRegister implements DisposableBean { private final Map<String, TimedTask> taskMap = Maps.newCurrentHashMap(); @Resource @Qualifier("taskScheduler") private TaskScheduler scheduler; /** * 提交任务 * * @param type type * @param time time * @param params params */ public void submit(TaskType type, Date time, Object params) { // 获取任务 TimedTask timedTask = this.getTask(type, params); // 执行任务 timedTask.submit(scheduler, time); } /** * 提交任务 * * @param type type * @param cron cron * @param params params */ public void submit(TaskType type, String cron, Object params) { // 获取任务 TimedTask timedTask = this.getTask(type, params); // 执行任务 timedTask.submit(scheduler, new CronTrigger(cron)); } /** * 获取任务 * * @param type type * @param params params */ private TimedTask getTask(TaskType type, Object params) { // 生成任务 Runnable runnable = type.create(params); String key = type.getKey(params); // 判断是否存在任务 if (taskMap.containsKey(key)) { throw Exceptions.init(MessageConst.TASK_PRESENT); } TimedTask timedTask = new TimedTask(runnable); taskMap.put(key, timedTask); return timedTask; } /** * 取消任务 * * @param type type * @param params params */ public void cancel(TaskType type, Object params) { String key = type.getKey(params); TimedTask task = taskMap.get(key); if (task != null) { taskMap.remove(key); task.cancel(); } } /** * 是否存在 * * @param type type * @param params params */ public boolean has(TaskType type, Object params) { return taskMap.containsKey(type.getKey(params)); } @Override public void destroy() { taskMap.values().forEach(TimedTask::cancel); taskMap.clear(); } }
这个类提供了执行, 提交任务的api, 实现 DisposableBean 接口, 便于在bean销毁时将任务一起销毁
6. 使用
@Resource private TaskRegister taskRegister; /** * 提交发布 */ @RequestMapping("/submit") @EventLog(EventType.SUBMIT_RELEASE) public Long submitAppRelease(@RequestBody ApplicationReleaseRequest request) { Valid.notBlank(request.getTitle()); Valid.notNull(request.getAppId()); Valid.notNull(request.getProfileId()); Valid.notNull(request.getBuildId()); Valid.notEmpty(request.getMachineIdList()); TimedReleaseType timedReleaseType = Valid.notNull(TimedReleaseType.of(request.getTimedRelease())); if (TimedReleaseType.TIMED.equals(timedReleaseType)) { Date timedReleaseTime = Valid.notNull(request.getTimedReleaseTime()); Valid.isTrue(timedReleaseTime.compareTo(new Date()) > 0, MessageConst.TIMED_GREATER_THAN_NOW); } // 提交 Long id = applicationReleaseService.submitAppRelease(request); // 提交任务 if (TimedReleaseType.TIMED.equals(timedReleaseType)) { taskRegister.submit(TaskType.RELEASE, request.getTimedReleaseTime(), id); } return id; }
最后
这是一个简单的动态添加定时任务的工具, 有很多的改造空间, 比如想持久化可以插入到库中, 定义一个 CommandLineRunner 在启动时将定时任务全部加载, 还可以给任务加钩子自动提交,自动删除等, 代码直接cv一定会报错, 就是一些工具, 常量类会报错, 改改就好了, 本人已亲测可用, 有什么问题可以在评论区沟通
-
springboot动态添加修改删除定时任务.md
2020-09-09 17:44:00(b)ThreadPoolTaskScheduler.schedule()方法会创建一个定时计划ScheduledFuture,在这个方法需要添加两个参数,Runnable(线程接口类) 和CronTrigger(定时任务触发器)(c)在ScheduledFuture中有一个cancel可以... -
springboot动态配置定时任务(schedule)
2019-06-13 11:02:18springboot动态配置定时任务(schedule),可不重启项目实现开关重启任务,改变任务定时规则 -
Springboot动态实现定时任务
2022-02-23 14:32:19springboot实现动态的定时任务spring简单集成定时任务
直接使用
@EnableScheduling
开启定时任务,使用@Scheduled(cron = "")
来标注任务马上就可以完成一个简单的定时任务了,这里就不贴上代码了spring动态实现定时任务
创建一个
SchedulingConfig
配置类来初始化定时任务的线程池的大小和名称等信息// 开启定时任务 @EnableScheduling @Configuration public class SchedulingConfig { @Bean public TaskScheduler taskScheduler() { // 创建任务调度线程池 ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); // 初始化线程池数量 taskScheduler.setPoolSize(4); // 是否将取消后的任务,从队列中删除 taskScheduler.setRemoveOnCancelPolicy(true); // 设置线程名前缀 taskScheduler.setThreadNamePrefix("TaskSchedulerThreadPool-"); return taskScheduler; } }
查找代码可以看见sringbot中有相关的定时任务注册类,需要实现
SchedulingConfigurer
类来进行注册定时任务,configureTasks
在项目初始化的时候会进行执行然后通ScheduledTaskRegistrar
用来注册定时任务,这里想进行动态的增加不太现实,我们可以对这个类做一个改变使我们可以在项目启动之后获取到ScheduledTaskRegistrar
这个类,代码如下:@Component public class ReportAutoTask implements SchedulingConfigurer { private ScheduledTaskRegistrar scheduledTaskRegistrar; /** * 根据taskId来储存指定的任务 */ private Map<String, ScheduledTask> scheduledTaskMap = new ConcurrentHashMap<>(16); /** * 定时任务注册(这个方法会在启动的时候进行执行) * * @param scheduledTaskRegistrar */ @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { this.scheduledTaskRegistrar = scheduledTaskRegistrar; } public ScheduledTaskRegistrar getScheduledTaskRegistrar() { return scheduledTaskRegistrar; } public void setScheduledTaskRegistrar(ScheduledTaskRegistrar scheduledTaskRegistrar) { this.scheduledTaskRegistrar = scheduledTaskRegistrar; } public Map<String, ScheduledTask> getScheduledTaskMap() { return scheduledTaskMap; } public void setScheduledTaskMap(Map<String, ScheduledTask> scheduledTaskMap) { this.scheduledTaskMap = scheduledTaskMap; } }
使用ScheduledTaskRegistrar来注册定时任务
红色的是这个类新增定时任务的方法,这里主要是用
addCronTask
这个方法来新增定时任务可以看到addCronTask上图中需要一个
Runnable
和一个表达式的参数,这两个参数也是创建一个CronTask的要求,所以我们首先去创建一个Runnable
,代码中是我自己创建的一些业务可以忽略,主要的方法就是一个run
方法@Component @Slf4j public class ReportAutoRunnable implements Runnable { /** * reportAutoDTO */ private ReportAutoDTO reportAutoDTO; public ReportAutoDTO getReportAutoDTO() { return reportAutoDTO; } public void setReportAutoDTO(ReportAutoDTO reportAutoDTO) { this.reportAutoDTO = reportAutoDTO; } @Autowired private ReportAutoService reportAutoService; @Override public void run() { String name = Thread.currentThread().getName(); try { log.info("定时任务开始执行 任务id:{} ,线程名称:{}", reportAutoDTO.getId(),name); reportAutoService.autoReport(reportAutoDTO); log.info("定时任务执行完毕 任务id:{} ,线程名称:{}", reportAutoDTO.getId(),name); } catch (Exception ex) { log.info("定时任务执行出现异常 任务id:{} ,异常信息:{}", reportAutoDTO.getId(),ex.getMessage()); } } }
自定义CronTask
创建完Runnable后可以创建一个
CronTask
,我是继承了一下CronTask类增加了两个属性一个id,一个名称public class ReportAutoCronTask extends CronTask { /** * 任务id */ private String taskId; /** * 任务名称 */ private String taskName; public ReportAutoCronTask(Runnable runnable, String expression) { super(runnable, expression); } public ReportAutoCronTask(Runnable runnable, CronTrigger cronTrigger) { super(runnable, cronTrigger); } public ReportAutoCronTask(Runnable runnable, String expression,String taskId,String taskName){ super(runnable, expression); this.taskId = taskId; this.taskName = taskName; } public String getTaskId() { return taskId; } public void setTaskId(String taskId) { this.taskId = taskId; } public String getTaskName() { return taskName; } public void setTaskName(String taskName) { this.taskName = taskName; } }
注册并启动定时任务
下面这一块代码就可以成功的注册一个定时任务
// 每30秒执行一次 String expression ="0/30 0 0 * ?"; // 创建定时任务 ReportAutoCronTask reportAutoCronTask = new ReportAutoCronTask(reportAutoRunnable, expression, "1111", "任务名称"); scheduledTaskRegistrar.addCronTask(reportAutoCronTask); // 启动定时任务 ScheduledTask scheduledTask = scheduledTaskRegistrar.scheduleCronTask(reportAutoCronTask);reportAutoCronTask.setFlag(true); scheduledTaskMap.put("1111", scheduledTask);
关闭指定的定时任务
遍历之前储存任务的集合直接调用
cancel()
就可以直接关闭带任务,如果需要删除的话就直接删除掉map中的定时任务就可以,想修改可以删除掉再重新新增for (Map.Entry<String, ScheduledTask> scheduledTaskEntry : scheduledTaskMap.entrySet()) { if (scheduledTaskEntry.getKey().equals(reportAutoDTO.getId())) { // 停止定时任务 scheduledTaskEntry.getValue().cancel(); } }
-
SpringBoot设置动态定时任务
2022-03-22 10:40:17之前写过文章记录怎么在SpringBoot项目中简单使用定时任务,不过由于要借助cron表达式且都提前定义好放在配置文件里,不能在项目运行中动态修改任务执行时间,实在不太灵活。 经过网上搜索学习后,特此记录如何在...之前写过文章记录怎么在SpringBoot项目中简单使用定时任务,不过由于要借助cron表达式且都提前定义好放在配置文件里,不能在项目运行中动态修改任务执行时间,实在不太灵活。
经过网上搜索学习后,特此记录如何在SpringBoot项目中实现动态定时任务。
因为只是一个demo,所以只引入了需要的依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> <optional>true</optional> </dependency> <!-- spring boot 2.3版本后,如果需要使用校验,需手动导入validation包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</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 * @date 2022/3/22 */ @EnableScheduling @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); System.out.println("(*^▽^*)启动成功!!!(〃'▽'〃)"); } }
配置文件application.yml,只定义了服务端口:
server: port: 8089
定时任务执行时间配置文件:task-config.ini:
printTime.cron=0/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 * @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实现动态控制定时任务支持多参数功能
2020-08-25 23:19:30主要介绍了SpringBoot实现动态控制定时任务-支持多参数功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 -
SpringBoot中并发定时任务的实现、动态定时任务的实现(看这一篇就够了)推荐
2020-08-26 03:42:38主要介绍了SpringBoot并发定时任务动态定时任务实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 -
基于springBoot动态配置定时任务
2020-09-27 17:39:44在生产环境中,有时要临时调整定时任务时间,或者禁用/启用定时任务; 以前都是修改cron表达式后重启项目; 总是感觉这个操作有点麻烦,不够方便, 于是,想实现一个动态的配置处理!!! 功能实现: 1.代码结构: 2.代码... -
springboot schedule 解决定时任务不执行的问题
2020-08-25 16:13:23主要介绍了springboot schedule 解决定时任务不执行的问题,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 -
SpringBoot实现动态定时任务
2020-04-21 16:22:12SpringBoot实现动态定时任务,是Springboot做的动态定时任务,可以暂停,恢复,添加,删除,等操作 -
springboot自定义动态添加定时任务
2019-05-20 16:58:45/** * 定时任务列表 */ @ApiOperation(value = "定时任务列表", notes = "定时任务列表") @GetMapping("/list") public ResultMap list(@RequestParam Map params) { PageUtils page = scheduleJobService.... -
基于Springboot执行多个定时任务并动态获取定时任务信息
2020-08-26 02:08:04主要为大家详细介绍了基于Springboot执行多个定时任务并动态获取定时任务信息,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
spring boot task实现动态创建定时任务的方法
2020-08-26 10:24:45主要介绍了spring boot task实现动态创建定时任务,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 -
springboot 动态添加定时任务(配合数据库增删查改定时任务)
2019-07-11 16:08:54第三步:创建我们的触发器Trigger 和JobDetail (即什么时候触发,触发条件是什么),我这边是把这个做成一个服务,方便其他地方动态调用,我这里只写了创建或者修改删除定时任务,比如暂停,直接调用Scheduler ... -
SpringBoot之动态配置定时任务
2020-10-15 16:52:45在Springboot中创建定时任务,实现动态配置定时任务 启动类开启定时任务 @EnableScheduling //开启定时任务 @EnableEurekaClient @SpringBootApplication @EnableElasticsearchRepositories(basePackages = ... -
Springboot实现动态设置定时任务
2021-02-18 10:08:55在实际设计开发“测试集”功能的时候,为每一个测试集提供了一个定时任务cron表达式字段,代码需要实现将具备cron表达式的测试集动态加入到定时任务中,按cron表达式的规则定时执行测试集中的接口用例 二、... -
SpringBoot Quartz 动态定时任务
2022-06-08 11:21:10SpringBoot Quartz 动态定时任务 -
Springboot2-Quartz 后台可动态配置的定时任务
2019-02-28 15:31:24SpringBoot2 与 Quartz 整合的Demo。 后台可添加、修改、移除 定时任务。 也可查看当前任务的状态 灵活的定时任务 -
Springboot项目添加定时任务
2021-09-01 12:49:08文章目录前言一、添加注解二、编写定时任务类 前言 在我们的项目中,经常会...在springboot项目的启动类上面添加定时任务注解 //开启定时任务 @EnableScheduling public class StaApplication { public static void . -
Springboot2.0.1整合Quartz动态定时任务
2018-09-15 21:21:06SpringBoot整合Quartz实现动态定时任务,在页面动态展示,添加,暂停,删除定时任务. 并整合了MyBatis-plus, 感兴趣的小伙伴 可以试试 -
springboot 创建动态定时任务
2021-01-04 15:48:20初始化定时任务周期 就是去数据库查询初始配置的定时任务,如果执行过程中有结果或者没有结果都可以对数据库表进行修改,然后下次再按修改后的时间执行任务 附上数据库表结构 package ... -
springboot整合Quartz实现动态配置定时任务的方法
2020-08-29 03:56:31本篇文章主要介绍了springboot整合Quartz实现动态配置定时任务的方法,非常具有实用价值,需要的朋友可以参考下 -
SpringBoot 动态操作定时任务(启动、停止、修改执行周期)
2021-07-02 23:00:59springboot自身带有一套定时任务框架,使用起来也比较简单,只需要在应用上添加@EnableScheduling注解开启定时任务的支持,然后在具体任务实现的方法上添加 @Scheduled然后配置corn表达式就完成了。 代码示例: 1、... -
【Scheduled定时任务】spring boot动态生成定时任务
2022-03-12 20:43:16记录一下动态生成定时任务和更新定时任务配置的开发经验 目录 前言 一、自定义定时任务配置类 二、实现SchedulingConfigurer接口 1.实现定时任务配置接口,在此自定义定时任务创建策略,动态管理定时任务 2.... -
SpringBoot实现用户定制的定时任务(动态定时任务)
2022-03-14 09:40:39我们知道SpringBoot能使用@Scheduled注解来进行定时任务的控制,该注解需要配合Cron表达式以及在启动类上添加@EnableScheduling注解才能使用。 不过我们现在的假定情景并不是程序员设定的定时任务,而是用户可以在... -
springboot动态定时任务整合quartz
2022-03-08 09:58:21springboot 动态定时任务 springboot实现动态定时任务的方法有两种: 可以实现SchedulingConfigurer 接口(可以见springboot 动态定时任务) 整合quartz(当前文章要说的) springboot整合需要的依赖 ...