001
014
015 package com.liferay.portal.kernel.backgroundtask;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018 import com.liferay.portal.kernel.lock.DuplicateLockException;
019 import com.liferay.portal.kernel.lock.Lock;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.model.BackgroundTask;
023
024
027 public class SerialBackgroundTaskExecutor
028 extends DelegatingBackgroundTaskExecutor {
029
030 public SerialBackgroundTaskExecutor(
031 BackgroundTaskExecutor backgroundTaskExecutor) {
032
033 super(backgroundTaskExecutor);
034 }
035
036 @Override
037 public BackgroundTaskResult execute(BackgroundTask backgroundTask)
038 throws Exception {
039
040 Lock lock = null;
041
042 try {
043 if (isSerial()) {
044 lock = acquireLock(backgroundTask);
045 }
046
047 BackgroundTaskExecutor backgroundTaskExecutor =
048 getBackgroundTaskExecutor();
049
050 return backgroundTaskExecutor.execute(backgroundTask);
051 }
052 finally {
053 if (lock != null) {
054 BackgroundTaskLockHelperUtil.unlockBackgroundTask(
055 backgroundTask);
056 }
057 }
058 }
059
060 protected Lock acquireLock(BackgroundTask backgroundTask)
061 throws DuplicateLockException {
062
063 Lock lock = null;
064
065 while (true) {
066 try {
067 lock = BackgroundTaskLockHelperUtil.lockBackgroundTask(
068 backgroundTask);
069
070 break;
071 }
072 catch (SystemException se) {
073 if (_log.isDebugEnabled()) {
074 _log.debug("Unable to acquire acquiring lock", se);
075 }
076
077 try {
078 Thread.sleep(50);
079 }
080 catch (InterruptedException ie) {
081 }
082 }
083 }
084
085 if (!lock.isNew()) {
086 throw new DuplicateLockException(lock);
087 }
088
089 return lock;
090 }
091
092 private static final Log _log = LogFactoryUtil.getLog(
093 SerialBackgroundTaskExecutor.class);
094
095 }