一种根据给定时间表运行任务的方法
我即将创建一个小型应用程序,它将负责以不同的时间间隔向不同的用户发送各种报告。我们可能正在谈论向不同的人发送 50 或 100 份不同的报告。有些报告需要每天生成,有些需要每周生成,有些则需要每月生成。
我之前一直使用 Quartz 库来定期运行任务。然而,为了让事情变得简单,我喜欢用一个 Quartz 线程来处理所有报告。也就是说,线程应该循环遍历所有报告(例如每 15 分钟一次),并确定是否需要生成和发送一个或多个报告。报告生成时间为 12:00 还是 12:15 并不重要。
我正在考虑是否有可能以某种方式为每个报告设置特定时间,例如“mon@12:00,wed@12:00”或“fri@09:30”。然后,基于此,线程将确定是否是发送报告的时间。
我的问题是;有其他人做过类似的事情吗?是否存在可以轻松实现此任务的库?
I'm about to create a small application which will be responsible for sending out various reports to various users at various intevals. We might be talking about 50 or 100 different reports going to different people. Some reports needs to be generated every day, some every week, and some every month.
I've been using the Quartz library earlier to run tasks at regular intervals. However, in order to keep things simple I like the thought of having a single Quartz thread taking care of all reports. That is, the thread should loop through all reports, say every 15 minutes, and determine wether it is time for one or more to be generated and sent. It does not matter if a report is generated at 12:00 or 12:15.
I'm thinking about wether it would be possible, somehow, for each report to set up specific times such as "mon@12:00,wed@12:00" or "fri@09:30". Then, based on that, the thread would determine if it was time to send a report or not.
My question is; has anyone else done something like this and does any libraries exist which can make it easy to implement this task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不简单地为每个报告注册一个单独的quartz任务实例并让Quartz为您处理所有调度?这毕竟是其背后的重点。
why not simply register a separate quartz task instance for each report and let Quartz handle all the scheduling for you? That is after all the point behind it.
您可以仅创建单个线程,它会在某个时间间隔对“作业计划数据结构”执行 ping 操作,以查看是否需要运行报告。如果是,它将运行该报告,否则,它将进行短暂的小睡,并在指定的睡眠时间后再次执行 ping 操作。
如果一项工作需要太多时间才能完成并且您开始积累工作,则会导致问题。
作业调度数据结构将保持其按时间戳排序的记录。
you can create just single thread and it would ping a "job schedule data structure" at some time interval to see if it needs to run a report. If yes, it would run the report, otherwise, it would go for a short nap and ping again after specified sleep time.
It will cause problem if one job takes too much time to complete and you start accumulating jobs.
The job schedule data structure would keep its record sorted by time stamp.