PublishedItemTask.java 2.57 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile: $
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
7 8
 *
 * This software is published under the terms of the GNU Public License (GPL),
9 10
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
 */

package org.jivesoftware.openfire.pubsub;

import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;

import java.util.Queue;
import java.util.TimerTask;

/**
 * A timed maintenance task that updates the database by adding and/or
 * removing <code>PublishedItem</code>s in regular intervals.
 * 
 * @author Matt Tucker
 */
public class PublishedItemTask extends TimerTask {
    /**
     * Queue that holds the items that need to be added to the database.
     */
    private Queue<PublishedItem> itemsToAdd = null;

    /**
     * Queue that holds the items that need to be deleted from the database.
     */
    private Queue<PublishedItem> itemsToDelete = null;

    /**
     * The service to perform the published item tasks on.
     */
    private PubSubService service = null;

    /**
     * The number of items to save on each run of the maintenance process.
     */
    private int items_batch_size = 50;

    public PublishedItemTask(PubSubService service) {
        this.service = service;
        this.itemsToAdd = service.getItemsToAdd();
        this.itemsToDelete = service.getItemsToDelete();
    }

    public void run() {
        try {
            PublishedItem entry;
            boolean success;
            // Delete from the database items contained in the itemsToDelete queue
            for (int index = 0; index <= items_batch_size && !itemsToDelete.isEmpty(); index++) {
                entry = itemsToDelete.poll();
                if (entry != null) {
                    success = PubSubPersistenceManager.removePublishedItem(service, entry);
                    if (!success) {
                        itemsToDelete.add(entry);
                    }
                }
            }
            // Save to the database items contained in the itemsToAdd queue
            for (int index = 0; index <= items_batch_size && !itemsToAdd.isEmpty(); index++) {
                entry = itemsToAdd.poll();
                if (entry != null) {
                    success = PubSubPersistenceManager.createPublishedItem(service, entry);
                    if (!success) {
                        itemsToAdd.add(entry);
                    }
                }
            }
        } catch (Throwable e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
    }
}