Clustering scheduled jobs with Quartz and Terracotta

We’re using Quartz to handle scheduled jobs in our java applications. Since we cluster our apps for failover using Terracotta, we need to address the issue of how to failover scheduled jobs (we only want each job to execute exactly once, but for fault tolerance it should be scheduled on multiple servers).

Quartz handles this situation with clustered job stores and Terracotta provides a job store for Quartz. Its easy to use – just add the quartz-terracotta jar (you’ll also need a quartz version > 1.7 – I’m using 1.8.4 below).

[sourcecode language=”xml”]

org.quartz-scheduler
quartz
1.8.4


org.terracotta.quartz
quartz-terracotta
1.2.0

[/sourcecode]

then set two systems properties. I’m doing it programmatically below because I use a commandline option to enable clustering in applications using embedded jetty. The “terracotta” commandline option is a terracotta url which tells the terracotta client where its servers are (something like “terracotta:9510,terracotta2:9510″ for a fault-tolerant pair of terracotta servers).

[sourcecode language=”java”]
if (cmd.hasOption(“terracotta”)) {
System.setProperty(“org.quartz.jobStore.class”, “org.terracotta.quartz.TerracottaJobStore”);
System.setProperty(“org.quartz.jobStore.tcConfigUrl”, cmd.getOptionValue(“terracotta”));
}
[/sourcecode]

That’s it basically. The only additional thing you have to take care of is that while setting up the scheduled job, you need to be aware that another member of the cluster may have already set it up, so you should expect an ObjectAlreadyExistsException while setting up the quartz job.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.