org.mortbay.util
Class ThreadedServer

java.lang.Object
  extended by org.mortbay.util.ThreadPool
      extended by org.mortbay.util.ThreadedServer
All Implemented Interfaces:
java.io.Serializable, LifeCycle
Direct Known 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
See Also:
Serialized Form

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(java.net.InetAddress address, int port)
          Construct for specific address and port.
ThreadedServer(InetAddrPort address)
          Construct for specific address and port.
ThreadedServer(int port)
          Construct for specific port.
ThreadedServer(java.lang.String host, int port)
          Construct for specific address and port.
 
Method Summary
protected  java.net.Socket acceptSocket(int timeout)
          Accept socket connection.
protected  java.net.Socket acceptSocket(java.net.ServerSocket ignored, int timeout)
          Deprecated. use acceptSocket(int timeout)
 int getAcceptorThreads()
          Get the nmber of threads used to accept connections
 int getAcceptQueueSize()
           
 java.lang.String getHost()
           
 java.net.InetAddress getInetAddress()
           
 InetAddrPort getInetAddrPort()
           
 int getLingerTimeSecs()
           
 int getMaxReadTimeMs()
           
 int getPort()
           
 java.net.ServerSocket getServerSocket()
           
 boolean getTcpNoDelay()
           
 void handle(java.lang.Object job)
          Handle Job.
protected  void handleConnection(java.io.InputStream in, java.io.OutputStream out)
          Handle new connection.
protected  void handleConnection(java.net.Socket connection)
          Handle new connection.
protected  java.net.ServerSocket newServerSocket(InetAddrPort address, int acceptQueueSize)
          New server socket.
 void open()
          Open the server socket.
 void setAcceptorThreads(int n)
          Set the number of threads used to accept connections.
 void setAcceptQueueSize(int acceptQueueSize)
          The size of the queue for unaccepted connections.
 void setHost(java.lang.String host)
           
 void setInetAddress(java.net.InetAddress addr)
           
 void setInetAddrPort(InetAddrPort address)
          Set the server InetAddress and port.
 void setLingerTimeSecs(int ls)
           
 void setMaxReadTimeMs(int ms)
          Deprecated. maxIdleTime is used instead.
 void setPort(int port)
           
 void setTcpNoDelay(boolean tcpNoDelay)
           
 void start()
          Start the LifeCycle.
 void stop()
          Stop the ThreadPool.
protected  void stopJob(java.lang.Thread thread, java.lang.Object job)
          Kill a job.
 java.lang.String toString()
           
 
Methods inherited from class org.mortbay.util.ThreadPool
getIdleThreads, getMaxIdleTimeMs, getMaxThreads, getMinThreads, getName, getPoolName, getThreads, getThreadsPriority, isDaemon, isStarted, join, run, setDaemon, setMaxIdleTimeMs, setMaxStopTimeMs, setMaxThreads, setMinThreads, setName, setPoolName, setThreadsPriority, shrink
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ThreadedServer

public ThreadedServer()

ThreadedServer

public ThreadedServer(int port)
Construct for specific port.


ThreadedServer

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


ThreadedServer

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

Throws:
java.net.UnknownHostException

ThreadedServer

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

Method Detail

getServerSocket

public java.net.ServerSocket getServerSocket()
Returns:
The ServerSocket

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.

getInetAddrPort

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

setHost

public void setHost(java.lang.String host)
             throws java.net.UnknownHostException
Parameters:
host -
Throws:
java.net.UnknownHostException

getHost

public java.lang.String getHost()
Returns:
Host name

setInetAddress

public void setInetAddress(java.net.InetAddress addr)
Parameters:
addr -

getInetAddress

public java.net.InetAddress getInetAddress()
Returns:
IP Address

setPort

public void setPort(int port)
Parameters:
port -

getPort

public int getPort()
Returns:
port number

setMaxReadTimeMs

public void setMaxReadTimeMs(int ms)
Deprecated. maxIdleTime is used instead.

Set Max Read Time.


getMaxReadTimeMs

public int getMaxReadTimeMs()
Returns:
milliseconds

setLingerTimeSecs

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

getLingerTimeSecs

public int getLingerTimeSecs()
Returns:
seconds.

setTcpNoDelay

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

getTcpNoDelay

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

getAcceptQueueSize

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

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.


getAcceptorThreads

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


handleConnection

protected void handleConnection(java.io.InputStream in,
                                java.io.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(java.net.Socket connection)
                         throws java.io.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).

Throws:
java.io.IOException

handle

public void handle(java.lang.Object job)
Handle Job. Implementation of ThreadPool.handle(), calls handleConnection.

Overrides:
handle in class ThreadPool
Parameters:
job - A Connection.

newServerSocket

protected java.net.ServerSocket newServerSocket(InetAddrPort address,
                                                int acceptQueueSize)
                                         throws java.io.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
Throws:
java.io.IOException

acceptSocket

protected java.net.Socket acceptSocket(java.net.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 java.net.Socket acceptSocket(int timeout)
Accept socket connection. May be overriden by derived class to create specialist serversockets (eg SSL).

Parameters:
serverSocket -
timeout - The time to wait for a connection. Normally passed the ThreadPool maxIdleTime.
Returns:
Accepted Socket

open

public void open()
          throws java.io.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.

Throws:
java.io.IOException - if an error occurs

start

public void start()
           throws java.lang.Exception
Description copied from interface: LifeCycle
Start the LifeCycle.

Specified by:
start in interface LifeCycle
Overrides:
start in class ThreadPool
Throws:
java.lang.Exception - An arbitrary exception may be thrown.

stop

public void stop()
          throws java.lang.InterruptedException
Description copied from class: ThreadPool
Stop the ThreadPool. New jobs are no longer accepted,idle threads are interrupted and stopJob is called on active threads. The method then waits min(getMaxStopTimeMs(),getMaxIdleTimeMs()), for all jobs to stop, at which time killJob is called.

Specified by:
stop in interface LifeCycle
Overrides:
stop in class ThreadPool
Throws:
java.lang.InterruptedException - Stopping a lifecycle is rarely atomic and may be interrupted by another thread. If this happens InterruptedException is throw and the component will be in an indeterminant state and should probably be discarded.

stopJob

protected void stopJob(java.lang.Thread thread,
                       java.lang.Object job)
Kill a job. This method closes IDLE and socket associated with a job

Overrides:
stopJob in class ThreadPool
Parameters:
thread -
job -

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


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