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.dao.orm.ORMException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.lock.DuplicateLockException;
020    import com.liferay.portal.kernel.lock.Lock;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
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 BackgroundTaskExecutor clone() {
038                    BackgroundTaskExecutor backgroundTaskExecutor =
039                            new SerialBackgroundTaskExecutor(getBackgroundTaskExecutor());
040    
041                    return backgroundTaskExecutor;
042            }
043    
044            @Override
045            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
046                    throws Exception {
047    
048                    Lock lock = null;
049    
050                    try {
051                            if (isSerial()) {
052                                    lock = acquireLock(backgroundTask);
053                            }
054    
055                            BackgroundTaskExecutor backgroundTaskExecutor =
056                                    getBackgroundTaskExecutor();
057    
058                            return backgroundTaskExecutor.execute(backgroundTask);
059                    }
060                    finally {
061                            if (lock != null) {
062                                    BackgroundTaskLockHelperUtil.unlockBackgroundTask(
063                                            backgroundTask);
064                            }
065                    }
066            }
067    
068            protected Lock acquireLock(BackgroundTask backgroundTask)
069                    throws DuplicateLockException {
070    
071                    Lock lock = null;
072    
073                    while (true) {
074                            try {
075                                    lock = BackgroundTaskLockHelperUtil.lockBackgroundTask(
076                                            backgroundTask);
077    
078                                    break;
079                            }
080                            catch (ORMException | SystemException e) {
081                                    if (_log.isDebugEnabled()) {
082                                            _log.debug("Unable to acquire acquiring lock", e);
083                                    }
084    
085                                    try {
086                                            Thread.sleep(50);
087                                    }
088                                    catch (InterruptedException ie) {
089                                    }
090                            }
091                    }
092    
093                    if (!lock.isNew()) {
094                            throw new DuplicateLockException(lock);
095                    }
096    
097                    return lock;
098            }
099    
100            private static final Log _log = LogFactoryUtil.getLog(
101                    SerialBackgroundTaskExecutor.class);
102    
103    }