📄 jmsserversession.java
字号:
/**
* 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-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: JmsServerSession.java,v 1.89 2003/08/17 01:32:26 tanderson Exp $
*
* Date Author Changes
* 04/07/2000 jima Created
* 04/25/2000 jima Changes to the interface
*/
package org.exolab.jms.server;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.InvalidDestinationException;
import javax.jms.InvalidSelectorException;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.transaction.TransactionManager;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.jms.JMSErrorCodes;
import org.exolab.jms.client.JmsMessageListener;
import org.exolab.jms.client.JmsQueue;
import org.exolab.jms.client.JmsTopic;
import org.exolab.jms.message.MessageHandle;
import org.exolab.jms.message.MessageId;
import org.exolab.jms.message.MessageImpl;
import org.exolab.jms.messagemgr.ConsumerEndpoint;
import org.exolab.jms.messagemgr.ConsumerManager;
import org.exolab.jms.messagemgr.DestinationManager;
import org.exolab.jms.messagemgr.InternalMessageListener;
import org.exolab.jms.messagemgr.MessageMgr;
import org.exolab.jms.messagemgr.QueueBrowserEndpoint;
import org.exolab.jms.messagemgr.ResourceManager;
import org.exolab.jms.messagemgr.ResourceManagerException;
/**
* A session represents a server side endpoint to the JMSServer. A client can
* create producers, consumers and destinations through the session in addi-
* tion to other functions. A session has a unique identifer which is a comb-
* ination of clientId-connectionId-sessionId.
* <p>
* A session represents a single-threaded context which implies that it cannot
* be used with more than one thread concurrently. Threads registered with this
* session are synchronized.
* <p>
* Finally, instances of this object can only be created by classes within the
* same package.
*
* @version $Revision: 1.89 $ $Date: 2003/08/17 01:32:26 $
* @author <a href="mailto:jima@exoffice.com">Jim Alateras</a>
* @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
* @see JmsServerConnection
*/
public class JmsServerSession
implements InternalMessageListener, XAResource {
/**
* The client identifies the owner of the session.
*/
private String _clientId = null;
/**
* The session identifier uniquely distinguishes this session from any
* other session. No two sessions can have the same session identifier.
* This is allocated by the connection that created this session
*/
private String _sessionId = null;
/**
* Back pointer to the connection that created this session. This
* is set during object creation time
*/
private JmsServerConnection _connection = null;
/**
* Maintain a list of consumers along with their associated destinations
* that have been created through this session
*/
private HashMap _consumers = new HashMap();
/**
* The message listener is the reference to a remote client that wull
* receive the messages
*/
private JmsMessageListener _listener = null;
/**
* This is the acknowledgement mode for the session
*/
private int _ackMode = Session.AUTO_ACKNOWLEDGE;
/**
* Indicates whether the session is transactional
*/
private boolean _transacted = false;
/**
* Holds the current xid that this session is associated with. A session
* can olny be associated with one xid at any one time.
*/
private Xid _xid = null;
/**
* Indicates if the underlying connection of this session has been stopped
*/
private boolean _stopped = true;
/**
* Indicated that the session has been closed
*/
private boolean _closed = false;
/**
* Holds the number of messages published by this session
*/
private long _publishCount;
/**
* Holds the number of messages consumed by this session
*/
private long _consumeCount;
/**
* Caches all sent messages
*/
private SentMessageCache _sentMessageCache;
/**
* The logger
*/
private static final Log _log = LogFactory.getLog(JmsServerSession.class);
/**
* Create an instance of the session using the specified client identifier.
* also pass a back pointer to the connection that created this session.
* By default, the session is stopped. The start() method must be invoked
* before messages will be dispatched to consumers.
*
* @param connection connection that created this session
* @param id client identity
* @param int acknowledgement mode for the session
* @param transacted true if the session is transactional
*/
JmsServerSession(JmsServerConnection connection, String id, int ackmode,
boolean transacted) {
_connection = connection;
_clientId = id;
_ackMode = ackmode;
_transacted = transacted;
_stopped = true;
_sentMessageCache = new SentMessageCache(this);
}
/**
* Set the session id for this object. The Connection is responsible for
* setting the session id once it has been created
*
* @param id session id
*/
void setSessionId(String id) {
_sessionId = id;
}
/**
* Return a reference to the client id
*
* @return String client id
*/
public String getClientId() {
return _clientId;
}
/**
* Return a reference to the session id
*
* @return String session id
*/
public String getSessionId() {
return _sessionId;
}
/**
* Start the message delivery for the session. If session delivery has
* already started then treat the operation as an no-op
*/
public void start() {
if (_log.isDebugEnabled()) {
_log.debug("start() [sessionId=" + _sessionId + "]");
}
if (_stopped) {
pause(false);
_stopped = false;
}
}
/**
* Stop message delivery for the session
*/
public void stop() {
if (_log.isDebugEnabled()) {
_log.debug("stop() [sessionId=" + _sessionId + "]");
}
if (!_stopped) {
pause(true);
_stopped = true;
}
}
/**
* Close and release any resource allocated to this session.
* This method may be called by multiple threads.
*
* @throws JMSException if the session cannot be closed
*/
public void close() throws JMSException {
boolean closed = false;
synchronized (this) {
closed = _closed;
if (!closed) {
_closed = true;
}
}
if (!closed) {
if (_log.isDebugEnabled()) {
_log.debug("close() [sessionId=" + _sessionId + "]");
}
// reset the listener
setMessageListener(null);
// iterate over the list of consumers and deregister the
// associated endpoints and then remove all the entries
Iterator consumers = _consumers.values().iterator();
while (consumers.hasNext()) {
ConsumerEndpoint consumer =
(ConsumerEndpoint) consumers.next();
ConsumerManager.instance().deleteConsumerEndpoint(consumer);
}
// clear the unacked message cache
_sentMessageCache.clear();
// clear the consumers
_consumers.clear();
// de-register the session from the connection
_connection.deleteSession(this);
} else {
if (_log.isDebugEnabled()) {
_log.debug("close() [sessionId=" + _sessionId +
"]: session already closed");
}
}
}
/**
* Acknowledge that the message with the following id has been processed
*
* @param clientId the clientId that sent the message to the client
* @param id the message to ack
* @throws JMSException if message cannot be acknowledged
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -