使用 Quartz 或任何其他 java api 进行动态作业调度 [英] Dynamic Job Scheduling with Quartz or any other java api

查看:63
本文介绍了使用 Quartz 或任何其他 java api 进行动态作业调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UI 界面,用户可以在其中定义作业名称、间隔、活动/非活动等.

I have an UI Interface where user can define Job name, interval, active/Inactive etc.

我们如何使用 Quartz Scheduler 或任何 java/Spring api 实现这一点?

How we can achieve this with Quartz Scheduler or any java/Spring api ?

例如.假设任何 Quartz 作业启动并且间隔设置为 10 分钟,那么在理想情况下作业将在接下来的 10 分钟间隔内运行.但是每次作业运行时,我们都想从数据库中获取最新的时间间隔并进行调度.

Ex. Suppose any Quartz job is started and interval is set as 10 min, So in ideal case job will run in next 10 min interval. But every time job runs we want to fetch the latest interval from database and schedule it.

10:00 作业运行并在数据库中间隔设置为 10 分钟10:10 作业运行并在数据库中间隔设置为 20 分钟

10:00 Job runs and in the database interval is set to 10 min 10:10 Job runs and in the database interval is set to 20 min

所以下次作业应该在 10:30 运行

So next time job should run at 10:30

推荐答案

如果你使用 Quartz,你可以实现一个自定义的 触发器.您的实现将在数据库中查找该值,并在下次运行时返回 getFireTimeAfter.

If you use Quartz, you can implement a custom Trigger. Your implementation would lookup the value in the database and return when the next time the run should happen in the getFireTimeAfter.

另一种选择是使用 Spring 调度 API 并实现 触发接口.同样,nextExecutionTime 方法将决定下一次运行的时间.

Another option is to use Spring Scheduling APIs and implement the Trigger interface. Same here, the nextExecutionTime method would decide when the next run should happen.

使用自定义实现的优势在于您可以完全控制触发逻辑(就像在您的情况下,在数据库中进行查找并动态设置下一次运行时间).

The advantage of using a custom implementation is that you have full control over the triggering logic (like in your case, do a lookup in the database and dynamically set the next run time).

这篇关于使用 Quartz 或任何其他 java api 进行动态作业调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆