本地 JVM 之间的通信

2022-01-16 00:00:00 sockets jvm java rmi cajo

我的问题:我可以/应该采取什么方法在本地运行的两个或多个 JVM 实例之间进行通信?

My question: What approach could/should I take to communicate between two or more JVM instances that are running locally?

问题的一些描述:
我正在为一个项目开发一个系统,该项目需要单独的 JVM 实例来完全隔离某些任务.

Some description of the problem:
I am developing a system for a project that requires separate JVM instances to isolate certain tasks from each other entirely.

在它运行时,父"JVM 将创建它期望执行的子"JVM,然后将结果返回给它(以相对简单的 POJO 类的格式,或者可能是结构化的 XML 数据).不应使用 SysErr/SysOut/SysIn 管道传输这些结果,因为子进程可能已经将它们用作其运行的一部分.

In it's running, the 'parent' JVM will create 'child' JVMs that it will expect to execute and then return results to it (in the format of relatively simple POJO classes, or perhaps structured XML data). These results should not be transferred using the SysErr/SysOut/SysIn pipes as the child may already use these as part of its running.

如果子 JVM 在一定时间内没有响应结果,父 JVM 应该能够向子 JVM 发出信号以停止处理,或终止子进程.否则,子 JVM 应该在完成其任务结束时正常退出.

If a child JVM does not respond with results within a certain time, the parent JVM should be able to signal to the child to cease processing, or to kill the child process. Otherwise, the child JVM should exit normally at the end of completing its task.

迄今为止的研究:
我知道有许多技术可能有用,例如......

Research so far:
I am aware there are a number of technologies that may be of use e.g....

  • 使用 Java 的 RMI 库
  • 使用套接字传输对象
  • 使用分发库,例如 Cajo、Hessian

...但我很想听听其他人在选择其中一个或任何其他选项之前可能会考虑哪些方法.

...but am interested in hearing what approaches others may consider before pursuing one of these options, or any others.

感谢您对此的任何帮助或建议!

Thanks for any help or advice on this!

修改:
要传输的数据量-相对较少,主要是少数 POJO,其中包含表示子执行结果的字符串.如果任何解决方案在处理大量信息时效率低下,这不太可能在我的系统中成为问题.转移的金额应该是相当静态的,所以这不是必须是可扩展的.

Edits:
Quantity of data to transfer- relatively small, it will mostly be just a handful of POJOs containing strings that will represent the result of the child executing. If any solution would be inefficient on larger amounts of information, this is unlikely to be a problem in my system. The amount being transferred should be pretty static and so this does not have to be scalable.

传输延迟-在这种情况下不是一个关键问题,尽管如果需要对结果进行任何轮询",这应该能够相当频繁,而不会产生大量开销,因此我可以保持响应稍后在此之上的 GUI(例如进度条)

Latency of transfer- not a critical concern in this case, although if any 'polling' of results is needed this should be able to be fairly frequent without significant overheads, so I can maintain a responsive GUI on top of this at a later time (e.g. progress bar)

推荐答案

我会使用 KryoNet 使用本地套接字,因为它专注于序列化并且非常轻量级(您还可以获得远程方法调用!我现在正在使用它),但禁用套接字断开超时.

I'd use KryoNet with local sockets since it specialises heavily in serialisation and is quite lightweight (you also get Remote Method Invocation! I'm using it right now), but disable the socket disconnection timeout.

RMI 的工作原理基本上是你有一个远程类型并且远程类型实现了一个接口.这个接口是共享的.在您的本地机器上,您通过 RMI 库将接口绑定到从 RMI 库注入"内存中的代码,结果是您拥有满足接口但能够与远程对象通信的东西.

RMI basically works on the principle that you have a remote type and that the remote type implements an interface. This interface is shared. On your local machine, you bind the interface via the RMI library to code 'injected' in-memory from the RMI library, the result being that you have something that satisfies the interface but is able to communicate with the remote object.

相关文章