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
ConstructorsModifierConstructorDescriptionprotected
Constructor. -
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.Builder
create()
Creates a new microservice builder.org.eclipse.jetty.server.Server
Method used to create (but not start) an instance of a Jetty server.void
CallsHandler.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 JettyMicroservice
Returns the Microservice instance.int
getPort()
Returns the port that this microservice started up on.Returns whether this microservice is using"http" or"https" .org.eclipse.jetty.server.Server
Returns the underlying Jetty server.org.eclipse.jetty.ee9.servlet.ServletContextHandler
Finds 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 void
Entry-point method.start()
Start this application.Starts the console thread for this microservice.protected int
Method 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
-
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.
-
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 classMicroservice
- 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 classMicroservice
- Returns:
- This object.
- Throws:
Exception
- Error occurred
-
stopConsole
Description copied from class:Microservice
Stops the console thread for this microservice.- Overrides:
stopConsole
in classMicroservice
- 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 classMicroservice
- Returns:
- This object.
- Throws:
Exception
- Error occurred.
-
join
Description copied from class:Microservice
Joins the application with the current thread.Default implementation is a no-op.
- Overrides:
join
in classMicroservice
- Returns:
- This object.
- Throws:
Exception
- Error occurred
-
stop
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 classMicroservice
- Returns:
- This object.
- Throws:
Exception
- Error occurred
-
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
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
Returns the hostname of this microservice.Simply uses
InetAddress.getLocalHost().getHostName() .- Returns:
- The hostname of this microservice.
-
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, 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.
-
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.
-
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
- ifcreateServer()
has not previously been called.
-
getServer
Returns the underlying Jetty server.- Returns:
- The underlying Jetty server, or
null ifcreateServer()
has not yet been called.
-
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.
-