Singleton in a Cluster

I needed to find a way to run a Quartz Cron Job on only one node in my cluster and failover to the second node should the first fail.

My first try was to deploy it to the deploy-hasingleton directory which was supposed to work as I wanted. Unfortunately my app which runs without problems in both the deploy and farm directories threw a whole lot of exceptions when I tried to deploy it in deploy-hasingleton. Since deploy-hasingleton doesn’t allow hot-deploy of apps which is a shame.

I did find a solution though.

@ResourceAdapter("quartz-ra.rar")
@MessageDriven(activationConfig = { 
   @ActivationConfigProperty(propertyName = "cronTrigger", 
                             propertyValue = "0 */1 * * * ?") })
public class MyJob implements Job
{
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException
    {
          System.err.println("Hello World!");
    }
}

The code above creates a job which will print “Hello World!” to System.err every minute on the minute as you’d expect from a cron job. In a clustered environment it will do so on every node.

Modifying the code to

@ResourceAdapter("quartz-ra.rar")
@MessageDriven(activationConfig = {
   @ActivationConfigProperty(propertyName = "cronTrigger",
                             propertyValue = "0 */1 * * * ?")
})
@Depends("jboss.ha:service=HASingletonDeployer,type=Barrier")
public class MyJob implements Job
{
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException
    {
          System.err.println("Hello World!");
    }
}

will make it so that the job will only run if the node it is running on is considered the Master node by the HASingletonDeployer. Kill the node it is running on and it will start running on the node that became the new master. 🙂

No comments yet

Leave a comment