public class RestMicroservice extends Microservice
createServer()
method and started with the startServer()
method.
These methods can be overridden to provided customized behavior.
jetty.xml
file as normal servlets.
Microservice.initLogging()
method.
This method can be overridden to provide customized logging behavior.
Microservice.onStart()
- Gets executed before start()
.
Microservice.onStop()
- Gets executed before stop()
.
onCreateServer()
- Gets executed before createServer()
.
onStartServer()
- Gets executed before startServer()
.
onPostStartServer()
- Gets executed after startServer()
.
onStopServer()
- Gets executed before stop()
.
onPostStopServer()
- Gets executed after stop()
.
Constructor and Description |
---|
RestMicroservice(String... args)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
RestMicroservice |
addServlet(javax.servlet.Servlet servlet,
String pathSpec)
Adds an arbitrary servlet to this microservice.
|
RestMicroservice |
addServletAttribute(String name,
Object value)
Adds a servlet attribute to the Jetty server.
|
protected org.eclipse.jetty.server.Server |
createServer()
Method used to create (but not start) an instance of a Jetty server.
|
String |
getContextPath()
Returns the context path that this microservice is using.
|
String |
getHostName()
Returns the hostname of this microservice.
|
static RestMicroservice |
getInstance()
Returns the Microservice instance.
|
int |
getPort()
Returns the port that this microservice started up on.
|
String |
getProtocol()
Returns whether this microservice is using
|
org.eclipse.jetty.server.Server |
getServer()
Returns the underlying Jetty server.
|
URI |
getURI()
Returns the URI where this microservice is listening on.
|
RestMicroservice |
join()
Joins the application with the current thread.
|
static void |
main(String[] args)
Main method.
|
void |
onConfigChange(List<ConfigEvent> events)
Called when
Config.commit() is called on the config file. |
protected void |
onCreateServer()
Called before
createServer() is called. |
protected void |
onPostStartServer()
Called after the Jetty server is started.
|
protected void |
onPostStopServer()
Called after the Jetty server is stopped.
|
protected void |
onStartServer()
Called before
startServer() is called. |
protected void |
onStopServer()
Called before the Jetty server is stopped.
|
RestMicroservice |
setConfig(String cfPath,
boolean create)
Specifies the path of the config file for this microservice.
|
RestMicroservice |
setJettyXml(Object jettyXml)
Sets the
jetty.xml used to configure the Jetty server. |
RestMicroservice |
setManifestContents(String... contents)
Convenience method for specifying the manifest contents directly.
|
RestMicroservice |
start()
Start this application.
|
protected int |
startServer()
Method used to start the Jetty server created by
createServer() . |
RestMicroservice |
stop()
Stop this application.
|
createConsoleCommands, createVarResolver, err, getArgs, getConfig, getConsoleCommands, getConsoleReader, getConsoleWriter, getLogger, getManifest, getVarResolver, initLogging, kill, log, onStart, onStop, out, setArgs, setConfig, setManifest, setManifest, setManifest, setManifest, startConsole
public RestMicroservice(String... args) throws Exception
args
- Command line arguments.Exception
public static RestMicroservice getInstance()
This method only works if there's only one Microservice instance in a JVM.
Otherwise, it's just overwritten by the last call to RestMicroservice(String...)
.
public static void main(String[] args) throws Exception
Subclasses must also implement this method!
args
- Command line arguments.Exception
public RestMicroservice start() throws Exception
Microservice
Default implementation simply calls Microservice.onStart()
.
Overridden methods MUST call this method FIRST so that the Microservice.onStart()
method is called.
start
in class Microservice
Exception
public RestMicroservice join() throws Exception
Microservice
Default implementation is a no-op.
join
in class Microservice
Exception
public RestMicroservice stop()
Microservice
Default implementation simply calls Microservice.onStop()
.
Overridden methods MUST call this method LAST so that the Microservice.onStop()
method is called.
stop
in class Microservice
public int getPort()
The value is determined by looking at the Server/Connectors[ServerConnector]/port
value in the
Jetty configuration.
public String getContextPath()
The value is determined by looking at the Server/Handlers[ServletContextHandler]/contextPath
value
in the Jetty configuration.
public String getProtocol()
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.
public String getHostName()
Simply uses InetAddress.getLocalHost().getHostName()
.
public URI getURI()
protected org.eclipse.jetty.server.Server createServer() throws Exception
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:
Exception
public RestMicroservice addServlet(javax.servlet.Servlet servlet, String pathSpec)
servlet
- The servlet instance.pathSpec
- The context path of the servlet.RuntimeException
- if createServer()
has not previously been called.public RestMicroservice addServletAttribute(String name, Object value)
name
- The server attribute name.value
- The context path of the servlet.RuntimeException
- if createServer()
has not previously been called.public org.eclipse.jetty.server.Server getServer()
createServer()
has not yet been called.protected int startServer() throws Exception
createServer()
.
Subclasses can override this method to customize server startup.
Exception
public void onConfigChange(List<ConfigEvent> events)
Config.commit()
is called on the config file.
The default behavior is configured by the following value in the config file:
onConfigChange
in interface ConfigEventListener
onConfigChange
in class Microservice
events
- The list of changes in the config file.public RestMicroservice setJettyXml(Object jettyXml)
jetty.xml
used to configure the Jetty server.
jettyXml
- The jetty.xml
.
File
representing the location on the file system.
InputStream
containing the contents of the file.
String
representing the file system path.
protected void onCreateServer()
createServer()
is called.
Subclasses can override this method to hook into the lifecycle of this application.
protected void onStartServer()
startServer()
is called.
Subclasses can override this method to hook into the lifecycle of this application.
protected void onPostStartServer()
Subclasses can override this method to hook into the lifecycle of this application.
protected void onStopServer()
Subclasses can override this method to hook into the lifecycle of this application.
protected void onPostStopServer()
Subclasses can override this method to hook into the lifecycle of this application.
public RestMicroservice setConfig(String cfPath, boolean create) throws IOException
Microservice
If you do not specify the config file location, we attempt to resolve it through the following methods:
Main-Config
entry in the manifest file.
If this path does not exist, a FileNotFoundException
will be thrown from the Microservice.start()
command.
setConfig
in class Microservice
cfPath
- The absolute or relative path of the config file.create
- Create the file if it doesn't exist.IOException
- If config file does not exist at the specified location or could not be read or created.public RestMicroservice setManifestContents(String... contents) throws IOException
Microservice
setManifestContents
in class Microservice
contents
- The lines in the manifest file.IOException
Copyright © 2018 Apache. All rights reserved.