This topic explains how to execute gfsh commands programmatically through the VMware Tanzu GemFire management API.

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 class path of your application. Do not copy this library to your class path 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 does not exist
commandService = CommandService.createLocalCommandService(cache);

// OR simply get CommandService instance if it exists, do not 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()) {
   System.out.println(regionListResult.nextLine());
}
      

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");
}
check-circle-line exclamation-circle-line close-line
Scroll to top icon