org.mortbay.util

Class ThreadedServer

Implemented Interfaces:
LifeCycle, Serializable
Known Direct Subclasses:
AJP13Listener, SocketListener

public abstract class ThreadedServer
extends ThreadPool

Threaded socket server. This class listens at a socket and gives the connections received to a pool of Threads

The class is abstract and derived classes must provide the handling for the connections.

The properties THREADED_SERVER_MIN_THREADS and THREADED_SERVER_MAX_THREADS can be set to control the number of threads created.

Version:
$Id: ThreadedServer.java,v 1.41 2005/12/10 00:38:20 gregwilkins Exp $
Author:
Greg Wilkins

Nested Class Summary

Nested classes/interfaces inherited from class org.mortbay.util.ThreadPool

ThreadPool.PoolThread

Field Summary

Fields inherited from class org.mortbay.util.ThreadPool

__DAEMON, __PRIORITY

Constructor Summary

ThreadedServer()
ThreadedServer(InetAddress address, int port)
Construct for specific address and port.
ThreadedServer(String host, int port)
Construct for specific address and port.
ThreadedServer(int port)
Construct for specific port.
ThreadedServer(InetAddrPort address)
Construct for specific address and port.

Method Summary

protected Socket
acceptSocket(ServerSocket ignored, int timeout)
Deprecated. use acceptSocket(int timeout)
protected Socket
acceptSocket(int timeout)
Accept socket connection.
int
getAcceptQueueSize()
int
getAcceptorThreads()
Get the nmber of threads used to accept connections
String
getHost()
InetAddrPort
getInetAddrPort()
InetAddress
getInetAddress()
int
getLingerTimeSecs()
int
getMaxReadTimeMs()
int
getPort()
ServerSocket
getServerSocket()
boolean
getTcpNoDelay()
void
handle(Object job)
Handle Job.
protected void
handleConnection(InputStream in, OutputStream out)
Handle new connection.
protected void
handleConnection(Socket connection)
Handle new connection.
protected ServerSocket
newServerSocket(InetAddrPort address, int acceptQueueSize)
New server socket.
void
open()
Open the server socket.
void
setAcceptQueueSize(int acceptQueueSize)
The size of the queue for unaccepted connections.
void
setAcceptorThreads(int n)
Set the number of threads used to accept connections.
void
setHost(String host)
void
setInetAddrPort(InetAddrPort address)
Set the server InetAddress and port.
void
setInetAddress(InetAddress addr)
void
setLingerTimeSecs(int ls)
void
setMaxReadTimeMs(int ms)
Deprecated. maxIdleTime is used instead.
void
setPort(int port)
void
setTcpNoDelay(boolean tcpNoDelay)
void
start()
void
stop()
protected void
stopJob(Thread thread, Object job)
Kill a job.
String
toString()

Methods inherited from class org.mortbay.util.ThreadPool

getIdleThreads, getMaxIdleTimeMs, getMaxThreads, getMinThreads, getName, getPoolName, getThreads, getThreadsPriority, handle, isDaemon, isStarted, join, run, setDaemon, setMaxIdleTimeMs, setMaxStopTimeMs, setMaxThreads, setMinThreads, setName, setPoolName, setThreadsPriority, shrink, start, stop, stopJob

Constructor Details

ThreadedServer

public ThreadedServer()

ThreadedServer

public ThreadedServer(InetAddress address,
                      int port)
Construct for specific address and port.

ThreadedServer

public ThreadedServer(String host,
                      int port)
            throws UnknownHostException
Construct for specific address and port.

ThreadedServer

public ThreadedServer(int port)
Construct for specific port.

ThreadedServer

public ThreadedServer(InetAddrPort address)
Construct for specific address and port.

Method Details

acceptSocket

protected Socket acceptSocket(ServerSocket ignored,
                              int timeout)

Deprecated. use acceptSocket(int timeout)

Accept socket connection. May be overriden by derived class to create specialist serversockets (eg SSL).
Parameters:
ignored -
timeout - The time to wait for a connection. Normally passed the ThreadPool maxIdleTime.
Returns:
Accepted Socket

acceptSocket

protected Socket acceptSocket(int timeout)
Accept socket connection. May be overriden by derived class to create specialist serversockets (eg SSL).
Parameters:
timeout - The time to wait for a connection. Normally passed the ThreadPool maxIdleTime.
Returns:
Accepted Socket

getAcceptQueueSize

public int getAcceptQueueSize()
Returns:
Returns the acceptQueueSize or -1 if not set.

getAcceptorThreads

public int getAcceptorThreads()
Get the nmber of threads used to accept connections

getHost

public String getHost()
Returns:
Host name

getInetAddrPort

public InetAddrPort getInetAddrPort()
Returns:
IP Address and port in a new Instance of InetAddrPort.

getInetAddress

public InetAddress getInetAddress()
Returns:
IP Address

getLingerTimeSecs

public int getLingerTimeSecs()
Returns:
seconds.

getMaxReadTimeMs

public int getMaxReadTimeMs()
Returns:
milliseconds

getPort

public int getPort()
Returns:
port number

getServerSocket

public ServerSocket getServerSocket()
Returns:
The ServerSocket

getTcpNoDelay

public boolean getTcpNoDelay()
Returns:
true if setTcpNoDelay(true) is called on accepted sockets.

handle

public void handle(Object job)
Handle Job. Implementation of ThreadPool.handle(), calls handleConnection.
Overrides:
handle in interface ThreadPool
Parameters:
job - A Connection.

handleConnection

protected void handleConnection(InputStream in,
                                OutputStream out)
Handle new connection. This method should be overridden by the derived class to implement the required handling. It is called by a thread created for it and does not need to return until it has finished it's task

handleConnection

protected void handleConnection(Socket connection)
            throws IOException
Handle new connection. If access is required to the actual socket, override this method instead of handleConnection(InputStream in,OutputStream out). The default implementation of this just calls handleConnection(InputStream in,OutputStream out).

newServerSocket

protected ServerSocket newServerSocket(InetAddrPort address,
                                       int acceptQueueSize)
            throws IOException
New server socket. Creates a new servers socket. May be overriden by derived class to create specialist serversockets (eg SSL).
Parameters:
address - Address and port
acceptQueueSize - Accept queue size
Returns:
The new ServerSocket

open

public void open()
            throws IOException
Open the server socket. This method can be called to open the server socket in advance of starting the listener. This can be used to test if the port is available.

setAcceptQueueSize

public void setAcceptQueueSize(int acceptQueueSize)
The size of the queue for unaccepted connections. If not set, will default to greater of maxThreads or 50.
Parameters:
acceptQueueSize - The acceptQueueSize to set.

setAcceptorThreads

public void setAcceptorThreads(int n)
Set the number of threads used to accept connections. This should normally be 1, except when multiple CPUs are available and low latency is a high priority.

setHost

public void setHost(String host)
            throws UnknownHostException
Parameters:
host -

setInetAddrPort

public void setInetAddrPort(InetAddrPort address)
Set the server InetAddress and port.
Parameters:
address - The Address to listen on, or 0.0.0.0:port for all interfaces.

setInetAddress

public void setInetAddress(InetAddress addr)
Parameters:
addr -

setLingerTimeSecs

public void setLingerTimeSecs(int ls)
Parameters:
ls - seconds to linger or -1 to disable linger.

setMaxReadTimeMs

public void setMaxReadTimeMs(int ms)

Deprecated. maxIdleTime is used instead.

Set Max Read Time.

setPort

public void setPort(int port)
Parameters:
port -

setTcpNoDelay

public void setTcpNoDelay(boolean tcpNoDelay)
Parameters:
tcpNoDelay - if true then setTcpNoDelay(true) is called on accepted sockets.

start

public void start()
            throws Exception
Specified by:
start in interface LifeCycle
Overrides:
start in interface ThreadPool

stop

public void stop()
            throws InterruptedException
Specified by:
stop in interface LifeCycle
Overrides:
stop in interface ThreadPool

stopJob

protected void stopJob(Thread thread,
                       Object job)
Kill a job. This method closes IDLE and socket associated with a job
Overrides:
stopJob in interface ThreadPool
Parameters:
thread -
job -

toString

public String toString()

Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.