Class JettyMicroservice

java.lang.Object
org.apache.juneau.microservice.Microservice
org.apache.juneau.microservice.jetty.JettyMicroservice
All Implemented Interfaces:
ConfigEventListener

public class JettyMicroservice extends Microservice
Entry point for Juneau microservice that implements a REST interface using Jetty on a single port.
Jetty Server Details
The Jetty server is created by the createServer() method and started with the startServer() method. These methods can be overridden to provided customized behavior.
Defining REST Resources
Top-level REST resources are defined in the jetty.xml file as normal servlets.
See Also:
  • Constructor Details

  • Method Details

    • getInstance

      public static JettyMicroservice getInstance()
      Returns the Microservice instance.

      This method only works if there's only one Microservice instance in a JVM. Otherwise, it's just overwritten by the last instantiated microservice.

      Returns:
      The Microservice instance, or null if there isn't one.
    • main

      public static void main(String[] args) throws Exception
      Entry-point method.
      Parameters:
      args - Command line arguments.
      Throws:
      Exception - Error occurred.
    • create

      Creates a new microservice builder.
      Returns:
      A new microservice builder.
    • init

      Description copied from class: Microservice
      Initializes this microservice.

      This method can be called whenever the microservice is not started.

      It will initialize (or reinitialize) the console commands, system properties, and logger.

      Overrides:
      init in class Microservice
      Returns:
      This object.
      Throws:
      ParseException - Malformed input encountered.
      IOException - Couldn't read a file.
    • startConsole

      Description copied from class: Microservice
      Starts the console thread for this microservice.
      Overrides:
      startConsole in class Microservice
      Returns:
      This object.
      Throws:
      Exception - Error occurred
    • stopConsole

      Description copied from class: Microservice
      Stops the console thread for this microservice.
      Overrides:
      stopConsole in class Microservice
      Returns:
      This object.
      Throws:
      Exception - Error occurred
    • start

      Description copied from class: Microservice
      Start this application.

      Overridden methods MUST call this method FIRST so that the MicroserviceListener.onStart(Microservice) method is called.

      Overrides:
      start in class Microservice
      Returns:
      This object.
      Throws:
      Exception - Error occurred.
    • join

      public JettyMicroservice join() throws Exception
      Description copied from class: Microservice
      Joins the application with the current thread.

      Default implementation is a no-op.

      Overrides:
      join in class Microservice
      Returns:
      This object.
      Throws:
      Exception - Error occurred
    • stop

      public JettyMicroservice stop() throws Exception
      Description copied from class: Microservice
      Stop this application.

      Overridden methods MUST call this method LAST so that the MicroserviceListener.onStop(Microservice) method is called.

      Overrides:
      stop in class Microservice
      Returns:
      This object.
      Throws:
      Exception - Error occurred
    • getPort

      public int getPort()
      Returns the port that this microservice started up on.

      The value is determined by looking at the Server/Connectors[ServerConnector]/port value in the Jetty configuration.

      Returns:
      The port that this microservice started up on.
    • getContextPath

      Returns the context path that this microservice is using.

      The value is determined by looking at the Server/Handlers[ServletContextHandler]/contextPath value in the Jetty configuration.

      Returns:
      The context path that this microservice is using.
    • getProtocol

      public String getProtocol()
      Returns whether this microservice is using "http" or "https".

      The value is determined by looking for the existence of an SSL Connection Factorie by looking for the Server/Connectors[ServerConnector]/ConnectionFactories[SslConnectionFactory] value in the Jetty configuration.

      Returns:
      Whether this microservice is using "http" or "https".
    • getHostName

      public String getHostName()
      Returns the hostname of this microservice.

      Simply uses InetAddress.getLocalHost().getHostName().

      Returns:
      The hostname of this microservice.
    • getURI

      public URI getURI()
      Returns the URI where this microservice is listening on.
      Returns:
      The URI where this microservice is listening on.
    • createServer

      public org.eclipse.jetty.server.Server createServer() throws ParseException, IOException, ExecutableException
      Method used to create (but not start) an instance of a Jetty server.

      Subclasses can override this method to customize the Jetty server before it is started.

      The default implementation is configured by the following values in the config file if a jetty.xml is not specified via a REST/jettyXml setting:

      #================================================================================ # Jetty settings #================================================================================ [Jetty] # Path of the jetty.xml file used to configure the Jetty server. config = jetty.xml # Resolve Juneau variables in the jetty.xml file. resolveVars = true # Port to use for the jetty server. # You can specify multiple ports. The first available will be used. '0' indicates to try a random port. # The resulting available port gets set as the system property "availablePort" which can be referenced in the # jetty.xml file as "$S{availablePort}" (assuming resolveVars is enabled). port = 10000,0,0,0

      Returns:
      The newly-created server.
      Throws:
      ParseException - Configuration file contains malformed input.
      IOException - File could not be read.
      ExecutableException - Exception occurred on invoked constructor/method/field.
    • destroyServer

      public void destroyServer() throws Exception
      Calls Handler.Abstract.destroy() on the underlying Jetty server if it exists.
      Throws:
      Exception - Error occurred.
    • addServlet

      public JettyMicroservice addServlet(jakarta.servlet.Servlet servlet, String pathSpec)
      Adds an arbitrary servlet to this microservice.
      Parameters:
      servlet - The servlet instance.
      pathSpec - The context path of the servlet.
      Returns:
      This object.
      Throws:
      RuntimeException - if createServer() has not previously been called.
    • getServletContextHandler

      public org.eclipse.jetty.ee9.servlet.ServletContextHandler getServletContextHandler()
      Finds and returns the servlet context handler defined in the Jetty container.
      Returns:
      The servlet context handler.
      Throws:
      RuntimeException - if context handler is not defined.
    • addServletAttribute

      Adds a servlet attribute to the Jetty server.
      Parameters:
      name - The server attribute name.
      value - The context path of the servlet.
      Returns:
      This object.
      Throws:
      RuntimeException - if createServer() has not previously been called.
    • getServer

      public org.eclipse.jetty.server.Server getServer()
      Returns the underlying Jetty server.
      Returns:
      The underlying Jetty server, or null if createServer() has not yet been called.
    • startServer

      protected int startServer() throws Exception
      Method used to start the Jetty server created by createServer().

      Subclasses can override this method to customize server startup.

      Returns:
      The port that this server started on.
      Throws:
      Exception - Error occurred.