cancel
Showing results for 
Search instead for 
Did you mean: 

Cluster aware scheduled jobs

nikes
Champ on-the-rise
Champ on-the-rise
Hello,

We have setup a cluster of 2 nodes (active/active) and would like to make our custom scheduled jobs cluster aware. Means
Job will be configured on both the node, but should get executed on one node only and not on both.

I am exploring JobLockService for this. any one tried before and would like to share example code?

Thanks
4 REPLIES 4

mrogers
Star Contributor
Star Contributor
The JobLockService is the way to do this.  

Basically the jobs can execute on any node in the cluster but only one at a time.   It allows you to take a system wide lock with a timeout (so if your system is shut down or stops the lock will expire.)  And in addition you can extend the lock as your job progresses, you can either do that explicitly or you can set up a callback that calls you back to say is your job still alive.

Look at the alfresco source code for examples of its use.

nikes
Champ on-the-rise
Champ on-the-rise

patil
Champ on-the-rise
Champ on-the-rise
Another way of doing it is by extending the AbstractScheduledLockedJob class and overriding
   public void executeJob(JobExecutionContext context)
         throws JobExecutionException {} method.


The parent class manages locking using the method

  /**
     * Lazily update the job lock
     */
    private void refreshLock()
    {
        Pair<Long, String> lockPair = lockThreadLocal.get();
        if (lockPair == null)
        {
            String lockToken = jobLockService.getLock(lockQName, LOCK_TTL);
            Long lastLock = new Long(System.currentTimeMillis());
            // We have not locked before
            lockPair = new Pair<Long, String>(lastLock, lockToken);
            lockThreadLocal.set(lockPair);
        }
        else
        {
            long now = System.currentTimeMillis();
            long lastLock = lockPair.getFirst().longValue();
            String lockToken = lockPair.getSecond();
            // Only refresh the lock if we are past a threshold
            if (now - lastLock > (long) (LOCK_TTL / 2L))
            {
                jobLockService.refreshLock(lockToken, lockQName, LOCK_TTL);
                lastLock = System.currentTimeMillis();
                lockPair = new Pair<Long, String>(lastLock, lockToken);
                lockThreadLocal.set(lockPair);
            }
        }
    }


Thanks,
Patil

mkopacz
Champ in-the-making
Champ in-the-making
Hi, I have a cluster consisting of two machines with Alfresco 5.0 Enterprise installed and would like to create a job that gets executed on only one of these machines at a time. I have followed this article http://docs.alfresco.com/5.1/references/dev-extension-points-scheduled-jobs.html, but without success - my job executes simultaneously on both nodes. Do you have any suggestions?