简体   繁体   中英

Is it possible to share variable between processes in Java with Process Builder

Original message

I'm currently working on several executable Jars being called by another Java process (or "Launcher) called by a scheduler.

To do this, we use ProcessBuilder . However, we need to be able to share variable between the parent process and the child process (The executable JAR).

I know it's possible to pass variable to the child process with the environment() method.

What we need is to be able to share informations from the child process to the parent process (the process results, mostly files). Is it possible? If it is, how so?

Update The two java processes are on the same computer and so share the same disk space. The Launcher process is called by a scheduler and this process cannot be changed.

I know that using a file is probably the best solution but I was wondering if there was no other solution.

Thanks in advance for your help.

A process is a sort of black box with which you can dialogue only via standard streams (input, output, error) or retrieving exit code of the process. All other systems needs an external way to communicate.

So you can:

  • Use the method exitValue() of the Process as a form of communication between child and parent.

  • Intercept the output of the child process using the getInputStream() method.

  • Share data with external resources (files, databases or opening communication sockets for example).

You need bidirectional communication between processes . You can choose from three alternatives:

  1. Asynchronous low level: Use a pre-stablished disk file for each communication: One file to write data from each child process to the parent process, and another to write data from the parent process to the child process (if needed).

  2. Synchronous generic server/client level: On every process that must receive data (either the parent process or either each child process), open a ServerSocket and accept requests according to your own protocol (typically this must be done on a secondary thread).

  3. Synchronous specific Java RMI level: This is the most advanced, flexible and usable alternative you can get: Design and implement an RMI interface and deploy it as a server on each process that must receive data, and also deploy it as client on each process that must send data.

What you need to do is not to execute your process directly, but rather create a (batch) script which executes the command and after this it writes the value of the variable to a file. Then make the ProcessBuilder execute the (batch) script. Then, before calling another process, read the file, and pass the variable value to the ProcessBuilder using the environment() method you mention.

So the (batch) script looks as follows:

<your command>
echo $variable > file.txt

And then you execute shell with the script filename as an argument:

bash batchscript.sh

In windows, it works the same, except you want to execute

cmd /c batchscript.cmd

There was a product called Terracotta that allowed to share object across multiple JVM instances. The onwer company still have open source versions of their products but I don't know them anymore.

If you want to use RMI as suggested by Little Santi you may use Spring-remoting to make it easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM