简体   繁体   English

创建将在特定时间后处理的计划任务

[英]Create a scheduled task which will be processed after the certain time

I'm working on a course project using Spring Boot and Hibernate. 我正在使用Spring Boot和Hibernate开发一个课程项目。 Currently, I have a Trip model, which contains information about Starting Place, Destination, Starting Date, Estimated Days, ... . 目前,我有一个Trip模型,其中包含有关起始地点,目的地,开始日期,预计天数等的信息。 My application allows its user create new trip with specified starting date. 我的应用程序允许其用户创建具有指定开始日期的新旅行。 When the trip is created, its status is set to WAITING by default. 创建行程时,默认情况下其状态设置为WAITING。 My issue is when the time (starting date) comes, I want to the status is changed to IN_PROGRESS automatically. 我的问题是当时间(开始日期)到来时,我希望状态自动更改为IN_PROGRESS。 I wonder how to implement it in Spring Boot. 我想知道如何在Spring Boot中实现它。 Some idea that I has found is using @Scheduled annotation, but what if I can load the specific time from Database, then add it to the annotation by Java code. 我发现的一些想法是使用@Scheduled注释,但如果我可以从Database加载特定时间,然后通过Java代码将其添加到注释中。

Some idea that I has found is using @Scheduled annotation, but what if I can load the specific time from Database, then add it to the annotation by Java code. 我发现的一些想法是使用@Scheduled注释,但如果我可以从Database加载特定时间,然后通过Java代码将其添加到注释中。

I'm looking forward to hearing from all of you. 我期待着听到你们所有人的意见。 Thanks for reading. 谢谢阅读。

but what if I can load the specific time from Database, then add it to the annotation by Java code. 但是,如果我可以从数据库加载特定时间,然后通过Java代码将其添加到注释中。

That's not what you would do. 这不是你会做的。

Instead, define a scheduled task in the code that runs at a pre-determined frequency, for instance once per day at midnight. 而是在代码中定义以预定频率运行的计划任务,例如每天午夜一次。 In the task, get all the trips that start on a given day and update the status (preferably in batch). 在任务中,获取在给定日期开始的所有行程并更新状态(最好是批量)。

You can use @Scheduled to accomplish this. 您可以使用@Scheduled来完成此任务。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM