Class GridJavaProcess


  • public final class GridJavaProcess
    extends Object
    Wrapper class around the Process suited 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_PREFIX to 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 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's null, the java.home system property is consulted with.
        Parameters:
        javaHome - Java home directory where to look for bin/java; if null, then java.home property value is used.
        Returns:
        Path to Java executable.
      • kill

        public void kill()
                  throws Exception
        Kills the java process.
        Throws:
        Exception - If any problem occurred.
      • 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.
      • suspend

        public void suspend()
                     throws Exception
        Suspends the process.
        Throws:
        Exception