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