Skip to main content

Console Commands

The Microservice API provides support for simple console commands.

public static void main(String[] args) {
Microservice
.create()
.args(args)
.build()
.start()
.startConsole() // Start console.
.join()
;
}

When started, the console renders the following output:

Running class 'Microservice' using config file 'my-microservice.cfg'.

List of available commands:
exit -- Shut down service
restart -- Restarts service
help -- Commands help

>

The builder methods for controlling the console are as follows:

Microservice.BuilderconsoleEnabled(boolean)consoleCommands(ConsoleCommand...)consoleCommands(Class...)console(Scanner,PrintWriter)

By default, the supported commands are pulled from the configuration file:

#=======================================================================================================================
# Console settings

#=======================================================================================================================
[Console]

enabled = true

# List of available console commands.
# These are classes that implements ConsoleCommand that allow you to submit commands to the microservice via
# the console.
# When listed here, the implementations must provide a no-arg constructor.
# They can also be provided dynamically by overriding the Microservice.createConsoleCommands() method.

commands =
org.apache.juneau.microservice.console.ExitCommand,
org.apache.juneau.microservice.console.RestartCommand,
org.apache.juneau.microservice.console.HelpCommand

New commands can be added by adding them to the configuration file, or programmatically using the consoleCommands(ConsoleCommand...) builder method. The API for defining console commands is shown below:

ConsoleCommandexecute(Scanner,PrintWriter,Args)getDescription()getExamples()getInfo()getName()getSynopsis()

By default, the console input and output are taken from System.in and System.out. These can be overridden using the console(Scanner,PrintWriter) method.