简体   繁体   English

跨Java进程共享对象

[英]Sharing objects across Java processes

I am executing another JVM (java.exe) from the main application. 我正在从主应用程序执行另一个JVM(java.exe)。 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 现在,我希望sO对象可用于proc对象表示的进程

Does ProcessBuilder provide any utilities for this purpose? ProcessBuilder是否为此提供任何实用程序?

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). 进程不能共享内存(通过JNI除外),因此您必须通过文件或通过RMI套接字连接以序列化的形式来回复制大对象(后者是更好的选择,因为它会导致固有的同步) 。

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. 使用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. 将会有IPC开销,因此它的性能将不如本地访问,细粒度的访问将变得很昂贵,但是如果您要获取摘要数据或其他汇总数据,那么这可能是一个不错的模型。

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. 可以动态加载和卸载。

No there is no shared memory support in Java. 没有Java中没有共享内存支持。

The simplest way to tackle this would be to serialize the object into a temp file and then deserialize it back in the new JVM. 解决此问题的最简单方法是将对象序列化为临时文件,然后在新的JVM中反序列化它。

我认为您可以为此目的使用分布式缓存(EHCache,memcached等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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