使用 Spring 进行任务调度,并将 cron 数据存储在实体中
我想基于一个实体运行 cron 任务,我们将该实体称为 TaskEntity,
TaskEntity 可以包含有关需要做什么(特定于域)以及何时执行的信息。 When 部分可以以基于 cron 的方式指定
最初我想研究 Quartz 但我找不到明确的例子
我不知道如何从这里继续。我只是想让最终用户能够在运行时添加任意数量的任务。如果最终用户更改任务(即禁用它、删除它或更改时间),那么它应该做出相应的行为。
I would like to run cron tasks based on an entity, lets call that entity TaskEntity
The TaskEntity can have info about what needs to be done (domain specific) and when. The when part can be specified in cron based manner
Initially I thought of looking into Quartz but I could not find clear examples
I am not sure how to proceed from here. I just want to give the end user the ability to add as many tasks as they want on run time. If the end user changes a task (ie disabled it, deletes it or changes the time) then it should behave accordingly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为最简单的解决方案是使用调度程序。
启动时,读取所有实体并计算任务的下一个开始时间。
当到达该时间时,关闭任务,计算下一个开始时间并进入睡眠状态。您必须监听实体的更新,以重新计算下一个开始时间,并在必要时重置调度程序。
您可以使用 org.quartz.Scheduler 来实现这一点。
查看文档了解详细信息: http://www.quartz-scheduler.org/docs/index .html
它看起来像这样。 (你的Job必须实现org.quartz.Job接口)
I think the easiest solution is to use a Scheduler.
On startup, read all your entities and calculate the next start time of a task.
When that time is reached, fire off the task, calculate the next starting time and go to sleep. You have to listen to updates on your entities to recalculate the next starting time and reset the scheduler if necessary.
You can user org.quartz.Scheduler for that.
Check the documentation for details: http://www.quartz-scheduler.org/docs/index.html
It will look something like this. (Your Job must implement the org.quartz.Job interface)