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

📄 jmssession.java

📁 一个java方面的消息订阅发送的源码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/**
 * 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.2 2005/03/18 03:36:37 tanderson Exp $
 */
package org.exolab.jms.client;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.IllegalStateException;
import javax.jms.InvalidDestinationException;
import javax.jms.InvalidSelectorException;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TemporaryQueue;
import javax.jms.TemporaryTopic;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicSubscriber;

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;
import org.exolab.jms.server.ServerSession;


/**
 * Client implementation of the <code>javax.jms.Session</code> interface.
 *
 * @author <a href="mailto:jima@exoffice.com">Jim Alateras</a>
 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
 * @version $Revision: 1.2 $ $Date: 2005/03/18 03:36:37 $
 */
class JmsSession implements Session, JmsMessageListener, MessageSessionIfc {

    /**
     * The owner of the session.
     */
    private JmsConnection _connection;

    /**
     * The proxy to the remote session implementation
     */
    private ServerSession _session = null;

    /**
     * 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;

    /**
     * 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;

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

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

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

    /**
     * This is the session's session listener which is used to receive all
     * messages associated with all consumers registered with this session.
     */
    private MessageListener _listener = null;

    /**
     * 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
        _session = connection.getServerConnection().createSession(_ackMode,
                                                                transacted);

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

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

    /**
     * Creates a <code>BytesMessage</code> object. A <code>BytesMessage</code>
     * object is used to send a message containing a stream of uninterpreted
     * bytes.
     *
     * @throws JMSException if the JMS provider fails to create this message due
     *                      to some internal error.
     */
    public BytesMessage createBytesMessage() throws JMSException {
        ensureOpen();
        return new BytesMessageImpl();
    }

    /**
     * Creates a <code>MapMessage</code> object. A <code>MapMessage</code>
     * object is used to send a self-defining set of name-value pairs, where
     * names are <code>String</code> objects and values are primitive values in
     * the Java programming language.
     *
     * @throws JMSException if the JMS provider fails to create this message due
     *                      to some internal error.
     */
    public MapMessage createMapMessage() throws JMSException {
        ensureOpen();
        return new MapMessageImpl();
    }

    /**
     * Creates a <code>Message</code> object. The <code>Message</code> interface
     * is the root interface of all JMS messages. A <code>Message</code> object
     * holds all the standard message header information. It can be sent when a
     * message containing only header information is sufficient.
     *
     * @throws JMSException if the JMS provider fails to create this message due
     *                      to some internal error.
     */
    public Message createMessage() throws JMSException {
        ensureOpen();
        return new MessageImpl();
    }

    /**
     * Creates an <code>ObjectMessage</code> object. An <code>ObjectMessage</code>
     * object is used to send a message that contains a serializable Java
     * object.
     *
     * @throws JMSException if the JMS provider fails to create this message due
     *                      to some internal error.
     */
    public ObjectMessage createObjectMessage() throws JMSException {
        ensureOpen();
        return new ObjectMessageImpl();
    }

    /**
     * Creates an initialized <code>ObjectMessage</code> object. An
     * <code>ObjectMessage</code> object is used to send a message that contains
     * a serializable Java object.
     *
     * @param object the object to use to initialize this message
     * @throws JMSException if the JMS provider fails to create this message due
     *                      to some internal error.
     */
    public ObjectMessage createObjectMessage(Serializable object)
            throws JMSException {
        ensureOpen();
        ObjectMessageImpl result = new ObjectMessageImpl();
        result.setObject(object);
        return result;
    }

    /**
     * Creates a <code>StreamMessage</code> object. A <code>StreamMessage</code>
     * object is used to send a self-defining stream of primitive values in the
     * Java programming language.
     *
     * @throws JMSException if the JMS provider fails to create this message due
     *                      to some internal error.
     */
    public StreamMessage createStreamMessage() throws JMSException {
        ensureOpen();
        return new StreamMessageImpl();
    }

    /**
     * Creates a <code>TextMessage</code> object. A <code>TextMessage</code>
     * object is used to send a message containing a <code>String</code>
     * object.
     *
     * @throws JMSException if the JMS provider fails to create this message due
     *                      to some internal error.
     */
    public TextMessage createTextMessage() throws JMSException {
        ensureOpen();
        return new TextMessageImpl();
    }

    /**
     * Creates an initialized <code>TextMessage</code> object. A
     * <code>TextMessage</code> object is used to send a message containing a
     * <code>String</code>.
     *
     * @param text the string used to initialize this message
     * @throws JMSException if the JMS provider fails to create this message due
     *                      to some internal error.
     */
    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;
    }

    /**
     * Returns the acknowledgement mode of the session. The acknowledgement mode
     * is set at the time that the session is created. If the session is
     * transacted, the acknowledgement mode is ignored.
     *

⌨️ 快捷键说明

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