org.mortbay.util

Class ThreadedServer

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

Constructor Summary
ThreadedServer()
ThreadedServer(int port)
Construct for specific port.
ThreadedServer(InetAddress address, int port)
Construct for specific address and port.
ThreadedServer(String host, int port)
Construct for specific address and port.
ThreadedServer(InetAddrPort address)
Construct for specific address and port.
Method Summary
protected SocketacceptSocket(ServerSocket ignored, int timeout)
Accept socket connection.
protected SocketacceptSocket(int timeout)
Accept socket connection.
intgetAcceptorThreads()
Get the nmber of threads used to accept connections
intgetAcceptQueueSize()
StringgetHost()
InetAddressgetInetAddress()
InetAddrPortgetInetAddrPort()
intgetLingerTimeSecs()
intgetMaxReadTimeMs()
intgetPort()
ServerSocketgetServerSocket()
booleangetTcpNoDelay()
voidhandle(Object job)
Handle Job.
protected voidhandleConnection(InputStream in, OutputStream out)
Handle new connection.
protected voidhandleConnection(Socket connection)
Handle new connection.
protected ServerSocketnewServerSocket(InetAddrPort address, int acceptQueueSize)
New server socket.
voidopen()
Open the server socket.
voidsetAcceptorThreads(int n)
Set the number of threads used to accept connections.
voidsetAcceptQueueSize(int acceptQueueSize)
The size of the queue for unaccepted connections.
voidsetHost(String host)
voidsetInetAddress(InetAddress addr)
voidsetInetAddrPort(InetAddrPort address)
Set the server InetAddress and port.
voidsetLingerTimeSecs(int ls)
voidsetMaxReadTimeMs(int ms)
Set Max Read Time.
voidsetPort(int port)
voidsetTcpNoDelay(boolean tcpNoDelay)
voidstart()
voidstop()
protected voidstopJob(Thread thread, Object job)
Kill a job.
StringtoString()

Constructor Detail

ThreadedServer

public ThreadedServer()

ThreadedServer

public ThreadedServer(int port)
Construct for specific port.

ThreadedServer

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

ThreadedServer

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

ThreadedServer

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

Method Detail

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: serverSocket timeout The time to wait for a connection. Normally passed the ThreadPool maxIdleTime.

Returns: Accepted Socket

getAcceptorThreads

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

getAcceptQueueSize

public int getAcceptQueueSize()

Returns: Returns the acceptQueueSize or -1 if not set.

getHost

public String getHost()

Returns: Host name

getInetAddress

public InetAddress getInetAddress()

Returns: IP Address

getInetAddrPort

public InetAddrPort getInetAddrPort()

Returns: IP Address and port in a new Instance of InetAddrPort.

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.

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)
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)
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

open

public void open()
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: IOException if an error occurs

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.

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.

setHost

public void setHost(String host)

Parameters: host

setInetAddress

public void setInetAddress(InetAddress addr)

Parameters: addr

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.

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()

stop

public void stop()

stopJob

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

Parameters: thread job

toString

public String toString()
Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.