📄 jmsserverconnectionmanager.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: JmsServerConnectionManager.java,v 1.10 2003/08/17 01:32:26 tanderson Exp $
*
* Date Author Changes
* 04/07/2000 jima Created
*/
package org.exolab.jms.server;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jms.JMSException;
import javax.jms.JMSSecurityException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.core.foundation.HandleIfc;
import org.exolab.jms.authentication.AuthenticationMgr;
import org.exolab.jms.events.BasicEventManager;
import org.exolab.jms.events.Event;
import org.exolab.jms.events.EventHandler;
import org.exolab.jms.events.IllegalEventDefinedException;
/**
* The connection manager is responsible for managing all connections to the
* JmsServer. The connection manager is a singleton (at this point anyway)
* that is accessible through the instance class method. It is also responsible
* for holding a list of connections.
* <p>
* A client uses a client identifier to create a connection and a connection
* type to identify the connection type- queue or topic.
* <p>
* TODO:
* Look at leasing connections and timing them out.
*
* @version $Revision: 1.10 $ $Date: 2003/08/17 01:32:26 $
* @author <a href="mailto:jima@exoffice.com">Jim Alateras</a>
* @see JmsServerConnection
*/
public class JmsServerConnectionManager
implements EventHandler {
/**
* The interval that garbage collection on the connection manager is
* executed to clean up abnormally terminated connections. This defaults
* to every minute.
*/
private long _gcInterval = 60 * 1000;
/**
* holds a list of active connections
*/
private Map _connections = null;
/**
* Holds the singleton instance of the class
*/
private static JmsServerConnectionManager _instance = null;
/**
* Used to stop multiple threads trying to init the singleton
*/
private static final Object _initializer = new Object();
/**
* This is the event that is fired to initiate garbage collection
* in the database
*/
private static final int CONNECTION_GC_EVENT = 1;
/**
* The logger
*/
private static final Log _log =
LogFactory.getLog(JmsServerConnectionManager.class);
/**
* The private constructor initialises the manager.
*/
private JmsServerConnectionManager() {
_connections = Collections.synchronizedMap(new HashMap());
// check to see whether the gcInterval has been overriden
if (System.getProperty("org.exolab.jms.connection.gcInterval") != null) {
try {
_gcInterval = Long.parseLong(System.getProperty(
"org.exolab.jms.connection.gcInterval"));
} catch (Exception ignore) {
// if the gcinterval is incorrectly specified then use
// the default
}
}
if (_gcInterval > 0) {
registerEvent();
}
}
/**
* The static method returns the singleton instance of the
* JmsConnectionManager. If one does not exist it is created prior to
* returning.
*
* @return JmsConnectionManager
*/
public static JmsServerConnectionManager instance() {
if (_instance == null) {
synchronized (_initializer) {
if (_instance == null) {
_instance = new JmsServerConnectionManager();
}
}
}
return _instance;
}
/**
* Create a connection with the specified client id. If the client already
* has an open connection then simply return a reference to this connection
* If a connection already exists for the specified client identity then
* simply return the exisiting connection
*
* @param id the client identity
* @param username the client's username
* @param password the client's password
* @return a new connection
* @throws JMSSecurityException if the client cannot be authenticated
* @throws JMSException if the connection cannot be created
*/
public JmsServerConnection createConnection(String id, String username,
String password)
throws JMSSecurityException, JMSException {
if (!AuthenticationMgr.instance().validateUser(username, password)) {
throw new JMSSecurityException("Failed to authenticate user " +
username);
}
JmsServerConnection result = (JmsServerConnection) _connections.get(id);
if (result == null) {
result = new JmsServerConnection(id);
// the connection is created in the stopped mode and is the
// added the list of connections
_connections.put(id, result);
}
return result;
}
/**
* Return an Enumeration of all the connections currently registered with
* the connection manager
*
* @return Iterator
*/
public Iterator getConnections() {
return _connections.values().iterator();
}
/**
* Return the connection associated with a particular client identity. If
* such a connection does not exist then return null
*
* @param id identity of the client
* @return JmsConnection associated connection or null
*/
public JmsServerConnection getConnection(String id) {
return (JmsServerConnection) _connections.get(id);
}
/**
* Close the connection associated with a particular client identity. If
* a connection for the client does not exist then do nothing.
*
* @param id identity of the client
*/
public void closeConnection(String id) {
JmsServerConnection connection =
(JmsServerConnection) _connections.remove(id);
if (connection != null) {
connection.close();
}
}
// implementation of EventHandler.handleEvent
public void handleEvent(int event, Object callback, long time) {
try {
Object[] ids = _connections.keySet().toArray();
for (int index = 0; index < ids.length; index++) {
JmsServerConnection connection =
(JmsServerConnection) _connections.get(ids[index]);
if ((connection != null) &&
(!connection.isClientEndpointActive())) {
_log.info("Cleaning up connection " + ids[index]);
closeConnection((String) ids[index]);
}
}
} finally {
registerEvent();
}
}
// implementation of EventHandler.getHandle
public HandleIfc getHandle() {
// not used
return null;
}
/**
* Register an event with the event service. The event will tell the
* connection manager to initiate garbage collection
*/
private void registerEvent() {
try {
BasicEventManager.instance().registerEventRelative(
new Event(CONNECTION_GC_EVENT, this, null),
_gcInterval);
} catch (IllegalEventDefinedException exception) {
_log.error("JmsServerConnectionManager.registerEvent failed",
exception);
}
}
} //-- JmsServerConnectionManager
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -