Scheduled tasks with multiple servers - a single point of responsibility

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

One of our applications is the cron \ scheduled tasks prefix.

After a short study, we found that the Spring framework provides a very direct solution for cron jobs, (Annotation Based Solution)

However, since both tomcats run the same webapp - if we use this Spring solution, we will create a very problematic scenario in which 2 clones work simultaneously (each on a different cat)

Is there any way to solve this problem? maybe this alternative is not suitable for our purpose?

thanks!

+7
spring tomcat cron scheduled-tasks
source share
2 answers

Typically, you will want to save a parameter indicating that the work is in progress. Just as Spring Batch does the trick, you can create a table in your database just to save the job. You can implement this however you want, but in the end, the scheduled tasks should check the database to see if the identical task is working, and if not, start the task. After completing the task, update the database accordingly so that you can continue.

+5
source share

The @kungfuters solution is by far the best ultimate goal, but as a simple first implementation, you can use the property to enable / disable tasks and perform tasks on only one of the servers.

-one
source share

All Articles