使用 Spring 进行任务调度,并将 cron 数据存储在实体中

发布于 2024-09-26 21:03:13 字数 236 浏览 6 评论 0原文

我想基于一个实体运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最初的梦 2024-10-03 21:03:13

我认为最简单的解决方案是使用调度程序。

启动时,读取所有实体并计算任务的下一个开始时间。

当到达该时间时,关闭任务,计算下一个开始时间并进入睡眠状态。您必须监听实体的更新,以重新计算下一个开始时间,并在必要时重置调度程序。

您可以使用 org.quartz.Scheduler 来实现这一点。
查看文档了解详细信息: http://www.quartz-scheduler.org/docs/index .html

它看起来像这样。 (你的Job必须实现org.quartz.Job接口)

Trigger trigger = new SimpleTrigger( "jobname", "scheduler group name", dateTime );
scheduler.scheduleJob( jobDetail, trigger );

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)

Trigger trigger = new SimpleTrigger( "jobname", "scheduler group name", dateTime );
scheduler.scheduleJob( jobDetail, trigger );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文