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    
023    /**
024     * @author Michael C. Han
025     */
026    public class SerialBackgroundTaskExecutor
027            extends DelegatingBackgroundTaskExecutor {
028    
029            public SerialBackgroundTaskExecutor(
030                    BackgroundTaskExecutor backgroundTaskExecutor) {
031    
032                    super(backgroundTaskExecutor);
033            }
034    
035            @Override
036            public BackgroundTaskExecutor clone() {
037                    BackgroundTaskExecutor backgroundTaskExecutor =
038                            new SerialBackgroundTaskExecutor(getBackgroundTaskExecutor());
039    
040                    return backgroundTaskExecutor;
041            }
042    
043            @Override
044            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
045                    throws Exception {
046    
047                    Lock lock = null;
048    
049                    try {
050                            if (isSerial()) {
051                                    lock = acquireLock(backgroundTask);
052                            }
053    
054                            BackgroundTaskExecutor backgroundTaskExecutor =
055                                    getBackgroundTaskExecutor();
056    
057                            return backgroundTaskExecutor.execute(backgroundTask);
058                    }
059                    finally {
060                            if (lock != null) {
061                                    BackgroundTaskLockHelperUtil.unlockBackgroundTask(
062                                            backgroundTask);
063                            }
064                    }
065            }
066    
067            protected Lock acquireLock(BackgroundTask backgroundTask)
068                    throws DuplicateLockException {
069    
070                    Lock lock = null;
071    
072                    while (true) {
073                            try {
074                                    lock = BackgroundTaskLockHelperUtil.lockBackgroundTask(
075                                            backgroundTask);
076    
077                                    break;
078                            }
079                            catch (SystemException se) {
080                                    if (_log.isDebugEnabled()) {
081                                            _log.debug("Unable to acquire acquiring lock", se);
082                                    }
083    
084                                    try {
085                                            Thread.sleep(50);
086                                    }
087                                    catch (InterruptedException ie) {
088                                    }
089                            }
090                    }
091    
092                    if (!lock.isNew()) {
093                            throw new DuplicateLockException(lock);
094                    }
095    
096                    return lock;
097            }
098    
099            private static final Log _log = LogFactoryUtil.getLog(
100                    SerialBackgroundTaskExecutor.class);
101    
102    }