Class WebServer

java.lang.Object
net.optionfactory.undertow.WebServer
All Implemented Interfaces:
Closeable, AutoCloseable

public class WebServer extends Object implements Closeable
Undertow embedded web server running a single web application.

Given one or more classes that implement the Spring Web WebApplicationInitializer interface, the embedded web server can be instantiated with:


 new WebServer("0.0.0.0", 8080, Deployment.springSelfContained(OurWebApplication.class))
 


If web resources have to be fetched from the filesystem (e.g. while developing the web UI), the web server should be initialized with:


 new WebServer("0.0.0.0", 8080, Deployment.springWithExternalWebResources("src/main/webapp/", OurWebApplication.class))
 
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    WebServer(String listeningAddress, int listeningPort, Deployment deployment)
    Creates the embedded web server.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Gracefully undeploys the web application and stops the embedded web server.
    void
    Loads the servlet deployment and starts the embedded web server.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • WebServer

      public WebServer(String listeningAddress, int listeningPort, Deployment deployment)
      Creates the embedded web server. Deployment configuration can be built using helper methods in Deployment.
      Parameters:
      listeningAddress - the address this server is bound to
      listeningPort - the port number where the server accepts connections
      deployment - deployment configuration
  • Method Details

    • start

      public void start() throws javax.servlet.ServletException
      Loads the servlet deployment and starts the embedded web server.
      Throws:
      javax.servlet.ServletException - if the web server cannot be started
    • close

      public void close()
      Gracefully undeploys the web application and stops the embedded web server.

      If the web server instance is a Spring-managed bean, this method will be automatically called on context teardown. Otherwise a shutdown hook can be configured with:

      
       WebServer webServer = ...
       Runtime.getRuntime().addShutdownHook(new Thread(webServer::close));
       
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable