Search in sources :

Example 1 with LinkedListNode

use of org.jivesoftware.util.LinkedListNode in project Openfire by igniterealtime.

the class PubSubPersistenceManager method flushPendingItems.

/**
     * Flush the cache(s) of items to be persisted (itemsToAdd) and deleted (itemsToDelete).
     * @param sendToCluster If true, delegate to cluster members, otherwise local only
     */
public static void flushPendingItems(boolean sendToCluster) {
    // forward to other cluster members and wait for response
    if (sendToCluster) {
        CacheFactory.doSynchronousClusterTask(new FlushTask(), false);
    }
    if (itemsToAdd.getFirst() == null && itemsToDelete.getFirst() == null) {
        // nothing to do for this cluster member
        return;
    }
    Connection con = null;
    boolean rollback = false;
    LinkedList<RetryWrapper> addList = null;
    LinkedList<PublishedItem> delList = null;
    // while not blocking new entries from being cached.
    synchronized (itemsPending) {
        addList = itemsToAdd;
        delList = itemsToDelete;
        itemsToAdd = new LinkedList<>();
        itemsToDelete = new LinkedList<>();
        // Ensure pending items are available via the item read cache;
        // this allows the item(s) to be fetched by other request threads
        // while being written to the DB from this thread
        int copied = 0;
        for (String key : itemsPending.keySet()) {
            if (!itemCache.containsKey(key)) {
                itemCache.put(key, (((RetryWrapper) itemsPending.get(key).object)).get());
                copied++;
            }
        }
        if (log.isDebugEnabled() && copied > 0) {
            log.debug("Added " + copied + " pending items to published item cache");
        }
        itemsPending.clear();
    }
    // will be returned to the pending item write cache.
    try {
        con = DbConnectionManager.getTransactionConnection();
        writePendingItems(con, addList, delList);
    } catch (SQLException se) {
        log.error("Failed to flush pending items; initiating rollback", se);
        // return new items to the write cache
        LinkedListNode<RetryWrapper> node = addList.getLast();
        while (node != null) {
            savePublishedItem(node.object);
            node.remove();
            node = addList.getLast();
        }
        rollback = true;
    } finally {
        DbConnectionManager.closeTransactionConnection(con, rollback);
    }
}
Also used : FlushTask(org.jivesoftware.openfire.pubsub.cluster.FlushTask) SQLException(java.sql.SQLException) Connection(java.sql.Connection) LinkedListNode(org.jivesoftware.util.LinkedListNode)

Aggregations

Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 FlushTask (org.jivesoftware.openfire.pubsub.cluster.FlushTask)1 LinkedListNode (org.jivesoftware.util.LinkedListNode)1