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

📄 jmssession.java

📁 实现了Jms的服务器源码,支持多种适配器,DB,FTP,支持多种数据库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**
 * 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 2000-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: JmsSession.java,v 1.59 2004/01/21 04:08:13 tanderson Exp $
 */
package org.exolab.jms.client;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.jms.BytesMessage;
import javax.jms.IllegalStateException;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;

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

import org.exolab.jms.message.BytesMessageImpl;
import org.exolab.jms.message.MapMessageImpl;
import org.exolab.jms.message.MessageConverter;
import org.exolab.jms.message.MessageConverterFactory;
import org.exolab.jms.message.MessageImpl;
import org.exolab.jms.message.MessageSessionIfc;
import org.exolab.jms.message.ObjectMessageImpl;
import org.exolab.jms.message.StreamMessageImpl;
import org.exolab.jms.message.TextMessageImpl;


/**
 * This class implements a Session interface and supports a single threaded
 * context. A session supports multiple consumers and producers but only within
 * the context of a single thread.
 *
 * @version     $Revision: 1.59 $ $Date: 2004/01/21 04:08:13 $
 * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
 * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
 */
abstract class JmsSession
    implements Session, JmsMessageListener, MessageSessionIfc {

    /**
     * If true, indicates that the session has been closed
     */
    private volatile boolean _closed = false;

    /**
     * If true, indicates that the session is in the process of being closed
     */
    private volatile boolean _closing = false;

    /**
     * This flag determines whether message delivery is enabled or disabled.
     * Message delivery if disabled if the enclosing connection is stopped.
     */
    private volatile boolean _stopped = true;

    /**
     * If true, indicates that the session has been stopped. When started,
     * messages may be sent
     */
    private volatile boolean _started = false;

    /**
     * A transacted session is bounded by successive commit. If this variable
     * set to true then this session is transacted. This implies that the
     * session is always in a transaction and transactions are demarcated by
     * commit or rollback.
     */
    private final boolean _transacted;

    /**
     * Indicates whether the consumer or the client will
     * acknowledge any messages it receives. Ignored if the session is
     * transacted. Legal values are
     * <code>Session.AUTO_ACKNOWLEDGE</code>,
     * <code>Session.CLIENT_ACKNOWLEDGE</code> and
     * <code>Session.DUPS_OK_ACKNOWLEDGE</code>.
     */
    private final int _ackMode;

    /**
     * This is the owner of the session. It also maintains a remote stub to
     * the server which it can use for its remote work.
     */
    private JmsConnection _connection;

    /**
     * Maintains the a map of JmsMessageConsumer.getClientId() ->
     * JmsMessageConsumer objects
     */
    private Hashtable _consumers = new Hashtable();

    /**
     * Maintains a list of producers for the session
     */
    private Vector _producers = new Vector();

    /**
     * Maintain a collection of acked messages for this transacted
     * session. These messages are only sent to the server on commit.
     */
    private Vector _messagesToSend = new Vector();

    /**
     * The identifier of the session
     */
    private final String _sessionId;

    /**
     * Maintain a copy of the stub that connects this machine to the remote
     * server.
     */
    private JmsSessionStubIfc _stub = null;

    /**
     * This is the session's session listener which is used to receive all
     * messages associated with all consumers registered with this session.
     * Even if consumers have registered listeners messages for those consumers
     * will go to the session's message listener instead.
     */
    private MessageListener _listener = null;

    /**
     * The consumer Id seed to allocate to new consumers
     */
    private long _consumerIdSeed = 0;

    /**
     * Tracks the number of messages sent by this session
     */
    private long _publishCount;

    /**
     * The message cache holds all messages for the session, allocated by
     * a JmsConnectionConsumer.
     */
    private Vector _messageCache = new Vector();

    /**
     * Monitor used to block consumers, if the session has been
     * stopped, or no messages are available
     */
    private final Object _receiveLock = new Object();

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


    /**
     * Construct a new <code>JmsSession</code>
     *
     * @param connection the owner of the session
     * @param transacted if <code>true</code>, the session is transacted.
     * @param ackMode indicates whether the consumer or the client will
     * acknowledge any messages it receives. This parameter will be ignored if
     * the session is transacted. Legal values are
     * <code>Session.AUTO_ACKNOWLEDGE</code>,
     * <code>Session.CLIENT_ACKNOWLEDGE</code> and
     * <code>Session.DUPS_OK_ACKNOWLEDGE</code>.
     * @throws JMSException if the session cannot be created
     */
    public JmsSession(JmsConnection connection, boolean transacted,
                      int ackMode) throws JMSException {

        if (connection == null) {
            throw new IllegalArgumentException(
                "Argument 'connection' is null");
        }

        _connection = connection;
        _transacted = transacted;
        _ackMode = ackMode;

        // construct the remote stub
        _stub = connection.getJmsConnectionStub().createSession(
            _ackMode, transacted);
        _sessionId = _stub.getSessionId();

        // set up this instance to be a message listener
        _stub.setMessageListener(this);

        // now we need to check whether we should start the session
        if (!connection.isStopped()) {
            start();
        }
    }

    /**
     * Create a <code>BytesMessage</code>
     *
     * @return a new <code>BytesMessage</code>
     * @throws JMSException if the message can't be created
     */
    public BytesMessage createBytesMessage() throws JMSException {
        ensureOpen();
        return new BytesMessageImpl();
    }

    /**
     * Create a <code>MapMessage</code>
     *
     * @return a new <code>MapMessage</code>
     * @throws JMSException if the message can't be created
     */
    public MapMessage createMapMessage() throws JMSException {
        ensureOpen();
        return new MapMessageImpl();
    }

    /**
     * Create a <code>Message</code>
     *
     * @return a new <code>Message</code>
     * @throws JMSException if the message can't be created
     */
    public Message createMessage() throws JMSException {
        ensureOpen();
        return new MessageImpl();
    }

    /**
     * Create an <code>ObjectMessage</code>
     *
     * @return a new <code>ObjectMessage</code>
     * @throws JMSException if the message can't be created
     */
    public ObjectMessage createObjectMessage() throws JMSException {
        ensureOpen();
        return new ObjectMessageImpl();
    }

    /**
     * Create an <code>ObjectMessage</code>
     *
     * @param object the object to use to initialise the message
     * @return a new <code>ObjectMessage</code>
     * @throws JMSException if the message can't be created
     */
    public ObjectMessage createObjectMessage(Serializable object)
        throws JMSException {
        ensureOpen();
        ObjectMessageImpl result = new ObjectMessageImpl();
        result.setObject(object);
        return result;
    }

    /**
     * Create a <code>StreamMessage</code>
     *
     * @return a new <code>StreamMessage</code>
     * @throws JMSException if the message can't be created
     */
    public StreamMessage createStreamMessage() throws JMSException {
        ensureOpen();
        return new StreamMessageImpl();
    }

    /**
     * Create a <code>TextMessage</code>
     *
     * @return a new <code>TextMessage</code>
     * @throws JMSException if the message can't be created
     */
    public TextMessage createTextMessage() throws JMSException {
        ensureOpen();
        return new TextMessageImpl();
    }

    /**
     * Create a <code>TextMessage</code>
     *
     * @param text the text to use to initialise the message
     * @return a new <code>TextMessage</code>
     * @throws JMSException if the message can't be created
     */
    public TextMessage createTextMessage(String text) throws JMSException {
        ensureOpen();
        TextMessageImpl result = new TextMessageImpl();
        result.setText(text);
        return result;
    }

    /**
     * Determines if the session is transacted
     *
     * @return <code>true</code> if the session is transacted
     * @throws JMSException if the session is closed
     */
    public boolean getTransacted() throws JMSException {
        ensureOpen();
        return _transacted;
    }

    /**
     * Commit all messages done in this transaction
     *
     * @throws JMSException if the transaction cannot be committed
     */
    public synchronized void commit() throws JMSException {
        ensureOpen();
        ensureTransactional();

⌨️ 快捷键说明

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