use of alma.acs.util.ProcessStreamGobbler in project ACS by ACS-Community.
the class ProcessUtilTest method testJava.
/**
* Tests ProcesUtil with java processes
*/
public void testJava() throws Exception {
Class<?> testClass = ProcessUtilTestDummy.class;
assertFalse(processUtil.isJavaProcessRunning(testClass));
// run dummy process
Process proc = runDummyProcess();
ProcessStreamGobbler gobbler = new ProcessStreamGobbler(proc, new DaemonThreadFactory(), true);
assertFalse("Dummy java process is supposed to still run after 3.5 seconds", gobbler.gobble(3500, TimeUnit.MILLISECONDS));
assertTrue(processUtil.isJavaProcessRunning(testClass));
List<String> stdout = gobbler.getStdout();
int msgCount = 0;
for (String msg : stdout) {
if (msg.startsWith("All is well ")) {
// ignore other lines with acsStartJava script output
msgCount++;
}
}
assertEquals("Expected two 'All is well' messages in 3.5 seconds (after ~ 0 and 2 seconds).", 2, msgCount);
List<String> stderr = gobbler.getStderr();
assertEquals(0, stderr.size());
// get PID of dummy process
List<String> pidList = processUtil.getJavaPIDs(testClass);
assertEquals("Expected to find one running instance of " + testClass.getName() + ".", 1, pidList.size());
logger.info("Found single dummy java class with PID=" + pidList.get(0));
// kill process
processUtil.killProcess(pidList.get(0), true);
assertFalse(processUtil.isJavaProcessRunning(testClass));
}
use of alma.acs.util.ProcessStreamGobbler in project ACS by ACS-Community.
the class ProcessUtil method getJavaPIDs.
/**
* Gets a map with key=(running java main classes) and value=(list of the process IDs).
* Filters out sun.tools.jps.Jps which is the tool used to get the processes.
* @return Map<classname, pid-list>
* @throws IOException
* @throws InterruptedException
*/
protected Map<String, List<String>> getJavaPIDs() throws IOException {
// The following command returns lines of the format
// 23551 com.cosylab.acs.maci.manager.app.Manager
// 29113 sun.tools.jps.Jps
String command = "jps -l";
Process proc = Runtime.getRuntime().exec(command);
ProcessStreamGobbler gob = new ProcessStreamGobbler(proc, new DaemonThreadFactory(), true);
gob.setDebug(DEBUG);
try {
// read stdout and stderr
if (!gob.gobble(10, TimeUnit.SECONDS)) {
throw new IOException("Failed to execute command '" + command + "' within 10 seconds");
}
if (gob.hasStreamReadErrors()) {
throw new IOException("Failed to read output of command '" + command + "'");
}
} catch (InterruptedException ex) {
throw new IOException("Thread reading output of command '" + command + "' got interrupted.");
}
// evaluate jps output
Map<String, List<String>> pidMap = new HashMap<String, List<String>>();
List<String> outlines = gob.getStdout();
String[] splitLine = null;
for (String line : outlines) {
if (line.length() > 0 && (splitLine = line.split(" ")).length == 2) {
String cname = splitLine[1];
if (!"sun.tools.jps.Jps".equals(cname)) {
String pid = splitLine[0];
List<String> pidList = pidMap.containsKey(cname) ? pidMap.get(cname) : new ArrayList<String>();
pidList.add(pid);
pidMap.put(cname, pidList);
}
} else {
logger.info("jps returned unexpected line '" + line + "'");
}
}
return pidMap;
}
Aggregations