Scheduled tasks with multiple servers

We have a Spring + JPA web application. We use two tomcat servers that run both application and uses the same DB.

One of our application requirmemnt is to preform cron scheduled tasks.

After a short research we found that spring framework delivers a very straight forward solution to cron jobs, (Annotation based solution)

However since both tomcats running the same webapp - if we will use this spring's solution we will create a very problematic scenario where 2 crons are running at the same time (each on a different tomcat)

Is there any way to solve this issue? maybe this alternative is not good for our purpose?

thanks!


As a general rule, you're going to want to save a setting to indicate that a job is running. Similar to how "Spring Batch" does the trick, you might want to create a table in your database simply for storing a job execution. You can choose to implement this however you'd like, but ultimately, your scheduled tasks should check the database to see if an identical task is already running, and if not, proceed with the task execution. Once the task has completed, update the database appropriately so that a future execution will be able to proceed.


@ kungfuters解决方案当然是一个更好的最终目标,但作为一个简单的第一个实现,您可以使用属性来启用/禁用任务,并且只将任务运行在其中一台服务器上。

链接地址: http://www.djcxy.com/p/75132.html

上一篇: 巨大的UDP延迟/滞后与Android

下一篇: 计划任务与多个服务器