📄 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-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: JmsConnection.java,v 1.4 2005/05/24 13:35:18 tanderson Exp $
*/
package org.exolab.jms.client;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jms.Connection;
import javax.jms.ConnectionConsumer;
import javax.jms.ConnectionMetaData;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.IllegalStateException;
import javax.jms.InvalidDestinationException;
import javax.jms.InvalidSelectorException;
import javax.jms.JMSException;
import javax.jms.ServerSessionPool;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.InvalidClientIDException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.jms.server.ServerConnection;
/**
* Client side implementation of the <code>javax.jms.Connection</code>
* interface.
*
* @author <a href="mailto:jima@comware.com.au">Jim Alateras</a>
* @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
* @version $Revision: 1.4 $ $Date: 2005/05/24 13:35:18 $
*/
class JmsConnection implements Connection {
/**
* The connection factory that constructed this.
*/
private JmsConnectionFactory _factory;
/**
* The proxy for the remote connection implementation.
*/
private ServerConnection _connection;
/**
* The connection identifier, assigned by the server.
*/
private final long _connectionId;
/**
* This flag indicates whether or not the connection is closed.
*/
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;
/**
* The identity associated with the client, set via the {@link
* JmsConnectionFactory} or {@link #setClientID}
*/
private String _clientId;
/**
* Gates the setting of the clientId more than once.
*/
private boolean _clientIdSet = false;
/**
* The exception listener for this connection.
*/
private ExceptionListener _exceptionListener;
/**
* The active sessions managed by this connection.
*/
private List _sessions = new ArrayList();
/**
* 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 clientID the pre-configured client identifier. May be
* <code>null</code>
* @param username the client username
* @param password the client password
* @throws JMSException if a connection cannot be established
*/
protected JmsConnection(JmsConnectionFactory factory, String clientID,
String username, String password)
throws JMSException {
if (factory == null) {
throw new IllegalArgumentException("Argument 'factory' is null");
}
_factory = factory;
_clientId = clientID;
_stopped = true;
// use the factory object to retrieve the proxy that
// will be used to get a JmsConnectionStubIfc instance
// and cache its identity locally
_connection = factory.getProxy().createConnection(_clientId, username,
password);
_connectionId = _connection.getConnectionId();
}
/**
* Returns the connection identifier.
*
* @return the connection identifier
*/
public long getConnectionId() {
return _connectionId;
}
/**
* Gets the client identifier for this connection.
*
* @return the unique client identifier
* @throws JMSException if the JMS provider fails to return the client ID
* for this connection due to some internal error.
*/
public String getClientID() throws JMSException {
ensureOpen();
setModified();
return _clientId;
}
/**
* Sets the client identifier for this connection.
*
* @param clientID the unique client identifier
* @throws JMSException if the JMS provider fails to set the
* client ID for this connection due to
* some internal error.
* @throws InvalidClientIDException if the JMS client specifies an invalid
* or duplicate client ID.
* @throws IllegalStateException if the JMS client attempts to set a
* connection's client ID at the wrong time
* or when it has been administratively
* configured.
*/
public void setClientID(String clientID) 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");
}
_connection.setClientID(clientID);
_clientId = clientID;
_clientIdSet = true; // prevent client id from being set more than once.
}
/**
* Returns the metadata for this connection.
*
* @return the connection metadata
* @throws JMSException if the JMS provider fails to get the connection
* metadata for this connection.
*/
public ConnectionMetaData getMetaData() throws JMSException {
ensureOpen();
setModified();
return _metaData;
}
/**
* Returns the <code>ExceptionListener</code> for this connection.
*
* @return the <code>ExceptionListener</code> for this connection, or
* <code>null</code> if none is associated with this connection.
* @throws JMSException if the JMS provider fails to get the
* <code>ExceptionListener</code> for this connection.
*/
public ExceptionListener getExceptionListener() throws JMSException {
ensureOpen();
setModified();
return _exceptionListener;
}
/**
* Sets an exception listener for this connection.
*
* @param listener the exception listener
* @throws JMSException if the JMS provider fails to set the exception
* listener for this connection.
*/
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 {
close();
} catch (JMSException exception) {
_log.error(exception.getMessage(), exception);
}
}
// finally notify registered exception listener
if (_exceptionListener != null) {
_exceptionListener.onException(message);
}
}
/**
* Starts (or restarts) a connection's delivery of incoming messages. A call
* to <code>start</code> on a connection that has already been started is
* ignored.
*
* @throws JMSException if the JMS provider fails to start message delivery
* due to some internal error.
*/
public synchronized void start() throws JMSException {
ensureOpen();
setModified();
try {
if (_stopped) {
// start the associated sessions
Iterator iterator = _sessions.iterator();
while (iterator.hasNext()) {
JmsSession session = (JmsSession) iterator.next();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -