Skip to main content

Lifecycle Methods

The lifecycle methods of the Microservice class consists of the following:

Microservicestart() startConsole() join() stop() stopConsole() exit() kill()

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 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 Microservice.startConsole() and 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 Microservice class are purposely left non-final so that subclasses can override them to provide customized behavior.