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.
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.
acceptSocket
protected Socket acceptSocket(ServerSocket ignored,
int timeout)
use acceptSocket(int timeout)
Accept socket connection. May be overriden by derived class to create specialist
serversockets (eg SSL).
ignored
- timeout
- The time to wait for a connection. Normally passed the ThreadPool maxIdleTime.
acceptSocket
protected Socket acceptSocket(int timeout)
Accept socket connection. May be overriden by derived class to create specialist
serversockets (eg SSL).
timeout
- The time to wait for a connection. Normally passed the ThreadPool maxIdleTime.
getAcceptQueueSize
public int getAcceptQueueSize()
- 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()
getInetAddrPort
public InetAddrPort getInetAddrPort()
- IP Address and port in a new Instance of InetAddrPort.
getInetAddress
public InetAddress getInetAddress()
getLingerTimeSecs
public int getLingerTimeSecs()
getMaxReadTimeMs
public int getMaxReadTimeMs()
getPort
public int getPort()
getServerSocket
public ServerSocket getServerSocket()
getTcpNoDelay
public boolean getTcpNoDelay()
- true if setTcpNoDelay(true) is called on accepted sockets.
handle
public void handle(Object job)
Handle Job. Implementation of ThreadPool.handle(), calls handleConnection.
- handle in interface ThreadPool
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).
address
- Address and portacceptQueueSize
- Accept queue size
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.
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
setInetAddrPort
public void setInetAddrPort(InetAddrPort address)
Set the server InetAddress and port.
address
- The Address to listen on, or 0.0.0.0:port for all interfaces.
setInetAddress
public void setInetAddress(InetAddress addr)
setLingerTimeSecs
public void setLingerTimeSecs(int ls)
ls
- seconds to linger or -1 to disable linger.
setMaxReadTimeMs
public void setMaxReadTimeMs(int ms)
maxIdleTime is used instead.
Set Max Read Time.
setPort
public void setPort(int port)
setTcpNoDelay
public void setTcpNoDelay(boolean tcpNoDelay)
tcpNoDelay
- if true then setTcpNoDelay(true) is called on accepted sockets.
stopJob
protected void stopJob(Thread thread,
Object job)
Kill a job. This method closes IDLE and socket associated with a job
- stopJob in interface ThreadPool
toString
public String toString()