在Java中的特定时间安排任务 [英] Scheduling task at some specific time in Java

查看:144
本文介绍了在Java中的特定时间安排任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行了一些代码,这些代码将在不同的日期时间安排许多作业.因此,总的来说,我将在特定的日期时间执行很多工作.我知道有Spring Scheduler会在某个时间段执行作业,但是它不会动态地调度作业.我可以将ActiveMQ与定时交付一起使用,也可以将Quartz用于我的目的,但是需要一些建议.我应该使用Quartz或ActiveMQ定时/延迟交付还是其他方式.

I have some code execution which will scheduled many jobs at different date-time. So overall I will have lot of jobs to run at specific date-time. I know that there is Spring Scheduler which will execute a job at some time period, but it does not schedule a job dynamically. I can use ActiveMQ with timed delivery or Quartz for my purpose but looking for a little suggestion. Shall I use Quartz or ActiveMQ timed/delayed delivery or something else.

Executor服务中还有定时执行的另一种选择,但是我相信,如果应用程序重新启动,则工作将消失.任何帮助将不胜感激.

There is another alternative as well in Executor service with timed execution, but if application restarts then the job will be gone I believe. Any help will be appreciated.

推荐答案

虽然您可以在ActiveMQ中计划消息传递,但它并不是设计用作作业调度程序的,而这恰恰是Quartz设计的目的.

While you can schedule message delivery in ActiveMQ it wasn't designed to be used as a job scheduler whereas that's exactly what Quartz was designed for.

在您的评论之一中,您谈到了想要可扩展的解决方案",ActiveMQ在大量计划的作业中无法很好地扩展,因为在队列中累积的消息越多,执行起来就越糟糕,因为它最终将具有将这些消息分页到磁盘,而不是将其保留在内存中.像大多数消息代理一样,ActiveMQ旨在在消耗消息之前将消息保留相对较短的时间.它与数据库更不同,后者更适合此用例.因此,对于大量的工作,Quartz应该比ActiveMQ更好地扩展.

In one of your comments you talked about wanting a "scalable solution" and ActiveMQ won't scale well with a huge number of scheduled jobs because the more messages which accumulate in the queues the worse it will perform since it will ultimately have to page those messages to disk rather than keeping them in memory. ActiveMQ, like most message brokers, was meant to hold messages for a relatively short amount of time before they are consumed. It's much different than a database which is better suited for this use-case. Quartz should scale better than ActiveMQ for a large number of jobs for this reason.

此外,您可以在Quartz中配置的作业的复杂性更高.如果您使用ActiveMQ,并且最终需要的功能比其支持的功能更多,那么这种复杂性将被降低到您的应用程序代码中.但是,由于Quartz是作为作业调度程序设计的,因此有很大的机会可以简单地使用Quartz做您想要的事情.

Also, the complexity of the jobs you can configure in Quartz is greater. If you go with ActiveMQ and you eventually need more functionality than it supports then that complexity will be pushed down into your application code. However, there's a fair chance could simply do what you want with Quartz since it was designed as a job scheduler.

最后,在我看来,数据库比消息代理更易于维护,并且在大多数云提供商中也易于配置数据库.我建议您选择Quartz.

Lastly, a database is more straight-forward to maintain than a message broker in my opinion and a database is also easy to provision in most cloud providers. I'd recommend you go with Quartz.

这篇关于在Java中的特定时间安排任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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