public abstract class ThreadedServer extends ThreadPool
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.
ThreadPool.PoolThread
__DAEMON, __PRIORITY
Constructor and Description |
---|
ThreadedServer() |
ThreadedServer(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(String host,
int port)
Construct for specific address and port.
|
Modifier and Type | Method and Description |
---|---|
protected Socket |
acceptSocket(int timeout)
Accept socket connection.
|
protected Socket |
acceptSocket(ServerSocket ignored,
int timeout)
Deprecated.
use acceptSocket(int timeout)
|
int |
getAcceptorThreads()
Get the nmber of threads used to accept connections
|
int |
getAcceptQueueSize() |
String |
getHost() |
InetAddress |
getInetAddress() |
InetAddrPort |
getInetAddrPort() |
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 |
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(String host) |
void |
setInetAddress(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(Thread thread,
Object job)
Kill a job.
|
String |
toString() |
getIdleThreads, getMaxIdleTimeMs, getMaxThreads, getMinThreads, getName, getPoolName, getThreads, getThreadsPriority, isDaemon, isStarted, join, run, setDaemon, setMaxIdleTimeMs, setMaxStopTimeMs, setMaxThreads, setMinThreads, setName, setPoolName, setThreadsPriority, shrink
public ThreadedServer()
public ThreadedServer(int port)
public ThreadedServer(InetAddress address, int port)
public ThreadedServer(String host, int port) throws UnknownHostException
UnknownHostException
public ThreadedServer(InetAddrPort address)
public ServerSocket getServerSocket()
public void setInetAddrPort(InetAddrPort address)
address
- The Address to listen on, or 0.0.0.0:port for all interfaces.public InetAddrPort getInetAddrPort()
public void setHost(String host) throws UnknownHostException
host
- UnknownHostException
public String getHost()
public void setInetAddress(InetAddress addr)
addr
- public InetAddress getInetAddress()
public void setPort(int port)
port
- public int getPort()
public void setMaxReadTimeMs(int ms)
public int getMaxReadTimeMs()
public void setLingerTimeSecs(int ls)
ls
- seconds to linger or -1 to disable linger.public int getLingerTimeSecs()
public void setTcpNoDelay(boolean tcpNoDelay)
tcpNoDelay
- if true then setTcpNoDelay(true) is called on accepted sockets.public boolean getTcpNoDelay()
public int getAcceptQueueSize()
public void setAcceptQueueSize(int acceptQueueSize)
acceptQueueSize
- The acceptQueueSize to set.public void setAcceptorThreads(int n)
public int getAcceptorThreads()
protected void handleConnection(InputStream in, OutputStream out)
protected void handleConnection(Socket connection) throws IOException
IOException
public void handle(Object job)
handle
in class ThreadPool
job
- A Connection.protected ServerSocket newServerSocket(InetAddrPort address, int acceptQueueSize) throws IOException
address
- Address and portacceptQueueSize
- Accept queue sizeIOException
protected Socket acceptSocket(ServerSocket ignored, int timeout)
ignored
- timeout
- The time to wait for a connection. Normally passed the ThreadPool maxIdleTime.protected Socket acceptSocket(int timeout)
timeout
- The time to wait for a connection. Normally passed the ThreadPool maxIdleTime.public void open() throws IOException
IOException
- if an error occurspublic void start() throws Exception
LifeCycle
start
in interface LifeCycle
start
in class ThreadPool
Exception
- An arbitrary exception may be thrown.public void stop() throws InterruptedException
ThreadPool
stop
in interface LifeCycle
stop
in class ThreadPool
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.protected void stopJob(Thread thread, Object job)
stopJob
in class ThreadPool
thread
- job
- Copyright © 2013. All rights reserved.