001    /**
002     * Copyright (c) 2000-2013 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.backgroundtask.messaging;
016    
017    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskConstants;
018    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskExecutor;
019    import com.liferay.portal.kernel.bean.BeanReference;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.messaging.BaseMessageListener;
022    import com.liferay.portal.kernel.messaging.Message;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.BackgroundTask;
025    import com.liferay.portal.service.BackgroundTaskLocalService;
026    import com.liferay.portal.service.LockLocalServiceUtil;
027    
028    /**
029     * @author Michael C. Han
030     */
031    public class BackgroundTaskQueuingMessageListener extends BaseMessageListener {
032    
033            @Override
034            protected void doReceive(Message message) throws Exception {
035                    String taskExecutorClassName = (String)message.get(
036                            "taskExecutorClassName");
037    
038                    if (Validator.isNull(taskExecutorClassName)) {
039                            return;
040                    }
041    
042                    int status = (Integer)message.get("status");
043    
044                    if ((status == BackgroundTaskConstants.STATUS_CANCELLED) ||
045                            (status == BackgroundTaskConstants.STATUS_FAILED) ||
046                            (status == BackgroundTaskConstants.STATUS_SUCCESSFUL)) {
047    
048                            executeQueuedBackgroundTasks(taskExecutorClassName);
049                    }
050                    else if (status == BackgroundTaskConstants.STATUS_QUEUED) {
051                            boolean locked = LockLocalServiceUtil.isLocked(
052                                    BackgroundTaskExecutor.class.getName(), taskExecutorClassName);
053    
054                            if (!locked) {
055                                    executeQueuedBackgroundTasks(taskExecutorClassName);
056                            }
057                    }
058            }
059    
060            private void executeQueuedBackgroundTasks(String taskExecutorClassName)
061                    throws SystemException {
062    
063                    BackgroundTask backgroundTask =
064                            _backgroundTaskLocalService.fetchFirstBackgroundTask(
065                                    taskExecutorClassName, BackgroundTaskConstants.STATUS_QUEUED);
066    
067                    if (backgroundTask == null) {
068                            return;
069                    }
070    
071                    _backgroundTaskLocalService.resumeBackgroundTask(
072                            backgroundTask.getBackgroundTaskId());
073            }
074    
075            @BeanReference(type = BackgroundTaskLocalService.class)
076            private BackgroundTaskLocalService _backgroundTaskLocalService;
077    
078    }