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