如何让 2 个 JVM 相互通信 [英] How to have 2 JVMs talk to one another

查看:51
本文介绍了如何让 2 个 JVM 相互通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

我有 2 个 JVM 进程(实际上是 2 个 java 进程分别运行,而不是 2 个线程)在本地机器上运行.让我们称它们为 ProcessAProcessB.

I have 2 JVM processes (really 2 java processes running separately, not 2 threads) running on a local machine. Let's call them ProcessA an ProcessB.

我希望它们相互通信(交换数据)(例如,ProcessAProcessB 发送消息以执行某些操作).

I want them to communicate (exchange data) with one another (e.g. ProcessA sends a message to ProcessB to do something).

现在,我通过编写一个临时文件来解决此问题,这些进程会定期扫描此文件以获取消息.我觉得这个解决方案不太好.

Now, I work around this issue by writing a temporary file and these process periodically scan this file to get message. I think this solution is not so good.

什么是实现我想要的更好的选择?

What would be a better alternative to achieve what I want?

推荐答案

IPC<的多种选择/a>:

Multiple options for IPC:

  • 不一定难,但是:
    • 可能有点冗长,
    • 随着您编写更多代码,可能会为错误提供更多空间.
    • 从技术上讲,这也是网络通信,但对您来说是透明的.
    • 通常也基于 RMI 或网络通信构建,但支持复杂的对话和工作流程
    • 对于简单的事情来说可能太重了
    • ActiveMQJBoss 消息传递
    • 更多用于JVM 管理和监控,但如果您主要希望一个进程查询另一个进程的数据,或者向其发送一些操作请求(如果它们不太复杂),则可以帮助实现您想要的内容
    • 也适用于 RMI(以及其他可能的协议)
    • 一开始不是那么简单,但是实际上相当简单使用
    • more meant for JVM management and monitoring, but could help to implement what you want if you mostly want to have one process query another for data, or send it some request for an action, if they aren't too complex
    • also works over RMI (amongst other possible protocols)
    • not so simple to wrap your head around at first, but actually rather simple to use
    • 这就是你现在正在做的
    • 这是可行的,但有很多问题需要处理
    • 您可以简单地向其他项目发送信号
    • 但是,它相当有限,并且需要您实现一个翻译层(尽管它是可行的,但比起任何严肃的事情来说,这是一个相当疯狂的想法.
    • You can simply send signals to your other project
    • However, it's fairly limited and requires you to implement a translation layer (it is doable, though, but a rather crazy idea to toy with than anything serious.

    没有更多细节,基于网络的基本 IPC 方法似乎是最好的,因为它是:

    Without more details, a bare-bone network-based IPC approach seems the best, as it's the:

    • 最具可扩展性(在向您的系统中添加新功能和工作流程方面
    • 最轻量级(就您的应用的内存占用而言)
    • 最简单(在设计方面)
    • 最具教育意义(在学习如何实施 IPC 方面).(正如你在评论中提到的socket 很难",它确实不是,应该是你工作的东西)

    话虽如此,根据您的示例(只是请求另一个进程执行操作),JMX 也可能对您来说已经足够了.

    That being said, based on your example (simply requesting the other process to do an action), JMX could also be good enough for you.

    这篇关于如何让 2 个 JVM 相互通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆