public class Microservice extends Object implements ConfigEventListener
A microservice defines a simple API for starting and stopping simple Java services contained in executable jars.
The general command for creating and starting a microservice from a main method is as follows:
Your microservice class must be specified as the
getArgs()
- The command-line arguments passed to the jar file.
getConfig()
- An external INI-style configuration file.
getManifest()
- The manifest file for the main jar file.
init()
- Gets executed immediately following construction.
start()
- Gets executed during startup.
stop()
- Gets executed when 'exit' is typed in the console or an external shutdown signal is received.
kill()
- Can be used to forcibly shut down the service. Doesn't get called during normal operation.
Modifier | Constructor and Description |
---|---|
protected |
Microservice(MicroserviceBuilder builder)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
static MicroserviceBuilder |
create()
Creates a new microservice builder.
|
void |
err(MessageBundle mb,
String messageKey,
Object... args)
Prints a localized message to STDERR.
|
boolean |
executeCommand(Args args,
Scanner in,
PrintWriter out)
Executes a console command.
|
String |
executeCommand(String command,
String input,
Object... args)
Convenience method for executing a console command directly.
|
void |
exit()
Stops the console (if it's started) and calls
System.exit(int) . |
Args |
getArgs()
Returns the command-line arguments passed into the application.
|
Config |
getConfig()
Returns the external INI-style configuration file that can be used to configure your microservice.
|
Map<String,ConsoleCommand> |
getConsoleCommands()
Returns the console commands associated with this microservice.
|
protected Scanner |
getConsoleReader()
Returns the console reader.
|
protected PrintWriter |
getConsoleWriter()
Returns the console writer.
|
static Microservice |
getInstance()
Returns the Microservice instance.
|
Logger |
getLogger()
Returns the logger for this microservice.
|
ManifestFile |
getManifest()
Returns the main jar manifest file contents as a simple
ObjectMap . |
VarResolver |
getVarResolver()
Returns the variable resolver for resolving variables in strings and files.
|
Microservice |
init()
Initializes this microservice.
|
Microservice |
join()
Joins the application with the current thread.
|
void |
kill()
Kill the JVM by calling
|
protected void |
log(Level level,
String message,
Object... args)
Logs a message to the log file.
|
void |
onConfigChange(ConfigEvents events)
Gets called immediately after a config file has been loaded.
|
void |
out(MessageBundle mb,
String messageKey,
Object... args)
Prints a localized message to the console writer.
|
protected File |
resolveFile(String path)
Resolves the specified path.
|
Microservice |
start()
Start this application.
|
Microservice |
startConsole()
Starts the console thread for this microservice.
|
Microservice |
stop()
Stop this application.
|
Microservice |
stopConsole()
Stops the console thread for this microservice.
|
protected Microservice(MicroserviceBuilder builder) throws IOException, ParseException
builder
- The builder containing the settings for this microservice.IOException
- Problem occurred reading file.ParseException
- Malformed input encountered.public static Microservice getInstance()
This method only works if there's only one Microservice instance in a JVM. Otherwise, it's just overwritten by the last instantiated microservice.
public static MicroserviceBuilder create()
protected File resolveFile(String path)
If the working directory has been explicitly specified, relative paths are resolved relative to that.
path
- The path to resolve.public Microservice init() throws IOException, ParseException
This method can be called whenever the microservice is not started.
It will initialize (or reinitialize) the console commands, system properties, and logger.
ParseException
- Malformed input encountered.IOException
- Couldn't read a file.public Microservice start() throws Exception
Overridden methods MUST call this method FIRST so that the MicroserviceListener.onStart(Microservice)
method is called.
Exception
- Error occurred.public Microservice startConsole() throws Exception
Exception
- Error occurredpublic Microservice stopConsole() throws Exception
Exception
- Error occurredpublic Args getArgs()
This method can be called from the class constructor.
See Args
for details on using this method.
public Config getConfig()
The config location is determined in the following order:
If all methods for locating the config fail, then this method returns an empty config.
Subclasses can set their own config file by using the following methods:
String variables are automatically resolved using the variable resolver returned by getVarResolver()
.
This method can be called from the class constructor.
public ManifestFile getManifest()
ObjectMap
.
This map consists of the contents of Manifest.getMainAttributes()
with the keys and entries converted to
simple strings.
This method can be called from the class constructor.
public VarResolver getVarResolver()
Variables can be controlled by the following methods:
public Logger getLogger()
public boolean executeCommand(Args args, Scanner in, PrintWriter out)
args
- The command arguments.
in
- Console input.out
- Console output.public String executeCommand(String command, String input, Object... args)
Allows you to execute a console command outside the console by simulating input and output.
command
- The command name to execute.input
- Optional input to the command. Can be args
- Optional command arguments to pass to the command.public Microservice join() throws Exception
Default implementation is a no-op.
Exception
- Error occurredpublic Microservice stop() throws Exception
Overridden methods MUST call this method LAST so that the MicroserviceListener.onStop(Microservice)
method is called.
Exception
- Error occurredpublic void exit() throws Exception
System.exit(int)
.Exception
- Error occurredpublic void kill()
public final Map<String,ConsoleCommand> getConsoleCommands()
protected Scanner getConsoleReader()
Subclasses can override this method to provide their own console input.
protected PrintWriter getConsoleWriter()
Subclasses can override this method to provide their own console output.
public void out(MessageBundle mb, String messageKey, Object... args)
Ignored if
mb
- The message bundle containing the message.messageKey
- The message key.args
- Optional MessageFormat
-style arguments.public void err(MessageBundle mb, String messageKey, Object... args)
Ignored if
mb
- The message bundle containing the message.messageKey
- The message key.args
- Optional MessageFormat
-style arguments.protected void log(Level level, String message, Object... args)
level
- The log level.message
- The message text.args
- Optional MessageFormat
-style arguments.public void onConfigChange(ConfigEvents events)
ConfigEventListener
onConfigChange
in interface ConfigEventListener
events
- The change events.Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.