Interface Server

All Superinterfaces:
AutoCloseable

public interface Server extends AutoCloseable
Contract for HTTP server implementations that are designed to be managed by a Soklet instance.

Most Soklet applications will use the default Server (constructed via the withPort(Integer) builder factory method) and therefore do not need to implement this interface directly.

For example:

 SokletConfig config = SokletConfig.withServer(
  Server.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
  • Method Details

    • start

      void start()
      Starts the server, which makes it able to accept requests from clients.

      If the server is already started, no action is taken.

      This method is designed for internal use by Soklet only and should not be invoked elsewhere.

    • stop

      void stop()
      Stops the server, which makes it unable to accept requests from clients.

      If the server is already stopped, no action is taken.

      This method is designed for internal use by Soklet only and should not be invoked elsewhere.

    • isStarted

      Is this server started (that is, able to handle requests from clients)?
      Returns:
      true if the server is started, false otherwise
    • initialize

      void initialize(@NonNull SokletConfig sokletConfig, @NonNull Server.RequestHandler requestHandler)
      The Soklet instance which manages this Server will invoke this method exactly once at initialization time - this allows Soklet to "talk" to your Server.

      This method is designed for internal use by Soklet only and should not be invoked elsewhere.

      Parameters:
      sokletConfig - configuration for the Soklet instance that controls this server
      requestHandler - a Soklet-internal request handler which takes a Server-provided request as input and supplies a MarshaledResponse as output for the Server to write back to the client
    • close

      default void close() throws Exception
      AutoCloseable-enabled synonym for stop().

      This method is designed for internal use by Soklet only and should not be invoked elsewhere.

      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception - if an exception occurs while stopping the server
    • withPort

      Acquires a builder for Server instances.
      Parameters:
      port - the port number on which the server should listen
      Returns:
      the builder
    • fromPort

      Creates a Server configured with the given port and default settings.
      Parameters:
      port - the port number on which the server should listen
      Returns:
      a Server instance