public class HttpServer extends Object
Each server has an HttpHandler
which creates a response for every request.
Configuration can be done on the HttpServerConf
before server start, for example
HttpServer server = new HttpServer(handler); server.conf() .port( 8080 ) .trafficDump( System.out::print ) ; server.start();
The server can be in 4 states: init, accepting, acceptingPaused, acceptingStopped.
After start()
, the server is in accepting
state.
New requests are accepted.
If pauseAccepting()
is called, the server is in acceptingPaused
state.
New requests 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 requests are not accepted.
The server socket ports are freed and can be grabbed by another server.
Existing requests are still being processed.
Their connections will be closed after their responses are written.
Check the number of live connections by getConnectionCount()
.
The stopAll()
call will stop accepting new requests and abort outstanding requests.
You can use stop(graceTimeout)
to gracefully stop the server.
Constructor and Description |
---|
HttpServer(HttpHandler handler)
Create an HttpServer.
|
HttpServer(HttpServerConf conf,
HttpHandler handler)
Create an HttpServer.
|
Instance Methods | |
---|---|
HttpServerConf |
conf()
Get the
HttpServerConf of this server. |
void |
addUpgrader(String protocol,
HttpUpgrader upgrader)
Add an
HttpUpgrader for the protocol. |
void |
start()
Start the server.
|
Set<ServerSocketChannel> |
getServerSockets()
Get server sockets.
|
int |
getConnectionCount()
Get the number of connections.
|
void |
pauseAccepting()
Pausing accepting new requests.
|
void |
resumeAccepting()
Resume accepting new requests.
|
void |
stopAccepting()
Stop accepting new requests.
|
void |
stopAll()
Stop accepting new requests and abort outstanding requests.
|
void |
stop(Duration graceTimeout)
Gracefully stop the server.
|
public HttpServer(HttpHandler handler)
Equivalent to
new HttpServer( new HttpServerConf(), handler )
.
public HttpServer(HttpServerConf conf, HttpHandler handler)
After the constructor call, the `conf` object can be accessed through conf()
,
and it can be modified as long as the server is not started.
public HttpServerConf conf()
HttpServerConf
of this server.
Changes to configuration must be done before the server is started.
public void addUpgrader(String protocol, HttpUpgrader upgrader)
HttpUpgrader
for the protocol.
This method must be called before the server is started.
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 requests.
getConnectionCount()
to reach 0, within `graceTimeout`.
stopAll()
.