Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing objects across Java processes

I am executing another JVM (java.exe) from the main application. Is there any way to share an object (rather large object) with the newly created process (at the time of creation or after it was created).

someObject sO= new someObject();

//sO is populated

//Creating new process

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("java  -cp " + tempDir +  jarsInPath  + " " + appMain);

Now I want the sO object to be available to the process denoted by the proc object

Does ProcessBuilder provide any utilities for this purpose?

like image 301
user174819 Avatar asked Sep 17 '09 08:09

user174819


2 Answers

If you want to share objects, the best way is to use threads instead of a separate process. Processes cannot share memory (except through JNI), so you'd have to copy the large object back and forth in serialized form, either via files or via RMI socket connection (with the latter being the better option since it results in inherent synchronization).

like image 156
Michael Borgwardt Avatar answered Nov 13 '22 04:11

Michael Borgwardt


You can expose a service to allow access to the data from the object. It's comparatively simple to set up inter-process communication using RMI. There's going to be an IPC overhead so this will not be as performant as local access, fine-grained access will get expensive, but if you're getting summary or other agregated data then this could be a decent model.

You don't say why these are separate processes. Do you have any opportunity to load the code of you child process directly into the parent? Dynamic loading and unloading is possible.

like image 25
djna Avatar answered Nov 13 '22 05:11

djna