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 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 Summary

Fields
`static String` `PID_MSG_PREFIX` Internal protocol message prefix saying that the next text in the outputted line is pid.

### Method Summary

Methods
`static GridJavaProcess` `exec(Class cls, String params, IgniteLogger log, IgniteInClosure<String> printC, GridAbsClosure procKilledC)` Executes main() method of the given class in a separate system process.
`static GridJavaProcess` `exec(Class cls, String params, IgniteLogger log, IgniteInClosure<String> printC, GridAbsClosure procKilledC, Collection<String> jvmArgs, String cp)` Executes main() method of the given class in a separate system process.
`static GridJavaProcess` `exec(String clsName, String params, IgniteLogger log, IgniteInClosure<String> printC, GridAbsClosure procKilledC, String javaHome, Collection<String> jvmArgs, String cp)` Executes main() method of the given class in a separate system process.
`int` `getPid()` Returns pid of the java process.
`Process` `getProcess()` Exposes wrapped java Process.
`void` `kill()` Kills the java process.
`void` `killProcess()` Kills process using `Process.destroy()`.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### 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
                       IgniteLogger log,
                       @Nullable
                       IgniteInClosure<String> printC,
                       @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
                       IgniteLogger log,
                       @Nullable
                       IgniteInClosure<String> printC,
                       @Nullable
                       GridAbsClosure procKilledC,
                       @Nullable
                       Collection<String> jvmArgs,
                       @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
                       IgniteLogger log,
                       @Nullable
                       IgniteInClosure<String> printC,
                       @Nullable
                       GridAbsClosure procKilledC,
                       @Nullable
                       String javaHome,
                       @Nullable
                       Collection<String> jvmArgs,
                       @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.

#### kill

    public void kill()
              throws Exception

Kills the java process.

Throws:
  `Exception` - If any problem occurred.

#### killProcess

    public void killProcess()

Kills process using `Process.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.

2017 Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Fabric | : ver. 1.9.0
Release Date | : March 2 2017