Search code examples
javaquartz-schedulerjobs

How to avoid such scenario?


I have a piece of the same software, running on 2 servers. Each of this piece of software runs Quartz that lunch at a specific time a job to be executed. Presuming that both servers have the clocks synchronized, these 2 jobs will start at the same time doing the same thing... How can I do that only one job to run and the other one don't ?

I have a database also, and my first thought was to make a table where to insert a line when the job starts and also to verify if there is a record for current day (if so then skip job execution...) But again, if the clocks on servers are perfectly synchronized then both apps will write and check at the same time, making useless this mechanism.

What other solution can I implement ?


Solution

  • Cluster 'em!

    Quartz cluster
    (source: quartz-scheduler.org)

    Basically both of your Quartz schedulers use the same database to synchronize and will only run job on one (idle) machine.

    But again, if the clocks on servers are perfectly synchronized then both apps will write and check at the same time, making useless this mechanism.

    This exactly what Quartz does! However it uses some database locking/transaction mechanisms so that when one instance fetches new jobs, the second one must wait.


    Presuming that both servers have the clocks synchronized

    They must have synchronized clocks to run in a cluster:

    Never run clustering on separate machines, unless their clocks are synchronized using some form of time-sync service