Class JettyServerComponent
- All Implemented Interfaces:
MicroserviceListener
MicroserviceListener fan-out.
Contributed by JettyConfiguration as a Microservice.start()
call invokes onStart(Microservice), which:
- Picks the first available port from the resolved
ports(falling back toJetty/port config, thenJetty-Port manifest, then8000 ). - Resolves the Jetty
Server— preferring an externally-supplied@Bean Server , otherwise building one fromjetty.xml via theJettyServerFactory. - Mounts servlets from
Jetty/servlets,Jetty/servletMap,Jetty/servletAttributesconfig sections, plus auto-mounts every@Bean Servletwhose runtime class is annotated with@Rest(atRest.path(), or"/" when no path is set). - Mounts every
@Bean ServletFilterRegistration(and the@Bean AuthFilterChain , at/* , unless a registration already wraps it) inorder. - Starts the server and publishes the bound port via the
availablePort/juneau.serverPortsystem 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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds a servlet filter to this Jetty server at the specified URL pattern.Adds a servlet filter to this Jetty server at one or more URL patterns.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.addFilter(ServletFilterRegistration registration) Adds a servlet filter registration to this Jetty server.addServlet(jakarta.servlet.Servlet servlet, String pathSpec) Adds an arbitrary servlet to this Jetty server.addServlet(jakarta.servlet.Servlet servlet, String... pathSpecs) Adds an arbitrary servlet to this Jetty server at one or more context paths.addServletAttribute(String name, Object value) Adds an attribute to the Jetty server.Returns the context path that this server is using.Returns the hostname of this microservice.intgetPort()Returns the port that this server is listening on.Returns whether this server is using"http" or"https" .org.eclipse.jetty.server.ServerReturns the underlying Jetty server.org.eclipse.jetty.ee11.servlet.ServletContextHandlerFinds and returns the servlet context handler defined in the Jetty container.getURI()Returns the URI where this server is listening.voidonConfigChange(Microservice ms, ConfigEvents events) Called if one or more changes occur in the config file.voidonStart(Microservice ms) Called at the beginning of theMicroservice.start()call.voidonStop(Microservice ms) Called at the end of theMicroservice.stop()call.
-
Constructor Details
-
JettyServerComponent
public JettyServerComponent()Constructor.
-
-
Method Details
-
onStart
Description copied from interface:MicroserviceListenerCalled at the beginning of theMicroservice.start()call.- Specified by:
onStartin interfaceMicroserviceListener- Parameters:
ms- Reference to microservice.
-
onStop
Description copied from interface:MicroserviceListenerCalled at the end of theMicroservice.stop()call.- Specified by:
onStopin interfaceMicroserviceListener- Parameters:
ms- Reference to microservice.
-
onConfigChange
Description copied from interface:MicroserviceListenerCalled if one or more changes occur in the config file.- Specified by:
onConfigChangein interfaceMicroserviceListener- Parameters:
ms- Reference to microservice.events- The list of changes in the config file.
-
addServlet
Adds an arbitrary servlet to this Jetty server.- Parameters:
servlet- The servlet instance.pathSpec- The context path of the servlet.- Returns:
- This object.
-
addServlet
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
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
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 inorder. Calling it directly is only possible once the server object exists (during startup) and before the server runs.- Parameters:
registration- The registration. Cannot benull .- Returns:
- This object.
- Throws:
IllegalArgumentException- If the registration isnull .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 theirREQUEST-only behavior.- Parameters:
filter- The filter instance. Cannot benull .dispatcherTypes- The dispatcher types. Cannot be empty.urlPatterns- The URL patterns. At least one is required; seeServletFilterRegistration.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
Returns the hostname of this microservice.- Returns:
- The hostname.
-
getPort
Returns the port that this server is listening on.- Returns:
- The port.
-
getProtocol
Returns whether this server is using"http" or"https" .- Returns:
- The protocol.
-
getServer
Returns the underlying Jetty server.- Returns:
- The underlying Jetty server.
- Throws:
NullPointerException- If the server has not been created yet.
-
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
Returns the URI where this server is listening.- Returns:
- The URI.
-