Interface HttpServer
- All Superinterfaces:
AutoCloseable
Contract for HTTP server implementations that are designed to be managed by a
Soklet instance.
Most Soklet applications will use the default HttpServer (constructed via the withPort(Integer) builder factory method) and therefore do not need to implement this interface directly.
For example:
SokletConfig config = SokletConfig.withHttpServer(
HttpServer.fromPort(8080)
).build();
try (Soklet soklet = Soklet.fromConfig(config)) {
soklet.start();
System.out.println("Soklet started, press [enter] to exit");
soklet.awaitShutdown(ShutdownTrigger.ENTER_KEY);
}- Author:
- Mark Allen
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classBuilder used to construct a standard implementation ofHttpServer.static interfaceRequest/response processing contract forHttpServerimplementations. -
Method Summary
Modifier and TypeMethodDescriptiondefault voidclose()AutoCloseable-enabled synonym forstop().static @NonNull HttpServerCreates aHttpServerconfigured with the given port and default settings.voidinitialize(@NonNull SokletConfig sokletConfig, @NonNull HttpServer.RequestHandler requestHandler) TheSokletinstance which manages thisHttpServerwill invoke this method exactly once at initialization time - this allowsSokletto "talk" to yourHttpServer.Is this server started (that is, able to handle requests from clients)?voidstart()Starts the server, which makes it able to accept requests from clients.voidstop()Stops the server, which makes it unable to accept requests from clients.static @NonNull HttpServer.BuilderAcquires a builder forHttpServerinstances.
-
Method Details
-
start
-
stop
-
isStarted
-
initialize
void initialize(@NonNull SokletConfig sokletConfig, @NonNull HttpServer.RequestHandler requestHandler) TheSokletinstance which manages thisHttpServerwill invoke this method exactly once at initialization time - this allowsSokletto "talk" to yourHttpServer.This method is designed for internal use by
Sokletonly and should not be invoked elsewhere.- Parameters:
sokletConfig- configuration for the Soklet instance that controls this serverrequestHandler- aSoklet-internal request handler which takes aHttpServer-provided request as input and supplies aMarshaledResponseas output for theHttpServerto write back to the client
-
close
AutoCloseable-enabled synonym forstop().This method is designed for internal use by
Sokletonly and should not be invoked elsewhere.- Specified by:
closein interfaceAutoCloseable- Throws:
Exception- if an exception occurs while stopping the server
-
withPort
Acquires a builder forHttpServerinstances.- Parameters:
port- the port number on which the server should listen- Returns:
- the builder
-
fromPort
Creates aHttpServerconfigured with the given port and default settings.- Parameters:
port- the port number on which the server should listen- Returns:
- a
HttpServerinstance
-