queuedestinationcache.java

来自「OpenJMS是一个开源的Java Message Service API 1.」· Java 代码 · 共 523 行 · 第 1/2 页

JAVA
523
字号
/** * 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.9 2006/06/09 12:58:56 tanderson Exp $ */package org.exolab.jms.messagemgr;import java.sql.Connection;import java.util.Collections;import java.util.Iterator;import java.util.LinkedList;import java.util.List;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.lease.LeaseManager;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.ServerConnectionManager;/** * 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.9 $ $Date: 2006/06/09 12:58:56 $ */public class QueueDestinationCache extends AbstractDestinationCache {    /**     * Maintains a list of {@link QueueConsumerMessageHandle} instances.     */    private final MessageQueue _handles = new MessageQueue();    /**     * Maintains a list of queue browsers for this cache.     */    private final List _browsers            = Collections.synchronizedList(new LinkedList());    /**     * The connection manager.     */    private final ServerConnectionManager _connections;    /**     * Synchronization helper.     */    private final Object _lock = new Object();    /**     * 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>.     *     * @param queue       the queue to cache messages for     * @param database    the database service     * @param leases      the lease manager     * @param connections the connection manager     * @throws JMSException if the cache can't be initialised     */    public QueueDestinationCache(JmsQueue queue,                                 DatabaseService database,                                 LeaseManager leases,                                 ServerConnectionManager connections)            throws JMSException {        super(queue, database, leases);        if (connections == null) {            throw new IllegalArgumentException(                    "Argument 'connections' is null");        }        _connections = connections;        if (queue.getPersistent()) {            init();        }    }    /**     * 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(this, 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.        ConsumerEndpoint consumer = getConsumerForMessage(message);        if (consumer != null) {            consumer.messageAdded(handle, message);        }    }    /**     * Invoked when the {@link MessageMgr} receives a persistent message.     *     * @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(JmsDestination destination,                                       MessageImpl message)            throws JMSException, PersistenceException {        MessageRef reference = new CachedMessageRef(message, true,                getMessageCache());        MessageHandle shared = new SharedMessageHandle(this, reference,                message);        MessageHandle handle = new QueueConsumerMessageHandle(shared);        handle.add();        addMessage(reference, message, handle);        // if there are any registered consumers, notify one of them that        // a message has arrived        ConsumerEndpoint consumer = getConsumerForMessage(message);        if (consumer != null) {            consumer.persistentMessageAdded(handle, message);        }    }    /**     * Returns the first available message matching the supplied message     * selector.     *     * @param selector the message selector to use. May be <code>null</code>     * @param cancel     * @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,                                                 Condition cancel)            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 && !cancel.get(); ++i) {                MessageHandle hdl = handles[i];                MessageImpl message = hdl.getMessage();                if (message != null && selector.selects(message)) {                    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) {

⌨️ 快捷键说明

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