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

📄 consumerendpoint.java

📁 实现了Jms的服务器源码,支持多种适配器,DB,FTP,支持多种数据库
💻 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-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: ConsumerEndpoint.java,v 1.36 2003/09/25 11:23:13 tanderson Exp $
 *
 * Date         Author  Changes
 * 3/1/2001     jima    Created
 */
package org.exolab.jms.messagemgr;

import java.io.Serializable;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Vector;

import javax.jms.InvalidSelectorException;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.transaction.TransactionManager;

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

import org.exolab.jms.Identifiable;
import org.exolab.jms.client.JmsDestination;
import org.exolab.jms.message.MessageHandle;
import org.exolab.jms.message.MessageImpl;
import org.exolab.jms.persistence.PersistenceException;
import org.exolab.jms.scheduler.Scheduler;
import org.exolab.jms.selector.Selector;
import org.exolab.jms.server.JmsServerSession;
import org.exolab.jms.util.UUID;


/**
 * A Consumer is a message subscriber with a unique identity
 *
 * @version     $Revision: 1.36 $ $Date: 2003/09/25 11:23:13 $
 * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
 * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
 */
abstract public class ConsumerEndpoint
    implements Serializable, Identifiable, DestinationCacheEventListener,
    Runnable {

    /**
     * The identity of the consumer
     */
    private String _id;

    /**
     * The client identity, which uniquely identifies the remote client
     * within a session. This is used to tag messages when they are
     * asynchronously delivered to the client
     */
    private long _clientId = -1;

    /**
     * The selector assoicated with this consumer. A selector is used
     * to filter messages.
     */
    protected Selector _selector = null;

    /**
     * Serializes access to the {@link #_waitingForMessage} flag. This is
     * only required when it is changed
     */
    protected final Object _waitingForMessageMonitor = new Object();

    /**
     * Used to block consumer until there is a message available
     */
    protected boolean _waitingForMessage = false;;

    /**
     * Holds the consumer's message listener. This means that messages
     * will be pushed down
     */
    protected InternalMessageListener _listener = null;

    /**
     * Maintains the maximum size of this cache
     */
    protected int _size = 1000;

    /**
     * This is the scheduler that is used to deliver messages if a consumer
     * has a registered listener
     */
    protected transient Scheduler _scheduler = null;

    /**
     * The acknowledgement mode for this endpoint.
     */
    protected transient int _ackMode = Session.AUTO_ACKNOWLEDGE;

    /**
     * The nolocal indicator, if set, inhibits consuming messages that have
     * been published on the same connection
     */
    protected transient boolean _nolocal = false;

    /**
     * Indicates whether this endpoint belongs to a transacted session
     */
    protected transient boolean _transacted = false;

    /**
     * Holds the connection id of the connection that the endpoint belongs too
     */
    protected transient int _connectionId = -1;

    /**
     * caches the session that created this endpoint
     */
    protected JmsServerSession _session = null;

    /**
     * This determines whether message delivery to the registered listener
     * is enabled or disabled.
     */
    private volatile boolean _stopped = true;

    /**
     * Identifies this endpoint as being closed
     */
    private volatile boolean _closed = false;

    /**
     * Indicates whether the this cache has been scheduled with the dispatcher
     * for asynchronous message delivery.
     */
    private boolean _scheduled = false;

    /**
     * Cache of all messages and handles for this consumer.
     */
    private MessageCache _cache = new MessageCache();

    /**
     * Synchronization helper for close() and deliverMessages()
     */
    private final Object _lock = new Object();

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


    /**
     * Construct a <code>ConsumerEndpoint</code>.</p>
     * The destination and selector determine where it will be sourcing
     * its messages from, and scheduler is used to asynchronously deliver
     * messages to the consumer.
     *
     * @param session - the owning session
     * @param clientId - uniquely identifies the remote client within session
     * @param selector - the selector attached to the consumer, if any.
     * @param scheduler - used to schedule async message delivery.
     * @throws InvalidSelectorException if the selector is not well formed
     */
    ConsumerEndpoint(JmsServerSession session, long clientId,
                     String selector, Scheduler scheduler)
        throws InvalidSelectorException {
        _id = UUID.next();
        _selector = (selector != null) ? new Selector(selector) : null;
        _clientId = clientId;
        _scheduler = scheduler;
        _session = session;
    }

    /**
     * Return the destination that this consumer is subscribed to
     *
     * @return the destination that this consumer is subscribed to
     */
    public abstract JmsDestination getDestination();

    // implementation of Identifiable.getId
    public String getId() {
        return _id;
    }

    /**
     * Returns the persistent identifier for this consumer.<p/>
     * This is the identity of the consumer which is persistent across
     * subscriptions and server restarts. <p/>
     *
     * This implementation simply returns the transient identifier, i.e,
     * {@link #getId}
     *
     * @return the persistent identifier for this consumer
     */
    public String getPersistentId() {
        return getId();
    }

    // implementation of Object.hashCode
    public int hashCode() {
        return _id.hashCode();
    }

    /**
     * Return a stringified version of the consumer
     *
     * @return String
     */
    public String toString() {
        return _id + ":" + getDestination();
    }

    /**
     * Unregister this consumer for the specified destination cache, so that it
     * will stop receiving messages from it.
     */
    public abstract void unregister();

    /**
     * Return a reference to the client identity. This is an indirect reference
     * back to the remote client, which can asynchronously receive messages
     *
     * @return identity of the client scoped to the session
     */
    public long getClientId() {
        return _clientId;
    }

    /**
     * Return the selector for this endpoint or null if one is not specified
     *
     * @return String - the endpoint's selector
     */
    public Selector getSelector() {
        return _selector;
    }

    /**
     * Set the selector for this endpoint
     *
     * @param selector - message selector as a string
     * @exception InvalidSelectorException - raised when selector is not
     *                                       well-formed
     */
    public void setSelector(String selector)
        throws InvalidSelectorException {
        _selector = (selector != null) ? new Selector(selector) : null;
    }

    /**
     * Return the ackmode for this endpoint
     *
     * @return int - acknowledgement mode
     */
    public int getAckMode() {
        return _ackMode;
    }

    /**
     * Set the ackMode for this endpoint
     *
     * @param ackmode - the new ack mode for the endpoint
     */
    public void setAckMode(int ackmode) {
        _ackMode = ackmode;
    }

    /**
     * Return the connection id that this endpoint belongs to
     *
     * @return the connection id
     */
    public int getConnectionId() {
        return _connectionId;
    }

    /**
     * Set the connection that this endpooint belongs too
     *
     * @param id - connection identity
     */
    public void setConnectionId(int id) {
        _connectionId = id;
    }

    /**
     * Return the value of the nolocal indicator
     *
     * @return boolean
     */
    public boolean getNoLocal() {
        return _nolocal;
    }

    /**
     * Set the no local indicator for this endpoint
     *
     * @param nolocal - true to inhibit messages published on this connection
     */
    public void setNoLocal(boolean nolocal) {
        _nolocal = nolocal;
    }

    /**
     * Check whether this endpoint belongs to a transacted session
     *
     * @return boolean - true if it does
     */
    public boolean getTransacted() {
        return _transacted;
    }

    /**
     * Set the state if the transacted flag for this endpoint
     *
     * @param transacted - true if it is transacted
     */
    public void setTransacted(boolean transacted) {
        _transacted = transacted;
    }

    /**
     * Set the maximum size of the cache. If there are more than this number
     * of messages in the cache the {@link CacheEvictionPolicy} is enforced
     * to remove messages.
     *
     * @param size - maximum number of messages a destination can hold
     */
    public void setMaximumSize(int size) {
        _size = size;
    }

    /**
     * Return the cache's maximum size
     *
     * @return int - size of cache
     */
    public int getMaximumSize() {
        return _size;
    }

    /**
     * Set the {@link CacheEvictionPolicy} for this object. This determines
     * how messages are removed when the cache's upper limit is reached.
     *
     * @param policy the eviction policy
     */
    public void setCacheEvictionPolicy(CacheEvictionPolicy policy) {
        // not implemented
    }

    /**
     * Return the number of unsent messages in the cache for this consumer
     *
     * @return the number of unsent messages
     */
    public int getMessageCount() {
        return _cache.getHandleCount();
    }

    /**
     * Return a reference to the session owning this endpoint
     *
     * @return JmsServerSession - the owning session

⌨️ 快捷键说明

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