|
A Talon XVM has the ability to discover annotated 'Command' methods provided by an application. Such commands can be invoked remotely by tools in an out of band fashion with an application's message driven event handlers to perform administrative functions. The command framework is designed to work in the context of command line tools, guis or for administrative applications. A command method is identified by annotating it with a @Command annotation, and its parameters are exposed by annotating its parameters with either @Option or @Argument annotations which allows commands to be invoked by passing the arguments in a command line format.
@Command(name = "resetOrderStats", description="Resets the number of orders processed") public String resetOrderStats(@Option(shortForm = 'v', longForm ="verbose", defaultValue="false") boolean verbose, @Argument(name = "newOrderReceivedCount"), position=1) long newNumOrdersReceived, @Argument(name = "newOrderProcessingCount", position=2) long newNumOrdersProcessed) { ... } |
Because command handlers are not executed on the application's business logic thread they are not allowed to touch application state. In general applications that intend to pass values back to command handlers should do so through member variables that are not part of the applications HA state that are declare as volatile. Alternatively, a command handler may inject a message into the application to perform logic that works with application state, but this is considered advanced usage as doing so is not a fault tolerant and blurs the lines between application and administrative traffic. |
At its simplest, a method that is composed of primitive arguments needs only to be annotated with a Command annotation to expose it as a command that can be invoked remotely.
@App public void MyApp { @AppStat(name = "Orders Processed") private volatile long numOrdersProcessed = 0; /** * Resets the number of orders processed * * @param newNumOrdersProcessed The new number of orders processed * @return The previous number of orders */ @Command public int resetOrdersProcessed(long newNumOrdersProcessed, boolean verbose) { long prevNumOrdersProcessed = numOrdersProcessed; numOrdersProcessed = numOrdersProcessed; String result = "Reset Orders Processed: " + prevNumOrdersProcessed + "->" + numOrdersProcessed; if(verbose) { System.out.println(result); } return result; } /** * Handles a new order. */ @EventHandler public final void onOrder(NewOrderMessage newOrder) { numOrdersReceived++; // ... do some processing numOrdersProcessed++ } } |
The above method can than be invoked via the Robin api as:
app.invokeCommand("resetOrdersProcessed 0 true") |
... the command name is identified by the name of the method and each parameter in the signature is treated as an argument to the command.
Above we gave a simple example of using the Command annotation to identify a command. The command can be further described by using the following command annotation elements
Parameter | Type | Description | Default |
---|---|---|---|
aliases | String [] | Returns the aliases used to invoke this command. This can be useful in cases where a command's | none |
description | String | A description of the command. This is used by tools to expose usage information for the command. It is a good idea to provide a description on commands as it provides tools with the ability to provide | none |
name | String | Returns the name of the command. If this parameter is omitted on an annotated Method then the name will default to the method name. | none |
parseOptions | boolean | Whether or not the command parser should attempt to parse options or if this command is | true |
The @Argument annotation can be applied to method parameters to describe and constrain arguments.
Parameter | Type | Description | Default |
---|---|---|---|
name | String | A short name for the argument. This is used to identify the argument when displaying command line usage. | none |
description | String | A brief description desribing what should be supplied for this argument. This is used to describe the argument when displaying command line usage | none |
defaultValue | String | The argument's default value (if not a required argument. This value is converted from the String supplied to the argument's type. | null |
required | boolean | Whether or not the argument is required. It is illegal to position a required argument after an optional one ... optional arguments. | true |
position | int | The position of the argument on the command line. The 1 based position of the argument for command line invocation. | none |
validOptions | String [] | Indicates the set of permissible values for the argument. | NULL |
Below is an example of how an Argument annotation may be used:
@Command(name = "resetOrderStats", description="Resets the number of orders processed") public int resetOrdersProcessed(@Option(shortForm = 'v', longForm ="verbose", defaultValue="false") boolean verbose, @Argument(name = "newOrderProcessingCount", position=1) long newNumOrdersProcessed) |
The @Option annotation can be applied to method parameters. An Option differs from an argument in that it is invoked using a command line switch. Options are useful in cases where a command method signature needs to be changed as scripts written against the old command signature will continue to function.
Parameter | Type | Description | Default |
---|---|---|---|
shortForm | char | The short (switch e.g. -o) form for the option. | none |
longForm | String | The long form for the argument (e.g. --option)). | none |
description | String | A brief description of the Option. | |
defaultValue | String | The option's default value. When the option is not specified the option will be set to this value. | null |
required | boolean | Whether or not the option is required. If the option is not specified but there is a default value, then the default will be used, otherwise it is an error. | true |
validOptions | String [] | Indicates the set of permissible values for the option. | NULL |
Below is an example of how an Argument annotation may be used:
@Command(name = "resetOrderStats", description="Resets the number of orders processed") public int resetOrdersProcessed(@Option(shortForm = 'v', longForm ="verbose", defaultValue="false") boolean verbose, @Argument(name = "newOrderProcessingCount", position=1) long newNumOrdersProcessed) |
The following types are returned when describing the command usage. In Json they are represented as the name from the enumeration below. The expectation is that tools will use this options in forms.
Valid Arguments and Options types:
Valid return types
All of the support argument types above and void.
Any @Command annotated method in the main application class will be discovered by a Talon XVM. If additional classes in your application contain @CommandHandler methods, they can be exposed to the Talon server using the @AppCommandHandlerContainersAccessor annotation which should add all objects that should be introspected for command handlers.
Example:
@AppHAPolicy(HAPolicy.EventSourcing) public static class MyApp { MyOtherClass someOtherClass = new MyOtherClass(); @AppCommandHandlerContainersAccessor public void getCommandHandlers(Set<Object> containers) { containers.add(someOtherClass ); } } class MyOtherClass { volatile int count = 0; MyOtherClass() { } @Command(name = "getCount", description="Returns a counter value") public int getCount() { return count; } } |
For Topic Oriented Applications, any @Managed object will be introspected for @Command methods. See ManagedObjectLocator. The DefaultManagedObjectLocator for Hornet calls TopicOrientedApplication.addAppCommandHandlerContainers(Set), so unless your application provides its own managed object locator, additional configured containers can be added by overriding addConfiguredContainers():
@AppHAPolicy(HAPolicy.EventSourcing) public static class MyApp { MyOtherClass someOtherClass = new MyOtherClass(); @Override public void addAppCommandHandlerContainers(Set<Object> containers) { containers.add(someOtherClass ); } } class MyOtherClass { volatile int count = 0; MyOtherClass() { } @Command(name = "getCount", description="Returns a counter value") public int getCount() { return count; } } |
Annotated command handlers can be discovered and invoked by deployment tools such as Robin and Lumino via admin connections made to the XVM in which the application is running.
The below command handler exercises all of the
@Command(name = "testCommand", aliases = { "TestCommandAlias" }, description = "A command that exercises all of the invocation APIs") public String testCommand(@Option(shortForm = 'b', longForm = "byteOption", required = true, description = "A byte Option", validOptions = { "0,", "1" }, defaultValue = "0") byte aByteOption, @Option(shortForm = 'c', longForm = "charOption", required = true, description = "A char Option", validOptions = { "a,", "b", "c" }, defaultValue = "b") char aCharOption, @Option(shortForm = 'n', longForm = "shortOption", required = true, description = "A short Option", validOptions = { "399,", "300" }, defaultValue = "399") short aShortOption, @Option(shortForm = 'i', longForm = "intOption", required = true, description = "An int Option", validOptions = { "20000,", "200000" }, defaultValue = "200000") int aIntOption, @Option(shortForm = 'l', longForm = "longOption", required = true, description = "A long Option", validOptions = { "100000000,", "2000000000" }, defaultValue = "100000000") long aLongOption, @Option(shortForm = 'f', longForm = "floatOption", required = true, description = "A float Option", validOptions = { "0.0,", "-2.0" }, defaultValue = "0.0") float aFloatOption, @Option(shortForm = 'd', longForm = "doubleOption", required = true, description = "A double Option", validOptions = { "0.0,", "1.2" }, defaultValue = "1.2") double aDoubleOption, @Option(shortForm = 's', longForm = "stringOption", required = true, description = "A String Option", validOptions = { "Foo,", "Bar" }, defaultValue = "Foo") String aStringOption, @Option(shortForm = 'm', longForm = "currencyOption", required = true, description = "A Currency Option", validOptions = { "USD,", "JPY" }, defaultValue = "JPY") Currency aCurrencyOption, @Option(shortForm = 'e', longForm = "enumOption", required = true, description = "An Enum Option", defaultValue = "PrettyPrint") JsonPrettyPrintStyle aEnumOption, @Argument(position = 1, required = true, name = "aByteArgument", description = "A byte Arguement", validOptions = { "0,", "1" }, defaultValue = "0") byte aByteArgument, @Argument(position = 2, required = true, name = "aCharArgument", description = "A char Arguement", validOptions = { "a,", "b", "c" }, defaultValue = "a") char aCharArgument, @Argument(position = 3, required = true, name = "aShortArgument", description = "A short Arguement", validOptions = { "399,", "300" }, defaultValue = "300") short aShortArgument, @Argument(position = 4, required = true, name = "aIntArgument", description = "An int Arguement", validOptions = { "20000,", "200000" }, defaultValue = "200000") int aIntArgument, @Argument(position = 5, required = true, name = "aLongArgument", description = "A long Arguement", validOptions = { "100000000,", "2000000000" }, defaultValue = "100000000") long aLongArgument, @Argument(position = 6, required = true, name = "aFloatArgument", description = "A float Arguement", validOptions = { "0.0,", "-2.01" }, defaultValue = "-2.01") float aFloatArgument, @Argument(position = 7, required = true, name = "aDoubleArgument", description = "A double Arguement", validOptions = { "0.0,", "1.2" }, defaultValue = "0.0") double aDoubleArgument, @Argument(position = 8, required = true, name = "aStringArgument", description = "A String Arguement", validOptions = { "Foo,", "Bar" }, defaultValue = "Foo") String aStringArgument, @Argument(position = 9, required = true, name = "aCurrencyArgument", description = "A Currency Arguement", validOptions = { "USD,", "JPY" }, defaultValue = "USD") Currency aCurrencyArgument, @Argument(position = 10, required = true, name = "aEnumArgument", description = "An Enum Arguement", defaultValue = "Minimal") JsonPrettyPrintStyle aEnumArgument) throws Exception { return "Received Arguments: " + Arrays.asList(new Object[] { aByteOption, aCharOption, aShortOption, aIntOption, aLongOption, aFloatOption, aDoubleOption, aStringOption, aCurrencyOption, aEnumOption, aByteArgument, aCharArgument, aShortArgument, aIntArgument, aLongArgument, aFloatArgument, aDoubleArgument, aStringArgument, aCurrencyArgument, aEnumArgument }).toString(); } |
testCommand A command that exercises all of the invocation APIs Usage: testCommand -b -c -n -i -l -f -d -s -m -e <aByteArgument> <aCharArgument> <aShortArgument> <aIntArgument> <aLongArgument> <aFloatArgument> <aDoubleArgument> <aStringArgument> <aCurrencyArgument> <aEnumArgument> <-b|--byteOption> <1|0,> Tests a byte Option default='0' <-c|--charOption> <a,|b|c> Tests a char Option default='b' <-n|--shortOption> <300|399,> Tests a short Option default='399' <-i|--intOption> <200000|20000,> Tests a int Option default='200000' <-l|--longOption> <100000000,|2000000000> Tests a long Option default='100000000' <-f|--floatOption> <0.0,|-2.0> Tests a float Option default='0.0' <-d|--doubleOption> <0.0,|1.2> Tests a double Option default='1.2' <-s|--stringOption> <Bar|Foo,> Tests a String Option default='Foo' <-m|--currencyOption> <JPY|USD,> Tests a Currency Option default='JPY' <-e|--enumOption> Tests a Enum Option default='PrettyPrint' [aByteArgument: <1|0,> Tests a byte Arguement default='0'] [aCharArgument: <a,|b|c> Tests a char Arguement default='a'] [aShortArgument: <300|399,> Tests a short Arguement default='300'] [aIntArgument: <200000|20000,> Tests a int Arguement default='200000'] [aLongArgument: <100000000,|2000000000> Tests a long Arguement default='100000000'] [aFloatArgument: <0.0,|-2.01> Tests a float Arguement default='-2.01'] [aDoubleArgument: <0.0,|1.2> Tests a double Arguement default='0.0'] [aStringArgument: <Bar|Foo,> Tests a String Arguement default='Foo'] [aCurrencyArgument: <JPY|USD,> Tests a Currency Arguement default='USD'] [aEnumArgument: Tests a Enum Arguement default='Minimal'] |
"name": "testCommand", "aliases": [ "TestCommandAlias" ], "description": "A command that exercises all of the invocation APIs", "additionalArguments": false, "arguments": [ { "position": 1, "required": true, "type": "BYTE", "name": "aByteArgument", "defaultValue": "0", "validValues": [ "1", "0," ], "description": "Tests a byte Arguement", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "position": 2, "required": true, "type": "CHAR", "name": "aCharArgument", "defaultValue": "a", "validValues": [ "a,", "b", "c" ], "description": "Tests a char Arguement", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "position": 3, "required": true, "type": "SHORT", "name": "aShortArgument", "defaultValue": "300", "validValues": [ "300", "399," ], "description": "Tests a short Arguement", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "position": 4, "required": true, "type": "INT", "name": "aIntArgument", "defaultValue": "200000", "validValues": [ "200000", "20000," ], "description": "Tests a int Arguement", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "position": 5, "required": true, "type": "LONG", "name": "aLongArgument", "defaultValue": "100000000", "validValues": [ "100000000,", "2000000000" ], "description": "Tests a long Arguement", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "position": 6, "required": true, "type": "FLOAT", "name": "aFloatArgument", "defaultValue": "-2.01", "validValues": [ "0.0,", "-2.01" ], "description": "Tests a float Arguement", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "position": 7, "required": true, "type": "DOUBLE", "name": "aDoubleArgument", "defaultValue": "0.0", "validValues": [ "0.0,", "1.2" ], "description": "Tests a double Arguement", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "position": 8, "required": true, "type": "STRING", "name": "aStringArgument", "defaultValue": "Foo", "validValues": [ "Bar", "Foo," ], "description": "Tests a String Arguement", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "position": 9, "required": true, "type": "STRING", "name": "aCurrencyArgument", "defaultValue": "USD", "validValues": [ "JPY", "USD," ], "description": "Tests a Currency Arguement", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "position": 10, "required": true, "type": "STRING", "name": "aEnumArgument", "defaultValue": "Minimal", "description": "Tests a Enum Arguement", "_xFieldBitmask_": [ 0 ], "xRogType": 0 } ], "options": [ { "shortForm": "b", "required": true, "type": "BYTE", "longForm": "byteOption", "defaultValue": "0", "validValues": [ "1", "0," ], "description": "Tests a byte Option", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "shortForm": "c", "required": true, "type": "CHAR", "longForm": "charOption", "defaultValue": "b", "validValues": [ "a,", "b", "c" ], "description": "Tests a char Option", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "shortForm": "n", "required": true, "type": "SHORT", "longForm": "shortOption", "defaultValue": "399", "validValues": [ "300", "399," ], "description": "Tests a short Option", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "shortForm": "i", "required": true, "type": "INT", "longForm": "intOption", "defaultValue": "200000", "validValues": [ "200000", "20000," ], "description": "Tests a int Option", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "shortForm": "l", "required": true, "type": "LONG", "longForm": "longOption", "defaultValue": "100000000", "validValues": [ "100000000,", "2000000000" ], "description": "Tests a long Option", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "shortForm": "f", "required": true, "type": "FLOAT", "longForm": "floatOption", "defaultValue": "0.0", "validValues": [ "0.0,", "-2.0" ], "description": "Tests a float Option", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "shortForm": "d", "required": true, "type": "DOUBLE", "longForm": "doubleOption", "defaultValue": "1.2", "validValues": [ "0.0,", "1.2" ], "description": "Tests a double Option", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "shortForm": "s", "required": true, "type": "STRING", "longForm": "stringOption", "defaultValue": "Foo", "validValues": [ "Bar", "Foo," ], "description": "Tests a String Option", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "shortForm": "m", "required": true, "type": "STRING", "longForm": "currencyOption", "defaultValue": "JPY", "validValues": [ "JPY", "USD," ], "description": "Tests a Currency Option", "_xFieldBitmask_": [ 0 ], "xRogType": 0 }, { "shortForm": "e", "required": true, "type": "STRING", "longForm": "enumOption", "defaultValue": "PrettyPrint", "description": "Tests a Enum Option", "_xFieldBitmask_": [ 0 ], "xRogType": 0 } ], "returnType": "VOID", "_xFieldBitmask_": [ 0 ], "xRogType": 0 } |