001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
025     * @author Michael C. Han
026     */
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    }