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

The lifecycle methods of the {@link oaj.microservice.Microservice} class consists of the following:

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

| public class App { | | private static final Microservice MICROSERVICE; | | public static void main(String[] args) { | MICROSERVICE = Microservice | .create() // Create builder. | .args(args) // Pass in args. | .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(); | } | }

If your application consists of a single microservice, you can use the {@link oaj.microservice.Microservice#getInstance()} method from anywhere in your code:

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

The {@link oaj.microservice.Microservice#startConsole()} and {@link oaj.microservice.Microservice#stopConsole()} control the lifecycle of the console commands. Typically you'll want to control these separately from the app so that you can easily restart your application from the console without affecting the console itself.

The lifecycle methods on the {@link oaj.microservice.Microservice} class are purposely left non-final so that subclasses can override them to provide customized behavior.