简体   繁体   English

在Spring Boot中对时间表任务进行软编码的最佳方法是什么?

[英]What is the best way for softcoding schedule tasks in Spring Boot?

In my Spring program, I have a Scheduled task. 在我的Spring程序中,我有一个计划任务。

@Scheduled(cron = "0 0 0 2 * *") // hardcoded schedule
public void executeBatchJob() {
    batchJob.execute();
}

I have a specification change and now have to let the user freely configure the date and time of execution via an API. 我有一个规范更改,现在必须让用户通过API自由配置执行日期和时间。

One way I came up was to run a scheduled task every morning at 0:00 and check if the date is indeed the date of execution. 我出现的一种方法是每天早上0:00运行计划的任务,并检查日期是否确实是执行日期。 If true, check the time of execution and schedule the batch job to run at that time of the day. 如果为true,请检查执行时间并安排批处理作业在一天的该时间运行。

Is there a "Spring" way of achieving this? 有没有实现这一目标的“春季”方式?

Triggers can be used to configure the scheduled jobs. 触发器可用于配置计划的作业。

From the docs 来自docs

The basic idea of the Trigger is that execution times may be determined based on past execution outcomes or even arbitrary conditions. 触发器的基本思想是可以根据过去的执行结果甚至任意条件来确定执行时间。

Check out this answer for detailed explanation. 查看答案以获取详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Spring Boot 应用程序中安排任务的最佳方法是什么 - What is best way to schedule task in spring boot application 使用Java(Spring)从数据库安排任务的最佳方法 - Best way to schedule tasks from database using Java (Spring) 在 Spring Boot 中处理异常的最佳方法是什么? - What is the best way to handle exception in Spring Boot? 有没有办法在spring boot中调度一个带参数的函数 - Is there any way to schedule a function with parameters in spring boot 在spring boot jpa中存储日期的最佳方法是什么? - What's the best way to store date in spring boot jpa? 在 Spring 引导中返回每个 API 的统一响应的最佳方法是什么? - What is the best way to return the uniform response for each API in Spring Boot? 用Thymeleaf在Spring Boot中记录活动的最佳方法是什么? - What is the best way to log Activity in Spring Boot with Thymeleaf? 检查 Spring 引导 RestAPI 的字段的最佳方法是什么 - What is the best way to check which fields are given to the Spring boot RestAPI 更新用户数据弹簧启动的最佳方法是什么? - What is the best way to update user data spring boot? 在 Spring Boot 应用程序中处理 json 文件的最佳方法是什么? - What is the best way to process a json file in spring boot app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM