- java.lang.Object
-
- com.pervasive.datarush.commons.util.GenericTCPServer
-
public final class GenericTCPServer extends Object
General purpose TCP server that provides thread pooling, start and stop code.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
GenericTCPServer.ConnectionHandler
Handles incomming connectionsclass
GenericTCPServer.ServerContext
Information passed to a server implementation to provide execution context.
-
Constructor Summary
Constructors Constructor Description GenericTCPServer(GenericTCPServer.ConnectionHandler handler)
Creates a new server
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
awaitShutdown(long timeout, TimeUnit unit)
Waits for pending requests to finish.void
disableThreadPooling()
Disables thread pooling of workers.GenericTCPServer.ConnectionHandler
getConnectionHandler()
Returns the handler to use for processing connectionsString
getHost()
Returns the specific ip address/host to bind toint
getIdleTimeTimeout()
Gets the idle time, in seconds, for the worker thread pool.int
getMaxWorkers()
Returns the max worker threads to allow.int
getMinWorkers()
Returns the min worker threads to allow.String
getName()
Gets the name used for worker threads.int
getPort()
Returns the port to bind to.Thread.State
getStatus()
boolean
isListenerDaemon()
Indicates whether the listener thread is a daemon thread.boolean
isWorkerDaemon()
Indicates whether worker threads are daemon threads.void
setHost(String host)
Sets the specific ip address/host to bind tovoid
setIdleTimeTimeout(int idleTimeTimeout)
Sets the idle time, in seconds, for the worker thread pool.void
setListenerDaemon(boolean daemon)
Sets whether the listener thread is marked as a daemon thread.void
setMaxWorkers(int maxWorkers)
Sets the max worker thread to allow.void
setMinWorkers(int minWorkers)
Sets the min worker thread to allow.void
setName(String name)
Sets the base name used for worker threads.void
setPort(int port)
Sets the port to bind to.void
setWorkerDaemon(boolean daemon)
Sets whether worker threads are marked as daemon threads.InetSocketAddress
start()
Starts the server.void
startShutdown()
Initiates shutdown by closing the listening socket.
-
-
-
Constructor Detail
-
GenericTCPServer
public GenericTCPServer(GenericTCPServer.ConnectionHandler handler)
Creates a new server- Parameters:
handler
- the handler to handle requests
-
-
Method Detail
-
getConnectionHandler
public GenericTCPServer.ConnectionHandler getConnectionHandler()
Returns the handler to use for processing connections- Returns:
- the configured connection handler
-
getMaxWorkers
public int getMaxWorkers()
Returns the max worker threads to allow.- Returns:
- The max worker threads to allow.
-
setMaxWorkers
public void setMaxWorkers(int maxWorkers)
Sets the max worker thread to allow.- Parameters:
maxWorkers
- The max worker threads to allow.
-
getMinWorkers
public int getMinWorkers()
Returns the min worker threads to allow.- Returns:
- The min worker threads to allow.
-
setMinWorkers
public void setMinWorkers(int minWorkers)
Sets the min worker thread to allow.- Parameters:
minWorkers
- The min worker threads to allow.
-
getHost
public String getHost()
Returns the specific ip address/host to bind to- Returns:
- the specific ip address/host to bind to
-
setHost
public void setHost(String host)
Sets the specific ip address/host to bind to- Parameters:
ipaddress
- the specific ip/host address to bind to
-
start
public final InetSocketAddress start()
Starts the server.- Returns:
- the listening address of the server
- Throws:
DRException
- if errors occur during start up
-
startShutdown
public final void startShutdown()
Initiates shutdown by closing the listening socket. Pending requests will be allowed to finish.
-
awaitShutdown
public final void awaitShutdown(long timeout, TimeUnit unit) throws InterruptedException
Waits for pending requests to finish. Upon successful completion of this method, the server is in a logically shutdown state such that it can be restarted.- Parameters:
timeout
- the length of the timeoutunit
- the unit of the timeout- Throws:
InterruptedException
- if interrupted while waiting for shutdown
-
getPort
public int getPort()
Returns the port to bind to. The default value of zero means chose an ephemeral port.- Returns:
- the configured port
-
setPort
public void setPort(int port)
Sets the port to bind to. The default value of zero means chose an ephemeral port.- Parameters:
port
- the port to which to bind
-
isListenerDaemon
public boolean isListenerDaemon()
Indicates whether the listener thread is a daemon thread.- Returns:
true
if the listener is a daemon thread,false
otherwise.
-
setListenerDaemon
public void setListenerDaemon(boolean daemon)
Sets whether the listener thread is marked as a daemon thread.- Parameters:
daemon
- indicates whether the listener is a daemon or not- See Also:
Thread.setDaemon(boolean)
-
isWorkerDaemon
public boolean isWorkerDaemon()
Indicates whether worker threads are daemon threads.- Returns:
true
if workers are daemon threads,false
otherwise.
-
setWorkerDaemon
public void setWorkerDaemon(boolean daemon)
Sets whether worker threads are marked as daemon threads.- Parameters:
daemon
- indicates whether workers are daemons or not- See Also:
Thread.setDaemon(boolean)
-
getStatus
public final Thread.State getStatus()
-
setName
public void setName(String name)
Sets the base name used for worker threads.- Parameters:
name
- the base name to use
-
getName
public String getName()
Gets the name used for worker threads.- Returns:
- the base name of worker threads
-
setIdleTimeTimeout
public void setIdleTimeTimeout(int idleTimeTimeout)
Sets the idle time, in seconds, for the worker thread pool. Worker threads will be removed from the pool after being idle for this amount of time.- Parameters:
idleTimeTimeout
- the number of seconds a thread may be idle
-
getIdleTimeTimeout
public int getIdleTimeTimeout()
Gets the idle time, in seconds, for the worker thread pool.- Returns:
- the number seconds a thread may be idle
-
disableThreadPooling
public void disableThreadPooling()
Disables thread pooling of workers. Every new request starts a new thread which terminates after handling the request.
-
-