Pivotal GemFire® v8.1

Executing gfsh Commands through the Management API

Executing gfsh Commands through the Management API

You can also use management APIs to execute gfsh commands programmatically.

Note: If you start the JMX Manager programmatically and wish to enable command processing, you must also add the absolute path of gfsh-dependencies.jar (located in $GEMFIRE/lib of your GemFire installation) to the CLASSPATH of your application. Do not copy this library to your CLASSPATH because this library refers to other dependencies in $GEMFIRE/lib by a relative path. The following code samples demonstrate how to process and execute gfsh commands using the Java API.

First, retrieve a CommandService instance.
Note: The CommandService API is currently only available on JMX Manager nodes.
// Get existing CommandService instance or create new if it doesn't exist
commandService = CommandService.createLocalCommandService(cache);

// OR simply get CommandService instance if it exists, don't create new one
CommandService commandService = CommandService.getUsableLocalCommandService();
Next, process the command and its output:
 // Process the user specified command String
 Result regionListResult = commandService.processCommand("list regions");
// Iterate through Command Result in String form line by line
while (regionListResult.hasNextLine()) {
Alternatively, instead of processing the command, you can create a CommandStatement Object from the command string which can be re-used.
// Create a command statement that can be reused multiple times
CommandStatement showDeadLocksCmdStmt = commandService.createCommandStatement
("show dead-locks --file=deadlock-info.txt");
Result showDeadlocksResult = showDeadLocksCmdStmt.process();

// If there is a file as a part of Command Result, it can be saved to a specified directory
if (showDeadlocksResult.hasIncomingFiles()) {
     showDeadlocksResult.saveIncomingFiles(System.getProperty("user.dir") + "/commandresults");