{title:'Lifecycle Methods', created:'8.0.0'}

To review, the {@link oaj.microservice.Microservice} class contains the following lifecycle methods:

The {@link oaj.microservice.jetty.JettyMicroservice} class which extends from {@link oaj.microservice.Microservice} provides the following additional lifecycle methods:

The additional lifecycle methods are typically not called directly but are exposed to allow subclasses to provide customized behavior for these events. For this reason, these methods are left as non-final so that they can be overridden.

A typical implementation of an app with lifecycle methods might look like the following:

| public class App { | | private static final JettyMicroservice MICROSERVICE; | | public static void main(String[] args) { | MICROSERVICE = JettyMicroservice | .create() // Create builder. | .args(args) // Pass in args. | .servlets(RootResource.class) // A Juneau RestServlet class. | .build() // Create microservice. | .start() // Start microservice. | .startConsole() // Start console. | .join() // Join thread. | ; | } | | public static void restart() { | MICROSERVICE.stop().start(); | } | | public static void exit() { | MICROSERVICE.exit(); | } | }

Similar to {@link oaj.microservice.Microservice#getInstance()}, the {@link oaj.microservice.jetty.JettyMicroservice#getInstance()} also allows easy access to the microservice:

| public class App { | | public static void main(String[] args) { | JettyMicroservice | .create() // Create builder. | .args(args) // Pass in args. | .servlets(RootResource.class) // A Juneau RestServlet class. | .build() // Create microservice. | .start() // Start microservice. | .startConsole() // Start console. | .join() // Join thread. | ; | } | | public static void restart() { | JettyMicroservice.getInstance().stop().start(); | } | | public static void exit() { | JettyMicroservice.getInstance().exit(); | } | }