动态改变Timer服务或者Quartz

发布于 2024-12-17 18:27:34 字数 204 浏览 0 评论 0原文

我们需要在 Java EE 服务器中执行调度作业,并且我们知道如何使用 Quartz 或 Timer 服务。

但我们的问题是,如果我们想改变生产排程或者手动触发批次,该怎么办?

在传统的解决方案中,我们使用 servlet 来运行作业。然后使用带有 http 客户端(即 lynx)的 cronjob 来触发 servlet。它很容易实施,并且可以在生产中改变。

We need a schedule job in a Java EE server and we know how to use Quartz or the Timer service.

But our question is, if we want to change the schedule on production or manually trigger the batch, how to do it?

In the traditional solution, we use a servlet to run the job. And then use a cronjob with a http client (i.e. lynx) to trigger the servlet. It's easy to implement and could change on production.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无所谓啦 2024-12-24 18:27:34

由于这个确切的问题,我从未发现计时器完全令人满意:您无法真正监视它们的状态或修改它们。

我推荐的是第二层工作经理课程。当您调用此类时,它会在时间“X”安排一个 Java EE 计时器,并且还记录您想要在时间“X”执行“作业”的事实。当时间到达时,Java EE 计时器会调用此作业管理器类,该类会找到作业并调用该作业。

这允许您编写一个“取消计划”函数。调用取消计划会删除该作业。当 Java EE 计时器在时间“X”调用时,此类未找到任何作业,因此会忽略它。

您还可以实现“更改计划”功能,删除旧条目,并在时间“Y”创建新条目,为时间“Y”安排 Java EE 计时器。 Java EE 计时器将在时间“X”到达,另一个计时器在时间“Y”到达,但只有时间“Y”有效。

因此,手动触发的问题是让一个 servlet 立即调用“更改计划”。

需要注意的另一个细节:由于计时器事件并不完全可靠,因此我们实现此类来查找当前时间之前已安排的所有作业,并在该时刻运行所有作业。然后,我们每 5 分钟左右安排一次额外的 Java EE 计时器事件。这些计时器将接替因某种原因而被遗漏的任何工作。如果您的作业队列是持久的,这一点很重要,那么在重新启动服务器时,它可能恰好在计时器应该关闭的时刻关闭。没问题:Java EE 计时器事件本身没有任何意义,它们只是用于唤醒作业处理程序,以便它可以运行所有过时的作业。

I have never found Timers to entirely satisfactory because of this exact problem: you can't really monitor their status or modify them.

What I recommend is a second layer job manager class. When you call this class, it schedules a Java EE timer for time 'X', and it also records the fact that you want to execute a 'job' at time 'X'. When that time arrives, the Java EE timer calls this job manager class, which finds the job, and calls the job.

What this allows you to do is to write an "unschedule" function. Calling unschedule would remove the job. When the Java EE timer calls at time 'X' this class does not find any job, and so ignores it.

You can also implement a "change schedule" function that removes the old entry, and create a new entry at time 'Y' scheduling a Java EE timer for time 'Y'. The Java EE timer will arrive at both time 'X' and another at time 'Y' but only the time 'Y' will have effect.

Thus manual triggering is a matter of having a servlet that call "change schedule" to be right now.

The one other detail to be careful of: because timer events are not completely reliable, we implement this class to find all the jobs that had been scheduled before the current time, and run all of them at that moment. We then schedule extra Java EE timer events for every 5 minutes or so. Those timers will pick up any jobs that for one reason or another had been left behind. This is important if your job queue is persistent, then it might be that while restarting the server, it is down at exactly the moment that the timer was supposed to go off. No problem: Java EE Timer events themselves have no meaning, they just serve to wake up the job handler, so it can run all the outdated jobs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文