FileDocCategorySizeDatePackage
SimpleConnectionManager.javaAPI DocApache James 2.3.111309Fri Jan 12 12:56:34 GMT 2007org.apache.james.util.connection

SimpleConnectionManager

public class SimpleConnectionManager extends org.apache.avalon.framework.logger.AbstractLogEnabled implements org.apache.avalon.framework.activity.Disposable, org.apache.avalon.framework.service.Serviceable, org.apache.james.services.JamesConnectionManager, org.apache.avalon.framework.configuration.Configurable
An implementation of ConnectionManager that supports configurable idle timeouts and a configurable value for the maximum number of client connections to a particular port.

Fields Summary
private static final int
DEFAULT_SOCKET_TIMEOUT
The default value for client socket idle timeouts. The Java default is 0, meaning no timeout. That's dangerous for a connection handler like this one, because it can easily lead to consumption of network resources. So we allow users to configure the system to allow no timeout, but if no timeout is specified in the configuration, we use a timeout of 5 minutes.
private static final int
DEFAULT_MAX_CONNECTIONS
The default value for the maximum number of allowed client connections.
private final HashMap
connectionMap
The map of connection name / server connections managed by this connection manager.
protected int
timeout
The idle timeout for the individual sockets spawed from the server socket.
protected int
maxOpenConn
The maximum number of client connections allowed per server connection.
private org.apache.avalon.cornerstone.services.threads.ThreadManager
threadManager
The ThreadManager component that is used to provide a default thread pool.
private volatile boolean
disposed
Whether the SimpleConnectionManager has been disposed.
Constructors Summary
Methods Summary
public voidconfigure(org.apache.avalon.framework.configuration.Configuration configuration)

see
org.apache.avalon.framework.configuration.Configurable#configure(Configuration)

           
           
        timeout = configuration.getChild("idle-timeout").getValueAsInteger(DEFAULT_SOCKET_TIMEOUT);
        maxOpenConn = configuration.getChild("max-connections").getValueAsInteger(DEFAULT_MAX_CONNECTIONS);
        if (timeout < 0) {
            StringBuffer exceptionBuffer =
                new StringBuffer(128).append("Specified socket timeout value of ").append(timeout).append(
                    " is not a legal value.");
            throw new ConfigurationException(exceptionBuffer.toString());
        }
        if (maxOpenConn < 0) {
            StringBuffer exceptionBuffer =
                new StringBuffer(128).append("Specified maximum number of open connections of ").append(
                    maxOpenConn).append(
                    " is not a legal value.");
            throw new ConfigurationException(exceptionBuffer.toString());
        }
        if (getLogger().isDebugEnabled()) {
            getLogger().debug(
                "Connection timeout is " + (timeout == 0 ? "unlimited" : Long.toString(timeout)));
            getLogger().debug(
                "The maximum number of simultaneously open connections is "
                    + (maxOpenConn == 0 ? "unlimited" : Integer.toString(maxOpenConn)));
        }
    
public voidconnect(java.lang.String name, java.net.ServerSocket socket, org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory handlerFactory, org.apache.excalibur.thread.ThreadPool threadPool, int maxOpenConnections)
Start managing a connection. Management involves accepting connections and farming them out to threads from pool to be handled.

param
name the name of connection
param
socket the ServerSocket from which to
param
handlerFactory the factory from which to acquire handlers
param
threadPool the thread pool to use
param
maxOpenConnections the maximum number of open connections allowed for this server socket.
exception
Exception if an error occurs

        if (disposed) {
            throw new IllegalStateException("Connection manager has already been shutdown.");
        }
        if (null != connectionMap.get(name)) {
            throw new IllegalArgumentException("Connection already exists with name " + name);
        }
        if (maxOpenConnections < 0) {
            throw new IllegalArgumentException("The maximum number of client connections per server socket cannot be less that zero.");
        }
        ServerConnection runner =
            new ServerConnection(socket, handlerFactory, threadPool, timeout, maxOpenConnections);
        setupLogger(runner);
        ContainerUtil.initialize(runner);
        connectionMap.put(name, runner);
        threadPool.execute(runner);
    
public voidconnect(java.lang.String name, java.net.ServerSocket socket, org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory handlerFactory, org.apache.excalibur.thread.ThreadPool threadPool)
Start managing a connection. Management involves accepting connections and farming them out to threads from pool to be handled.

param
name the name of connection
param
socket the ServerSocket from which to
param
handlerFactory the factory from which to acquire handlers
param
threadPool the thread pool to use
exception
Exception if an error occurs

        connect(name, socket, handlerFactory, threadPool, maxOpenConn);
    
public voidconnect(java.lang.String name, java.net.ServerSocket socket, org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory handlerFactory)
Start managing a connection. This is similar to other connect method except that it uses default thread pool.

param
name the name of connection
param
socket the ServerSocket from which to
param
handlerFactory the factory from which to acquire handlers
exception
Exception if an error occurs

        connect(name, socket, handlerFactory, threadManager.getDefaultThreadPool());
    
public voidconnect(java.lang.String name, java.net.ServerSocket socket, org.apache.avalon.cornerstone.services.connection.ConnectionHandlerFactory handlerFactory, int maxOpenConnections)
Start managing a connection. This is similar to other connect method except that it uses default thread pool.

param
name the name of connection
param
socket the ServerSocket from which to
param
handlerFactory the factory from which to acquire handlers
param
maxOpenConnections the maximum number of open connections allowed for this server socket.
exception
Exception if an error occurs

        connect(name, socket, handlerFactory, threadManager.getDefaultThreadPool(), maxOpenConnections);
    
public voiddisconnect(java.lang.String name)
This shuts down all handlers and socket, waiting for each to gracefully shutdown.

param
name the name of connection
exception
Exception if an error occurs

        disconnect(name, false);
    
public voiddisconnect(java.lang.String name, boolean tearDown)
This shuts down a connection. If tearDown is true then it will forcefully the connection and try to return as soon as possible. Otherwise it will behave the same as void disconnect( String name );

param
name the name of connection
param
tearDown if true will forcefully tear down all handlers
exception
Exception if an error occurs

        ServerConnection connection = (ServerConnection)connectionMap.remove(name);
        if (null == connection) {
            throw new IllegalArgumentException("No such connection with name " + name);
        }
        // TODO: deal with tear down parameter
        connection.dispose();
    
public voiddispose()
Disconnects all the underlying ServerConnections

        disposed = true;
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Starting SimpleConnectionManager dispose...");
        }
        final String[] names = (String[])connectionMap.keySet().toArray(new String[0]);
        for (int i = 0; i < names.length; i++) {
            try {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("Disconnecting ServerConnection " + names[i]);
                }
                disconnect(names[i], true);
            } catch (final Exception e) {
                getLogger().warn("Error disconnecting " + names[i], e);
            }
        }
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Finishing SimpleConnectionManager dispose...");
        }
    
public intgetMaximumNumberOfOpenConnections()
Returns the default maximum number of open connections supported by this SimpleConnectionManager

return
the maximum number of connections

        return maxOpenConn;
    
public voidservice(org.apache.avalon.framework.service.ServiceManager componentManager)

see
org.apache.avalon.framework.service.Serviceable#service(ServiceManager)

        threadManager = (ThreadManager)componentManager.lookup(ThreadManager.ROLE);