Package org.apache.ignite.internal.util
Class GridJavaProcess
- java.lang.Object
-
- org.apache.ignite.internal.util.GridJavaProcess
-
public final class GridJavaProcess extends Object
Wrapper class around theProcesssuited to run any Java class as separate java process.This launcher supports simple interchange-with-a-process protocol to talk (in fact, listen) to process. For the moment the only message in protocol is run process PID. Class-to-run should print it's PID prefixed with
#PID_MSG_PREFIXto tell the GridJavaProcess it's PID.Protocol transport is any of (or both) system.out and/or system.err, so any protocol message should be printed in the class-to-run.
NOTE 1: For the moment inner class running is not supported.
NOTE 2: This util class should work fine on Linux, Mac OS and Windows.
-
-
Field Summary
Fields Modifier and Type Field Description static StringPID_MSG_PREFIXInternal protocol message prefix saying that the next text in the outputted line is pid.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static GridJavaProcessexec(Class cls, String params, @Nullable IgniteLogger log, @Nullable IgniteInClosure<String> printC, @Nullable GridAbsClosure procKilledC)Executes main() method of the given class in a separate system process.static GridJavaProcessexec(Class cls, String params, @Nullable IgniteLogger log, @Nullable IgniteInClosure<String> printC, @Nullable GridAbsClosure procKilledC, @Nullable Collection<String> jvmArgs, @Nullable String cp)Executes main() method of the given class in a separate system process.static GridJavaProcessexec(String clsName, String params, @Nullable IgniteLogger log, @Nullable IgniteInClosure<String> printC, @Nullable GridAbsClosure procKilledC, @Nullable String javaHome, @Nullable Collection<String> jvmArgs, @Nullable String cp)Executes main() method of the given class in a separate system process.intgetPid()Returns pid of the java process.ProcessgetProcess()Exposes wrapped java Process.voidkill()Kills the java process.voidkillProcess()Kills process usingProcess.destroy().static StringresolveJavaBin(@Nullable String javaHome)Resolves path to java binary (that can be executed using exec).voidsuspend()Suspends the process.
-
-
-
Field Detail
-
PID_MSG_PREFIX
public static final String PID_MSG_PREFIX
Internal protocol message prefix saying that the next text in the outputted line is pid.- See Also:
- Constant Field Values
-
-
Method Detail
-
exec
public static GridJavaProcess exec(Class cls, String params, @Nullable @Nullable IgniteLogger log, @Nullable @Nullable IgniteInClosure<String> printC, @Nullable @Nullable GridAbsClosure procKilledC) throws Exception
Executes main() method of the given class in a separate system process.- Parameters:
cls- Class with main() method to be run.params- main() method parameters.printC- Optional closure to be called each time wrapped process prints line to system.out or system.err.procKilledC- Optional closure to be called when process termination is detected.log- Log to use.- Returns:
- Wrapper around
Process - Throws:
Exception- If any problem occurred.
-
exec
public static GridJavaProcess exec(Class cls, String params, @Nullable @Nullable IgniteLogger log, @Nullable @Nullable IgniteInClosure<String> printC, @Nullable @Nullable GridAbsClosure procKilledC, @Nullable @Nullable Collection<String> jvmArgs, @Nullable @Nullable String cp) throws Exception
Executes main() method of the given class in a separate system process.- Parameters:
cls- Class with main() method to be run.params- main() method parameters.printC- Optional closure to be called each time wrapped process prints line to system.out or system.err.procKilledC- Optional closure to be called when process termination is detected.log- Log to use.jvmArgs- JVM arguments to use.cp- Additional classpath.- Returns:
- Wrapper around
Process - Throws:
Exception- If any problem occurred.
-
exec
public static GridJavaProcess exec(String clsName, String params, @Nullable @Nullable IgniteLogger log, @Nullable @Nullable IgniteInClosure<String> printC, @Nullable @Nullable GridAbsClosure procKilledC, @Nullable @Nullable String javaHome, @Nullable @Nullable Collection<String> jvmArgs, @Nullable @Nullable String cp) throws Exception
Executes main() method of the given class in a separate system process.- Parameters:
clsName- Class with main() method to be run.params- main() method parameters.log- Log to use.printC- Optional closure to be called each time wrapped process prints line to system.out or system.err.procKilledC- Optional closure to be called when process termination is detected.javaHome- Java home location. The process will be started under given JVM.jvmArgs- JVM arguments to use.cp- Additional classpath.- Returns:
- Wrapper around
Process - Throws:
Exception- If any problem occurred.
-
resolveJavaBin
public static String resolveJavaBin(@Nullable @Nullable String javaHome)
Resolves path to java binary (that can be executed using exec). Either the provided java home directory is used, or, if it'snull, the java.home system property is consulted with.- Parameters:
javaHome- Java home directory where to look for bin/java; ifnull, then java.home property value is used.- Returns:
- Path to Java executable.
-
kill
public void kill() throws ExceptionKills the java process.- Throws:
Exception- If any problem occurred.
-
killProcess
public void killProcess()
Kills process usingProcess.destroy().
-
getPid
public int getPid()
Returns pid of the java process. Wrapped java class should print it's PID to system.out or system.err to make wrapper know about it.- Returns:
- Pid of the java process or -1 if pid is unknown.
-
getProcess
public Process getProcess()
Exposes wrapped java Process.- Returns:
- Wrapped java process.
-
-