use of org.apache.commons.exec.ExecuteResultHandler in project stanbol by apache.
the class JarExecutor method start.
/**
* Start the jar if not done yet, and setup runtime hook to stop it.
*/
public void start() throws Exception {
final ExecuteResultHandler h = new ExecuteResultHandler() {
@Override
public void onProcessFailed(ExecuteException ex) {
log.error("Process execution failed:" + ex, ex);
}
@Override
public void onProcessComplete(int result) {
log.info("Process execution complete, exit code=" + result);
}
};
final String vmOptions = System.getProperty("jar.executor.vm.options");
final Executor e = new DefaultExecutor();
if (this.workingDirectory != null) {
e.setWorkingDirectory(this.workingDirectory);
}
final CommandLine cl = new CommandLine(javaExecutable);
if (vmOptions != null && vmOptions.length() > 0) {
// not the case for common usage patterns
for (String option : StringUtils.split(vmOptions, " ")) {
cl.addArgument(option);
}
}
cl.addArgument("-jar");
cl.addArgument(jarToExecute.getAbsolutePath());
cl.addArgument("-p");
cl.addArgument(String.valueOf(serverPort));
log.info("Executing " + cl);
e.setStreamHandler(new PumpStreamHandler());
e.setProcessDestroyer(new ShutdownHookProcessDestroyer());
e.execute(cl, h);
}
use of org.apache.commons.exec.ExecuteResultHandler in project sling by apache.
the class JarExecutor method start.
/** Start the jar if not done yet, and setup runtime hook
* to stop it.
*/
public void start() throws Exception {
final ExecuteResultHandler h = new ExecuteResultHandler() {
public void onProcessFailed(ExecuteException ex) {
log.error("Process execution failed:" + ex, ex);
}
public void onProcessComplete(int result) {
log.info("Process execution complete, exit code=" + result);
}
};
final String vmOptions = config.getProperty(PROP_VM_OPTIONS);
executor = new DefaultExecutor();
final CommandLine cl = new CommandLine(jvmFullPath);
if (vmOptions != null && vmOptions.length() > 0) {
cl.addArguments(vmOptions);
}
cl.addArgument("-jar");
cl.addArgument(jarToExecute.getAbsolutePath());
// Additional options for the jar that's executed.
// $JAREXEC_SERVER_PORT$ is replaced our serverPort value
String jarOptions = config.getProperty(PROP_JAR_OPTIONS);
if (jarOptions != null && jarOptions.length() > 0) {
jarOptions = jarOptions.replaceAll("\\$JAREXEC_SERVER_PORT\\$", String.valueOf(serverPort));
log.info("Executable jar options: {}", jarOptions);
cl.addArguments(jarOptions);
}
final String workFolderOption = config.getProperty(PROP_WORK_FOLDER);
if (workFolderOption != null && workFolderOption.length() > 0) {
final File workFolder = new File(workFolderOption);
if (!workFolder.isDirectory()) {
throw new IOException("Work dir set by " + PROP_WORK_FOLDER + " option does not exist: " + workFolder.getAbsolutePath());
}
log.info("Setting working directory for executable jar: {}", workFolder.getAbsolutePath());
executor.setWorkingDirectory(workFolder);
}
String tmStr = config.getProperty(PROP_EXIT_TIMEOUT_SECONDS);
final int exitTimeoutSeconds = tmStr == null ? DEFAULT_EXIT_TIMEOUT : Integer.valueOf(tmStr);
if ("true".equals(config.getProperty(PROP_SYNC_EXEC, ""))) {
final long start = System.currentTimeMillis();
log.info("Executing and waiting for result: " + cl);
final int result = executor.execute(cl);
final int expected = Integer.valueOf(config.getProperty(PROP_SYNC_EXEC_EXPECTED, "0"));
log.info("Execution took " + (System.currentTimeMillis() - start) + " msec");
if (result != expected) {
throw new ExecutorException("Expected result code " + expected + ", got " + result);
}
} else {
log.info("Executing asynchronously: " + cl);
executor.setStreamHandler(new PumpStreamHandler());
final ShutdownHookSingleProcessDestroyer pd = new ShutdownHookSingleProcessDestroyer("java -jar " + jarToExecute.getName(), exitTimeoutSeconds);
final boolean waitOnShutdown = Boolean.valueOf(config.getProperty(PROP_WAIT_ONSHUTDOWN, "false"));
log.info("Setting up ProcessDestroyer with waitOnShutdown=" + waitOnShutdown);
pd.setWaitOnShutdown(waitOnShutdown);
executor.setProcessDestroyer(pd);
executor.execute(cl, h);
}
}
use of org.apache.commons.exec.ExecuteResultHandler in project sling by apache.
the class JarExecutor method start.
/** Start the jar if not done yet, and setup runtime hook
* to stop it.
*/
public void start() throws Exception {
final ExecuteResultHandler h = new ExecuteResultHandler() {
public void onProcessFailed(ExecuteException ex) {
log.error("Process execution failed:" + ex, ex);
}
public void onProcessComplete(int result) {
log.info("Process execution complete, exit code=" + result);
}
};
final String vmOptions = config.getProperty(PROP_VM_OPTIONS);
executor = new DefaultExecutor();
final CommandLine cl = new CommandLine(jvmFullPath);
if (vmOptions != null && vmOptions.length() > 0) {
cl.addArguments(vmOptions);
}
cl.addArgument("-jar");
cl.addArgument(jarToExecute.getAbsolutePath());
// Additional options for the jar that's executed.
// $JAREXEC_SERVER_PORT$ is replaced our serverPort value
String jarOptions = config.getProperty(PROP_JAR_OPTIONS);
if (jarOptions != null && jarOptions.length() > 0) {
jarOptions = jarOptions.replaceAll("\\$JAREXEC_SERVER_PORT\\$", String.valueOf(serverPort));
log.info("Executable jar options: {}", jarOptions);
cl.addArguments(jarOptions);
}
final String workFolderOption = config.getProperty(PROP_WORK_FOLDER);
if (workFolderOption != null && workFolderOption.length() > 0) {
final File workFolder = new File(workFolderOption);
if (!workFolder.isDirectory()) {
throw new IOException("Work dir set by " + PROP_WORK_FOLDER + " option does not exist: " + workFolder.getAbsolutePath());
}
log.info("Setting working directory for executable jar: {}", workFolder.getAbsolutePath());
executor.setWorkingDirectory(workFolder);
}
String tmStr = config.getProperty(PROP_EXIT_TIMEOUT_SECONDS);
final int exitTimeoutSeconds = tmStr == null ? DEFAULT_EXIT_TIMEOUT : Integer.valueOf(tmStr);
if ("true".equals(config.getProperty(PROP_SYNC_EXEC, ""))) {
final long start = System.currentTimeMillis();
log.info("Executing and waiting for result: " + cl);
final int result = executor.execute(cl);
final int expected = Integer.valueOf(config.getProperty(PROP_SYNC_EXEC_EXPECTED, "0"));
log.info("Execution took " + (System.currentTimeMillis() - start) + " msec");
if (result != expected) {
throw new ExecutorException("Expected result code " + expected + ", got " + result);
}
} else {
log.info("Executing asynchronously: " + cl);
executor.setStreamHandler(new PumpStreamHandler());
final ShutdownHookSingleProcessDestroyer pd = new ShutdownHookSingleProcessDestroyer("java -jar " + jarToExecute.getName(), exitTimeoutSeconds);
final boolean waitOnShutdown = Boolean.valueOf(config.getProperty(PROP_WAIT_ONSHUTDOWN, "false"));
log.info("Setting up ProcessDestroyer with waitOnShutdown=" + waitOnShutdown);
pd.setWaitOnShutdown(waitOnShutdown);
executor.setProcessDestroyer(pd);
executor.execute(cl, h);
}
}
Aggregations