📄 jmsconnection.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-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: JmsConnection.java,v 1.26 2004/01/11 02:33:44 tanderson Exp $
*
* Date Author Changes
* 3/21/2000 jima Created
*/
package org.exolab.jms.client;
import java.util.Enumeration;
import java.util.Vector;
import javax.jms.Connection;
import javax.jms.ConnectionMetaData;
import javax.jms.ExceptionListener;
import javax.jms.IllegalStateException;
import javax.jms.JMSException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.core.service.ServiceException;
import org.exolab.jms.events.BasicEventManager;
/**
* Client side implementation of the <code>javax.jms.Connection</code>
* interface.
*
* @version $Revision: 1.26 $ $Date: 2004/01/11 02:33:44 $
* @author <a href="mailto:jima@exoffice.com">Jim Alateras</a>
* @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
*/
abstract class JmsConnection implements Connection {
/**
* This flag indicates whether or not the connection is closed. It
* is set to false on creation
*/
private boolean _closed = false;
/**
* This flag indicates whether the connection is in the start or
* stopped state
*/
private boolean _stopped = true;
/**
* This flag indicates whether the connection has been modified.
* If so, subsequent attempts to invoke {@link #setClientID} will
* cause an <code>IllegalStateException</code> being thrown
*/
private boolean _modified = false;
/**
* This is the identity associated with the client. The identity is
* generated and assigned by the connection factory
*/
private String _clientId = null;
/**
* Gates the setting of the clientId more than once
*/
private boolean _clientIdSet = false;
/**
* A connection can maintain one and only one exception listener, The
* listener will be informed of unusual and exception events
*/
private ExceptionListener _exceptionListener = null;
/**
* Manage the list of activated and registered sessions. Everytime a
* session is created through this connection it is registered.
*/
private Vector _sessions = new Vector();
/**
* This is a back pointer to the connection factory which created this
* connection. It is assigned during object construction
*/
private JmsConnectionFactory _factory = null;
/**
* This is a stub to a remote object on the JMSSever. This abstraction
* allows us to use different underlying ORBS.
*/
private JmsConnectionStubIfc _stub = null;
/**
* The id is generated by the server and is unique for each connection
* created. This id is then used to generate other ids
*/
private String _connectionId = null;
/**
* The number of connections created
*/
private static volatile int _activeConnections = 0;
/**
* The connection data is immutable at this stage. This enables us to cache
* a single copy in memory
*/
private static final JmsConnectionMetaData _metaData =
new JmsConnectionMetaData();
/**
* The logger
*/
private static final Log _log = LogFactory.getLog(JmsConnection.class);
/**
* Construct a new <code>JmsConnection</code>
* <p>
* This attempts to establish a connection to the JMS server
*
* @param factory the connection factory responsible for creating this
* @param id the client identity
* @param username the client username
* @param password the client password
* @throws JMSException if a connection cannot be established
*/
protected JmsConnection(JmsConnectionFactory factory, String id,
String username, String password)
throws JMSException {
if (factory == null) {
throw new IllegalArgumentException("Argument 'factory' is null");
}
if (id == null) {
throw new IllegalArgumentException("Argument 'id' is null");
}
if (id.trim().length() == 0) {
throw new IllegalArgumentException("Argument 'id' is invalid: '"
+ id + "'");
}
_factory = factory;
_clientId = id;
_stopped = true;
// use the factory object to retrieve the proxy that
// will be used to get a JmsConnectionStubIfc instance
// and cache its identity locally
_stub = factory.getProxy().createConnection(_clientId, username,
password);
_connectionId = _stub.getConnectionId();
// start the event manager
startEventManager();
}
// implementation of Connection.getClientID
public String getClientID() throws JMSException {
ensureOpen();
setModified();
return _clientId;
}
// implementation of Connection.setClientID
public void setClientID(String id) throws JMSException {
ensureOpen();
// check if the client id has already been set
if (_clientIdSet) {
throw new IllegalStateException(
"The client id has already been set");
}
if (_modified) {
throw new IllegalStateException(
"The client identifier must be set before any other operation "
+ "is performed");
}
// gate the client id from being set more than once.
_clientId = id;
_clientIdSet = true;
}
// implementation of Connection.getMetaData
public ConnectionMetaData getMetaData() throws JMSException {
ensureOpen();
setModified();
return _metaData;
}
// implementation of Connection.getExceptionListener
public ExceptionListener getExceptionListener() throws JMSException {
ensureOpen();
setModified();
return _exceptionListener;
}
// implementation of Connection.setExceptionListener
public void setExceptionListener(ExceptionListener listener)
throws JMSException {
ensureOpen();
setModified();
_exceptionListener = listener;
}
/**
* Notify the exception listener of a JMSException. If the exception
* listener is not set then ignore it
*
* @param message message to deliver
*/
public void notifyExceptionListener(JMSException message) {
// check the error code
if (message.getErrorCode() != null &&
message.getErrorCode().equals(
JmsErrorCodes.CONNECTION_TO_SERVER_DROPPED)) {
// the connection to the server has been dropped so we need to
// release all local resources.
try {
destroy();
} catch (JMSException exception) {
_log.error(exception.getMessage(), exception);
}
}
// finally notify registered exception listener
if (_exceptionListener != null) {
_exceptionListener.onException(message);
}
}
// implementation of Connection.start
public synchronized void start() throws JMSException {
ensureOpen();
setModified();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -