SimpleConnectionManagerpublic 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.ConfigurableAn 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_TIMEOUTThe 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_CONNECTIONSThe default value for the maximum number of allowed client
connections. | private final HashMap | connectionMapThe map of connection name / server connections managed by this connection
manager. | protected int | timeoutThe idle timeout for the individual sockets spawed from the server socket. | protected int | maxOpenConnThe maximum number of client connections allowed per server connection. | private org.apache.avalon.cornerstone.services.threads.ThreadManager | threadManagerThe ThreadManager component that is used to provide a default thread pool. | private volatile boolean | disposedWhether the SimpleConnectionManager has been disposed. |
Methods Summary |
---|
public void | configure(org.apache.avalon.framework.configuration.Configuration 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 void | connect(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.
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 void | connect(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.
connect(name, socket, handlerFactory, threadPool, maxOpenConn);
| public void | connect(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.
connect(name, socket, handlerFactory, threadManager.getDefaultThreadPool());
| public void | connect(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.
connect(name, socket, handlerFactory, threadManager.getDefaultThreadPool(), maxOpenConnections);
| public void | disconnect(java.lang.String name)This shuts down all handlers and socket, waiting for each to gracefully shutdown.
disconnect(name, false);
| public void | disconnect(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 );
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 void | dispose()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 int | getMaximumNumberOfOpenConnections()Returns the default maximum number of open connections supported by this
SimpleConnectionManager
return maxOpenConn;
| public void | service(org.apache.avalon.framework.service.ServiceManager componentManager)
threadManager = (ThreadManager)componentManager.lookup(ThreadManager.ROLE);
|
|