Search code examples
liferayquartz-schedulerliferay-6

Dont run job when it is already running


I have a job scheduled in Liferay, if for example this job is running every 5 minutes and needs more than 5 minutes to complete, how is it handled in Liferay?

What I have observed is that the job will just start again, this could result in problems for me.

Is it somehow possible to not trigger a job, when it is already running?

*using liferay 6.0.6

Thanks


Solution

  • Try LockLocalServiceUtil and its methods lock(), unlock() and isLocked(). Something like this:

    try {
        if (LockLocalServiceUtil.isLocked()) {
            return;
        }
        LockLocalServiceUtil.lock();
    
        // do your job
    } finally {
        LockLocalServiceUtil.unlock();
    }
    

    Locks are stored in database so there will be no problem in a cluster environment.