⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queuedestinationcache.java

📁 一个java方面的消息订阅发送的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright
 *    statements and notices.  Redistributions must also contain a
 *    copy of this document.
 *
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. The name "Exolab" must not be used to endorse or promote
 *    products derived from this Software without prior written
 *    permission of Exoffice Technologies.  For written permission,
 *    please contact info@exolab.org.
 *
 * 4. Products derived from this Software may not be called "Exolab"
 *    nor may "Exolab" appear in their names without prior written
 *    permission of Exoffice Technologies. Exolab is a registered
 *    trademark of Exoffice Technologies.
 *
 * 5. Due credit should be given to the Exolab Project
 *    (http://www.exolab.org/).
 *
 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Copyright 2001-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: QueueDestinationCache.java,v 1.4 2005/03/24 13:41:23 tanderson Exp $
 */
package org.exolab.jms.messagemgr;

import java.sql.Connection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;
import javax.jms.JMSException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.exolab.jms.client.JmsDestination;
import org.exolab.jms.client.JmsQueue;
import org.exolab.jms.client.JmsTemporaryDestination;
import org.exolab.jms.message.MessageImpl;
import org.exolab.jms.persistence.DatabaseService;
import org.exolab.jms.persistence.PersistenceException;
import org.exolab.jms.selector.Selector;
import org.exolab.jms.server.JmsServerConnectionManager;


/**
 * A {@link DestinationCache} for queues.
 *
 * @author <a href="mailto:jima@comware.com.au">Jim Alateras</a>
 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
 * @version $Revision: 1.4 $ $Date: 2005/03/24 13:41:23 $
 */
public class QueueDestinationCache extends AbstractDestinationCache {

    /**
     * Maintains a list of {@link QueueConsumerMessageHandle} instances.
     */
    private MessageQueue _handles;

    /**
     * Maintains a list of queue browsers for this cache.
     */
    protected List _browsers = Collections.synchronizedList(new LinkedList());

    /**
     * Index of the last {@link QueueConsumerEndpoint} that received a message
     * from this destination.
     * If multiple consumers are attached to this queue then messages will be
     * sent to each in a round robin fashion
     */
    private int _lastConsumerIndex = 0;

    /**
     * The logger.
     */
    private static final Log _log = LogFactory.getLog(
            QueueDestinationCache.class);


    /**
     * Construct a new <code>QueueDestinationCache</code> for a non-persistent
     * queue.
     *
     * @param queue the queue to cache messages for
     */
    public QueueDestinationCache(JmsQueue queue) {
        super(queue);
        _handles = new MessageQueue();
    }

    /**
     * Construct a new <code>QueueDestinationCache</code> for a persistent
     * queue.
     *
     * @param queue      the queue to cache messages for
     * @param connection the database connection
     * @throws JMSException for any error
     * @throws PersistenceException if initialisation fails
     */
    public QueueDestinationCache(JmsQueue queue, Connection connection)
            throws JMSException, PersistenceException {
        super(queue, connection);
    }

    /**
     * A Queue can also hav a queue listener, which simply gets informed of all
     * messages that arrive at this destination.
     *
     * @param listener - queue listener
     */
    public void addQueueListener(QueueBrowserEndpoint listener) {
        // add if not present
        if (!_browsers.contains(listener)) {
            _browsers.add(listener);
        }
    }

    /**
     * Remove the queue listener associated with this cache
     *
     * @param listener - queue listener to remove
     */
    public void removeQueueListener(QueueBrowserEndpoint listener) {
        // add if not present
        if (_browsers.contains(listener)) {
            _browsers.remove(listener);
        }
    }

    /**
     * Invoked when the {@link MessageMgr} receives a non-persistent message.
     *
     * @param destination the message's destination
     * @param message     the message
     * @throws JMSException if the listener fails to handle the message
     */
    public void messageAdded(JmsDestination destination, MessageImpl message)
            throws JMSException {
        MessageRef reference = new CachedMessageRef(message, false,
                                                    getMessageCache());
        MessageHandle shared = new SharedMessageHandle(reference, message);
        MessageHandle handle = new QueueConsumerMessageHandle(shared);

        // all messages are added to this queue. Receivers will
        // then pick messages from it as required.
        addMessage(reference, message, handle);

        // if we have any registered consumers then we need to
        // send the message to one of them first.
        QueueConsumerEndpoint endpoint = getEndpointForMessage(message);
        if (endpoint != null) {
            endpoint.messageAdded(handle, message);
        }
    }

    /**
     * Invoked when the {@link MessageMgr} receives a persistent message.
     *
     * @param connection  the database connection
     * @param destination the message's destination
     * @param message     the message
     * @throws JMSException         if the listener fails to handle the message
     * @throws PersistenceException if there is a persistence related problem
     */
    public void persistentMessageAdded(Connection connection,
                                       JmsDestination destination,
                                       MessageImpl message)
            throws JMSException, PersistenceException {
        MessageRef reference = new CachedMessageRef(message, true,
                                                    getMessageCache());
        MessageHandle shared = new SharedMessageHandle(reference, message);
        MessageHandle handle = new QueueConsumerMessageHandle(shared);
        handle.add(connection);

        addMessage(reference, message, handle);

        // if there are any registered consumers, notify one of them that
        // a message has arrived
        QueueConsumerEndpoint endpoint = getEndpointForMessage(message);
        if (endpoint != null) {
            endpoint.persistentMessageAdded(handle, message, connection);
        }
    }

    /**
     * Returns the first available message matching the supplied message
     * selector.
     *
     * @param selector the message selector to use. May be <code>null</code>
     * @return handle to the first message, or <code>null</code> if there
     * are no messages, or none matching <code>selector</code>
     * @throws JMSException for any error
     */
    public synchronized MessageHandle getMessage(Selector selector)
            throws JMSException {
        QueueConsumerMessageHandle handle = null;
        if (selector == null) {
           // if no selector has been specified then remove and return
           // the first message
           handle = (QueueConsumerMessageHandle) _handles.removeFirst();
        } else {
            // for non null selector we must find the first matching
            MessageHandle[] handles = _handles.toArray();
            for (int i = 0; i < handles.length; ++i) {
                MessageHandle hdl = handles[i];
                if (selector.selects(hdl.getMessage())) {
                    handle = (QueueConsumerMessageHandle) hdl;
                    _handles.remove(handle);
                    break;
                }
            }
        }
        return handle;
    }

    /**
     * Playback all the messages in the cache to the specified {@link
     * QueueBrowserEndpoint}.
     *
     * @param browser the queue browser
     * @throws JMSException for any error
     */
    public void playbackMessages(QueueBrowserEndpoint browser)
            throws JMSException {
        MessageHandle[] handles = _handles.toArray();
        for (int i = 0; i < handles.length; ++i) {
            MessageHandle handle = handles[i];
            MessageImpl message = handle.getMessage();
            if (message != null) {
                browser.messageAdded(handle, message);
            }
        }
    }

    /**
     * Return a message handle back to the cache, to recover unsent or
     * unacknowledged messages.
     *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -