blob: 6c642d6a57f77f37a5f46cf6127f366e51c19849 [file] [log] [blame]
---
title: Executing gfsh Commands through the Management API
---
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
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 <%=vars.product_name%> 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.
``` pre
// 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:
``` pre
// 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.
``` pre
// 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");
}
```