Class JettyMicroservice
- All Implemented Interfaces:
ConfigEventListener
Jetty Server Details
The Jetty server is created by thecreateServer() 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 theSee Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor. -
Method Summary
Modifier and TypeMethodDescriptionaddServlet(jakarta.servlet.Servlet servlet, String pathSpec) Adds an arbitrary servlet to this microservice.addServletAttribute(String name, Object value) Adds a servlet attribute to the Jetty server.static JettyMicroservice.Buildercreate()Creates a new microservice builder.org.eclipse.jetty.server.ServerMethod used to create (but not start) an instance of a Jetty server.voidCallsHandler.Abstract.destroy()on the underlying Jetty server if it exists.Returns the context path that this microservice is using.Returns the hostname of this microservice.static JettyMicroserviceReturns the Microservice instance.intgetPort()Returns the port that this microservice started up on.Returns whether this microservice 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 microservice is listening on.init()Initializes this microservice.join()Joins the application with the current thread.static voidEntry-point method.start()Start this application.Starts the console thread for this microservice.protected intMethod used to start the Jetty server created bycreateServer().stop()Stop this application.Stops the console thread for this microservice.Methods inherited from class org.apache.juneau.microservice.Microservice
err, executeCommand, executeCommand, exit, getArgs, getConfig, getConsoleCommands, getConsoleReader, getConsoleWriter, getLogger, getManifest, getVarResolver, kill, log, onConfigChange, out, resolveFile
-
Constructor Details
-
JettyMicroservice
Constructor.- Parameters:
builder- The constructor arguments.- Throws:
IOException- Problem occurred reading file.ParseException- Malformed content found in config file.
-
-
Method Details
-
create
Creates a new microservice builder.- Returns:
- A new microservice builder.
-
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
Entry-point method.- Parameters:
args- Command line arguments.- Throws:
Exception- Error occurred.
-
addServlet
Adds an arbitrary servlet to this microservice.- Parameters:
servlet- The servlet instance.pathSpec- The context path of the servlet.- Returns:
- This object.
- Throws:
RuntimeException- ifcreateServer()has not previously been called.
-
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- ifcreateServer()has not previously been called.
-
createServer
public org.eclipse.jetty.server.Server createServer() throws ParseException, IOException, ExecutableExceptionMethod 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
CallsHandler.Abstract.destroy()on the underlying Jetty server if it exists.- Throws:
Exception- Error occurred.
-
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.
-
getHostName
Returns the hostname of this microservice.Simply uses
InetAddress.getLocalHost().getHostName() .- Returns:
- The hostname of this microservice.
-
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.
-
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" .
-
getServer
Returns the underlying Jetty server.- Returns:
- The underlying Jetty server, or
null ifcreateServer()has not yet been called.
-
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.
-
getURI
Returns the URI where this microservice is listening on.- Returns:
- The URI where this microservice is listening on.
-
init
Description copied from class:MicroserviceInitializes 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:
initin classMicroservice- Returns:
- This object.
- Throws:
ParseException- Malformed input encountered.IOException- Couldn't read a file.
-
join
Description copied from class:MicroserviceJoins the application with the current thread.Default implementation is a no-op.
- Overrides:
joinin classMicroservice- Returns:
- This object.
- Throws:
Exception- Error occurred
-
start
Description copied from class:MicroserviceStart this application.Overridden methods MUST call this method FIRST so that the
MicroserviceListener.onStart(Microservice)method is called.- Overrides:
startin classMicroservice- Returns:
- This object.
- Throws:
Exception- Error occurred.
-
startConsole
Description copied from class:MicroserviceStarts the console thread for this microservice.- Overrides:
startConsolein classMicroservice- Returns:
- This object.
- Throws:
Exception- Error occurred
-
stop
Description copied from class:MicroserviceStop this application.Overridden methods MUST call this method LAST so that the
MicroserviceListener.onStop(Microservice)method is called.- Overrides:
stopin classMicroservice- Returns:
- This object.
- Throws:
Exception- Error occurred
-
stopConsole
Description copied from class:MicroserviceStops the console thread for this microservice.- Overrides:
stopConsolein classMicroservice- Returns:
- This object.
- Throws:
Exception- Error occurred
-
startServer
Method used to start the Jetty server created bycreateServer().Subclasses can override this method to customize server startup.
- Returns:
- The port that this server started on.
- Throws:
Exception- Error occurred.
-