The JBoss AS7 Command Line Interface has support for writing CLI scripts that can be executed against a running AS7 instance. You can also use the CLI public API to write sophisticated server management programs in java.
This article shows how to do something in between. You can use the full power of any JVM-based scripting language using a simplified wrapper class for the CLI API.
AS7.2 introduces the scriptsupport.CLI class. This class acts as a facade for the CLI public API. AS7.2 also introduces the CLI remote client jar which provides everything you need to connect to AS7 and execute CLI commands.
Example
As an example, we will implement a simple script in several different JVM-based scripting languages. The script will do the following:
- Connect to a local AS7 instance
- Determine if the instance is standalone or domain
- "cd" to the MBean containing runtime info
- Read the attributes for "server start time" and "server up time".
- Output those values.
- Disconnect from the AS7 instance.
Groovy CLI scripts
To run the groovy script below, just add the CLI remote client jar to the classpath when executing groovy:
groovy -cp jboss-cli-client.jar uptime.groovy
uptime.groovy
import org.jboss.as.cli.scriptsupport.* cli = CLI.newInstance() cli.connect() if (cli.getCommandContext().isDomainMode()) { cli.cmd("cd /host=master/core-service=platform-mbean/type=runtime") } else { cli.cmd("cd /core-service=platform-mbean/type=runtime") } result = cli.cmd(":read-attribute(name=start-time)") response = result.getResponse() startTime = response.get("result").asLong() result = cli.cmd(":read-attribute(name=uptime)") response = result.getResponse() serveruptime = response.get("result").asString() println() println("The server was started on " + new Date(startTime)) println("It has been running for " + serveruptime + "ms") cli.disconnect()
Rhino (javascript) CLI scripts
To run the Rhino script below, add the Rhino jars and the jboss-cli-client.jar to the classpath. You will also need to provide the Rhino main class.
java -cp js-14.jar;js.jar;jboss-cli-client.jar org.mozilla.javascript.tools.shell.Main -f uptime.js
uptime.js
importPackage(org.jboss.as.cli.scriptsupport) cli = CLI.newInstance() cli.connect() if (cli.getCommandContext().isDomainMode()) { cli.cmd("cd /host=master/core-service=platform-mbean/type=runtime") } else { cli.cmd("cd /core-service=platform-mbean/type=runtime") } result = cli.cmd(":read-attribute(name=start-time)") response = result.getResponse() startTime = response.get("result").asLong() result = cli.cmd(":read-attribute(name=uptime)") response = result.getResponse() serveruptime = response.get("result").asString() print() print("The server was started on " + new Date(startTime)) print("It has been running for " + serveruptime + "ms") cli.disconnect()
Jython CLI scripts
To run the Jython script below, you will need to add jboss-cli-client.jar to your system CLASSPATH. Just adding it to the python.path will not allow dynamic loading of some classes that CLI needs.
jython jython.py
jython.py
from java.util import Date from org.jboss.as.cli.scriptsupport import CLI cli = CLI.newInstance() cli.connect() if cli.getCommandContext().isDomainMode(): cli.cmd("cd /host=master/core-service=platform-mbean/type=runtime") else: cli.cmd("cd /core-service=platform-mbean/type=runtime") result = cli.cmd(":read-attribute(name=start-time)") response = result.getResponse() startTime = response.get("result").asLong() result = cli.cmd(":read-attribute(name=uptime)") response = result.getResponse() serveruptime = response.get("result").asString() print print 'The server was started on ' + Date(startTime).toString() print 'It has been running for ' + serveruptime + 'ms' cli.disconnect()
Using the scriptsupport.CLI class
As mentioned before, the org.jboss.as.cli.scriptsupport.CLI class acts as a facade for the CLI public API. For the most part, its methods are self-explanitory.
CLI.Result | cmd(String cliCommand) Execute a CLI command. |
void | connect() Connect to the server using the default host and port. |
void | connect(String username, char[] password) Connect to the server using the default host and port. |
void | connect(String controllerHost, int controllerPort, String username, char[] password) Connect to the server using a specified host and port. |
void |
Connect to the server using a specified protocol, host, and port. |
void | disconnect() Disconnect from the server. |
CommandContext | getCommandContext() Return the CLI CommandContext that was created when connected to the server. |
static CLI | newInstance() Create a new CLI instance. |
CLI.cmd() and CLI.Result
The cmd(String cliCommand)
method takes a String that can be anything you would normally enter in CLI's regular interactive mode. It returns CLI.Result, which has the following methods:
String | getCliCommand() Return the original command as a String. |
org.jboss.dmr.ModelNode | getRequest() If the command resulted in a server-side operation, return the ModelNode representation of the operation. |
org.jboss.dmr.ModelNode | getResponse() If the command resulted in a server-side operation, return the ModelNode representation of the response. |
boolean | isLocalCommand() Return true if the command was only executed locally and did not result in a server-side operation. |
boolean | isSuccess() Return true if the command was successful. |
CLI.Result tells you everything that happened when you invoked CLI.cmd(cliCommand). Most of the time, you just want to know your command was successful. So you can just call the isSuccess() method.
But if you want more details on the request and response, that information is available. Unless it is a local command, the command String is converted to a ModelNode object that the AS7 server understands. See Format of a Detyped Operation Request for details. A response from the server will also be in the form of a ModelNode object. See Format of a Detyped Operation Response for details on the response.
Comments