Class JettyServerComponent

java.lang.Object
org.apache.juneau.microservice.jetty.JettyServerComponent
All Implemented Interfaces:
MicroserviceListener

public class JettyServerComponent extends Object implements MicroserviceListener
Drives the Jetty server lifecycle on behalf of the microservice via the MicroserviceListener fan-out.

Contributed by JettyConfiguration as a @Bean. The microservice's Microservice.start() call invokes onStart(Microservice), which:

  1. Picks the first available port from the resolved ports (falling back to Jetty/port config, then Jetty-Port manifest, then 8000).
  2. Resolves the Jetty Server — preferring an externally-supplied @Bean Server, otherwise building one from jetty.xml via the JettyServerFactory.
  3. Mounts servlets from Jetty/servlets, Jetty/servletMap, Jetty/servletAttributes config sections, plus auto-mounts every @Bean Servlet whose runtime class is annotated with @Rest (at Rest.path(), or "/" when no path is set).
  4. Mounts every @Bean ServletFilterRegistration (and the @Bean AuthFilterChain, at /*, unless a registration already wraps it) in order.
  5. Starts the server and publishes the bound port via the availablePort / juneau.serverPort system properties.

onStop(Microservice) performs the inverse: stops the server gracefully in a worker thread.

External callers reach this component via Microservice.getInstance().getBeanStore().getBean(JettyServerComponent.class).orElseThrow().

See Also:
Since:
10.0.0
  • Constructor Details

  • Method Details

    • onStart

      public void onStart(Microservice ms)
      Description copied from interface: MicroserviceListener
      Called at the beginning of the Microservice.start() call.
      Specified by:
      onStart in interface MicroserviceListener
      Parameters:
      ms - Reference to microservice.
    • onStop

      public void onStop(Microservice ms)
      Description copied from interface: MicroserviceListener
      Called at the end of the Microservice.stop() call.
      Specified by:
      onStop in interface MicroserviceListener
      Parameters:
      ms - Reference to microservice.
    • onConfigChange

      public void onConfigChange(Microservice ms, ConfigEvents events)
      Description copied from interface: MicroserviceListener
      Called if one or more changes occur in the config file.
      Specified by:
      onConfigChange in interface MicroserviceListener
      Parameters:
      ms - Reference to microservice.
      events - The list of changes in the config file.
    • addServlet

      public JettyServerComponent addServlet(jakarta.servlet.Servlet servlet, String pathSpec)
      Adds an arbitrary servlet to this Jetty server.
      Parameters:
      servlet - The servlet instance.
      pathSpec - The context path of the servlet.
      Returns:
      This object.
    • addServlet

      public JettyServerComponent addServlet(jakarta.servlet.Servlet servlet, String... pathSpecs)
      Adds an arbitrary servlet to this Jetty server at one or more context paths.
      Parameters:
      servlet - The servlet instance.
      pathSpecs - The context paths of the servlet.
      Returns:
      This object.
    • addFilter

      public JettyServerComponent addFilter(jakarta.servlet.Filter filter, String urlPattern)
      Adds a servlet filter to this Jetty server at the specified URL pattern.
      Parameters:
      filter - The filter instance.
      urlPattern - The URL pattern the filter applies to (e.g. "/*", "/api/*").
      Returns:
      This object.
    • addFilter

      public JettyServerComponent addFilter(jakarta.servlet.Filter filter, String... urlPatterns)
      Adds a servlet filter to this Jetty server at one or more URL patterns.
      Parameters:
      filter - The filter instance.
      urlPatterns - The URL patterns the filter applies to.
      Returns:
      This object.
    • addFilter

      Adds a servlet filter registration to this Jetty server.

      Maps the filter on every URL pattern and dispatcher type of the registration with one mapping, so the filter runs at most once per dispatch even when the patterns overlap. Mapping order is call order, so call this outermost first.

      Applications contribute a ServletFilterRegistration @Bean instead; onStart(Microservice) applies the beans through this method in order. Calling it directly is only possible once the server object exists (during startup) and before the server runs.

      Parameters:
      registration - The registration. Cannot be null.
      Returns:
      This object.
      Throws:
      IllegalArgumentException - If the registration is null.
      IllegalStateException - If the server has already started.
    • addFilter

      public JettyServerComponent addFilter(jakarta.servlet.Filter filter, Set<jakarta.servlet.DispatcherType> dispatcherTypes, String... urlPatterns)
      Adds a servlet filter to this Jetty server on the given dispatcher types and URL patterns.

      Shorthand for addFilter(ServletFilterRegistration) with the default order and async support, and the same validation. The overloads without dispatcher types keep their REQUEST-only behavior.

      Parameters:
      filter - The filter instance. Cannot be null.
      dispatcherTypes - The dispatcher types. Cannot be empty.
      urlPatterns - The URL patterns. At least one is required; see ServletFilterRegistration.Builder#urlPatterns(String...).
      Returns:
      This object.
      Throws:
      IllegalArgumentException - If an argument is invalid.
      IllegalStateException - If the server has already started.
    • addServletAttribute

      Adds an attribute to the Jetty server.
      Parameters:
      name - The server attribute name.
      value - The attribute value.
      Returns:
      This object.
    • getContextPath

      Returns the context path that this server is using.
      Returns:
      The context path.
    • getHostName

      public String getHostName()
      Returns the hostname of this microservice.
      Returns:
      The hostname.
    • getPort

      public int getPort()
      Returns the port that this server is listening on.
      Returns:
      The port.
    • getProtocol

      public String getProtocol()
      Returns whether this server is using "http" or "https".
      Returns:
      The protocol.
    • getServer

      public org.eclipse.jetty.server.Server getServer()
      Returns the underlying Jetty server.
      Returns:
      The underlying Jetty server.
      Throws:
      NullPointerException - If the server has not been created yet.
    • getServletContextHandler

      public org.eclipse.jetty.ee11.servlet.ServletContextHandler getServletContextHandler()
      Finds and returns the servlet context handler defined in the Jetty container.
      Returns:
      The servlet context handler.
      Throws:
      IllegalStateException - If the context handler is not defined.
    • getURI

      public URI getURI()
      Returns the URI where this server is listening.
      Returns:
      The URI.