Tator
Tator

Reputation: 556

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

Upvotes: 0

Views: 1139

Answers (2)

milos.zikmund
milos.zikmund

Reputation: 206

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.

Upvotes: 1

yannicuLar
yannicuLar

Reputation: 3133

This might not be the best practice but here goes

    private static boolean runningJob = false;
@Override
public void receive(Message arg0) throws MessageListenerException {

    if(runningJob)
        return;

    runningJob = true; //marking that a job just started
    //Do stuff  { ....... } // this might take a while
    runningJob = false;
}

Upvotes: 0

Related Questions