简体   繁体   English

Java中可靠的非网络IPC

[英]Reliable non-network IPC in Java

Is there a reliable, cross-platform way to do IPC (between two JVMs running on the same host) in Java (J2SE) that doesn't rely on the network stack? 是否有可靠的,跨平台的方式来执行Java(J2SE)中不依赖网络堆栈的IPC(在同一主机上运行的两个JVM之间)?

To be more specific, I have a server application that I'd like to provide a small "monitoring" GUI app for. 更具体地说,我有一个服务器应用程序,我想为其提供一个小型的“监视” GUI应用程序。 The monitor app would simply talk to the server process and display simple status information. Monitor应用程序将仅与服务器进程对话并显示简单的状态信息。 The server app has a web interface for most of its interaction, but sometimes things go wrong (port conflict, user forgot password) that require a local control app. 服务器应用程序具有用于大多数交互的Web界面,但有时出问题(端口冲突,用户忘记密码),需要本地控制应用程序。

In the past I've done this by having the server listen on 127.0.01 on a specific port and the client communicates that way. 过去,我是通过让服务器在特定端口上侦听127.0.01来完成此操作的,而客户端通过这种方式进行通信。 However, this isn't as reliable as I'd like. 但是,这并不像我想要的那样可靠。 Certain things can make this not work (Windows's network stack can be bizarre with VPN adapters, MediaSense, laptops lid closing/power saving modes). 某些事情可能会使它无法正常工作(Windows的网络堆栈在VPN适配器,MediaSense,笔记本电脑盖关闭/省电模式下可能很奇怪)。 You can imagine the user's confusion when the tool they use to diagnose the server doesn't even think the server is running. 您可以想象用户用于诊断服务器的工具甚至认为服务器没有运行时会感到困惑。

Named Pipes seem plausible, but Java doesn't seem to have an API for them unless I'm mistaken. 命名管道似乎是合理的,但是Java似乎没有针对它们的API,除非我弄错了。 Ideas? 有想法吗? Third party libraries that support this? 支持此功能的第三方库? My performance requirements are obviously extremely lax in case that helps. 如果有帮助,我的性能要求显然非常宽松。

One of my specialties is really low-tech solutions. 我的专长之一是真正的低技术解决方案。 Especially if your performance requirements aren't critical: 特别是在您的性能要求不是很关键的情况下:

The low-low tech alternative to named pipes is named FILES. 命名管道的技术含量较低的替代方法是FILES。 Think yourself up a protocol where one app writes a file and another reads it. 考虑一下自己的协议,其中一个应用程序写入文件,而另一个应用程序读取文件。 If need be, you can do semaphoring between them. 如果需要,您可以在它们之间进行信号量。

Remember that a rename is pretty much an atomic operation, so you could calmly write a file in some process and then make it magically appear in its entirety by renaming/moving it from somewhere that wasn't previously visible. 请记住,重命名几乎是一个原子操作,因此您可以在某个过程中冷静地写入文件,然后通过将其重命名/从以前不可见的位置移动来使其整体神奇地出现。

You can poll for data by checking for appearance of a file (in a loop with a SLEEP in it), and you can signal completion by deleting the file. 您可以通过检查文件的外观(在其中包含SLEEP的循环中)来轮询数据,也可以通过删除文件来表明已完成。

An added benefit is that you can debug your app using the DIR command :) 另一个好处是,您可以使用DIR命令来调试应用程序:)

Depending on how much data you need to pass between the server and the diagnostic tool you could: 根据需要在服务器和诊断工具之间传递多少数据,您可以:

  • go low-tech and have a background thread check a file in the file system; 技术含量低并且有后台线程检查文件系统中的文件; fetch commands from it; 从中获取命令; write ouput into a second to be picked up by the diagnostic tool. 将输出写入秒,以供诊断工具提取。
  • build a component that manages an input/output queue in shared memory connecting to it via JNI. 构建一个组件,以管理通过JNI连接到它的共享内存中的输入/输出队列。

Consider JMX. 考虑JMX。 I do not know if any of the Windows JVM's allow JMX over shared memory. 我不知道是否有任何Windows JVM允许通过共享内存进行JMX。

Does Windows even have named pipes? Windows甚至有命名管道吗? I was going to suggest it. 我打算提出建议。 You'd just have to use an exec() to create it. 您只需要使用exec()即可创建它。

Map a read_write byte buffer into memory from a FileChannel. 将一个read_write字节缓冲区从FileChannel映射到内存中。 Write status information into the byte buffer, then call force() to get it written out. 将状态信息写入字节缓冲区,然后调用force()将其写出。 On the monitor side, open up the same file and map it into memory too. 在监视器端,打开相同的文件,并将其也映射到内存中。 Poll it periodically to find out the status. 定期对其进行轮询以了解状态。

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

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