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:

- {@link oaj.microservice.MicroserviceBuilder}
  - {@link oaj.microservice.MicroserviceBuilder#consoleEnabled(boolean) consoleEnabled(boolean)}
  - {@link oaj.microservice.MicroserviceBuilder#consoleCommands(ConsoleCommand...) consoleCommands(ConsoleCommand...)}
  - {@link oaj.microservice.MicroserviceBuilder#consoleCommands(Class...) consoleCommands(Class...)}
  - {@link oaj.microservice.MicroserviceBuilder#console(Scanner,PrintWriter) 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 {@link oaj.microservice.MicroserviceBuilder#consoleCommands(ConsoleCommand...) consoleCommands(ConsoleCommand...)} builder method.

The API for defining console commands is shown below:

- {@link oaj.microservice.console.ConsoleCommand}
  - {@link oaj.microservice.console.ConsoleCommand#execute(Scanner,PrintWriter,Args) execute(Scanner,PrintWriter,Args)}
  - {@link oaj.microservice.console.ConsoleCommand#getDescription() getDescription()}
  - {@link oaj.microservice.console.ConsoleCommand#getExamples() getExamples()}
  - {@link oaj.microservice.console.ConsoleCommand#getInfo() getInfo()}
  - {@link oaj.microservice.console.ConsoleCommand#getName() getName()}
  - {@link oaj.microservice.console.ConsoleCommand#getSynopsis() getSynopsis()}

By default, the console input and output are taken from {@link java.lang.System.in} and {@link java.lang.System.out}. These can be overridden using the {@link oaj.microservice.MicroserviceBuilder#console(Scanner,PrintWriter) console(Scanner,PrintWriter)} method.