public class TcpServer extends Object
A TcpServer can be bound to multiple addresses;
each address has a corresponding channel handler.
See TcpServer.Conf.handlers
.
The server can be in 4 states: init, accepting, acceptingPaused, acceptingStopped.
After start()
, the server is in accepting
state.
New incoming connections are accepted.
If pauseAccepting()
is called, the server is in acceptingPaused
state.
New connections are not accepted.
This is useful to protect the server when some resources are about to be exhausted.
The server socket ports are still owned by this server.
The server can be resumed to accepting
state by resumeAccepting()
.
After stopAccepting()
, the server is in acceptingStopped
state.
New connections are not accepted.
The server socket ports are freed and can be grabbed by another server.
Existing connections are still functional until app closes them.
Check the number of live connections by getConnectionCount()
.
The stopAll()
call will stop accepting new connections and kill all existing connections.
The server is then back to the init
state.
You can use stop(graceTimeout)
to gracefully stop the server.
Modifier and Type | Class and Description |
---|---|
static class |
TcpServer.Conf
Configuration for TcpServer.
|
Constructor and Description |
---|
TcpServer(TcpServer.Conf conf)
Create a TcpServer.
|
Instance Methods | |
---|---|
void |
start()
Start the server.
|
Set<ServerSocketChannel> |
getServerSockets()
Get server sockets.
|
int |
getConnectionCount()
Get the number of connections.
|
void |
pauseAccepting()
Pause accepting new connections.
|
void |
resumeAccepting()
Resume accepting new connections.
|
void |
stopAccepting()
Stop accepting new connections.
|
void |
stopAll()
Stop accepting new connections and kill all connections.
|
void |
stop(Duration graceTimeout)
Gracefully stop the server.
|
public TcpServer(TcpServer.Conf conf)
init
state.public void start() throws Exception
Exception
public Set<ServerSocketChannel> getServerSockets()
Returns an empty set if the server is not started, or has been stopped.
public int getConnectionCount()
public void pauseAccepting()
public void resumeAccepting()
public void stopAccepting()
public void stopAll()
public void stop(Duration graceTimeout)
stopAccepting()
to stop accepting new connections.
getConnectionCount()
to reach 0, within `graceTimeout`.
stopAll()
.