QueuedTasksManager.java 2.21 KB
Newer Older
1 2 3 4 5 6 7
/**
 * $RCSfile: $
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2008-2008 Jive Software. All rights reserved.
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 */

package org.jivesoftware.openfire.muc.cluster;

import org.jivesoftware.openfire.cluster.ClusterManager;
import org.jivesoftware.util.TaskEngine;

import java.util.Queue;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Queue tasks while this JVM is joining the cluster and the requested room was still not loaded.
 *
 * @author Gaston Dombiak
 */
public class QueuedTasksManager {

    private static QueuedTasksManager instance = new QueuedTasksManager();

39
    private Queue<MUCRoomTask> taskQueue = new ConcurrentLinkedQueue<>();
40 41 42 43 44 45 46 47 48 49 50

    public static QueuedTasksManager getInstance() {
        return instance;
    }

    /**
     * Hide the constructor so no one can create other instances
     */
    private QueuedTasksManager() {
        // Register a periodic task that will execute queued tasks
        TaskEngine.getInstance().scheduleAtFixedRate(new TimerTask() {
51 52
            @Override
			public void run() {
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
                if (!ClusterManager.isClusteringStarting()) {
                    MUCRoomTask mucRoomTask;
                    while ((mucRoomTask = taskQueue.poll()) != null) {
                        mucRoomTask.run();
                    }
                }
            }
        }, 1000, 30000);
    }

    /**
     * Queues a task. The queued task will be executed once this JVM completed joining the cluster.
     * Moreover, if joining the cluster failed then the queue will also be consumed.
     *
     * @param task the task to queue.
     */
    public void addTask(MUCRoomTask task) {
        taskQueue.add(task);
    }
}