具有多个服务器的计划任务 - 单点责任 [英] Scheduled tasks with multiple servers - single point of responsibility

查看:176
本文介绍了具有多个服务器的计划任务 - 单点责任的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Spring + JPA Web应用程序。
我们使用两个运行应用程序并使用同一个数据库的tomcat服务器。



我们的一个应用程序请求是预处理cron \\ p>

经过一个简短的研究,我们发现spring框架为cron工作提供了一个非常直接的解决方案:
(基于注释的解决方案)



然而,因为两个tomcat运行相同的webapp - 如果我们将使用这个春天的解决方案,我们将创建一个非常有问题的场景,其中2 crons同时运行(每个在不同的tomcat)



有没有办法解决这个问题?

解决方案

p>作为一般规则,您将要保存一个设置以指示作业正在运行。类似于Spring Batch的做法,你可能想在数据库中创建一个表,只是为了存储一个作业执行。您可以选择实现这一点,但最终,您的计划任务应该检查数据库,以查看是否一个相同的任务已经运行,如果没有,继续执行任务。任务完成后,请相应地更新数据库,以便将来的执行能够继续。


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.

这篇关于具有多个服务器的计划任务 - 单点责任的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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