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

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

问题描述

我有以下情况:



我有两个JVM进程(真的2 java 不是2个线程)在本地机器上运行。让我们把它们叫做 ProcessA a ProcessB



它们彼此通信(交换数据)(例如 ProcessA 发送消息到 ProcessB 来做某事) / p>

现在,我解决这个问题,通过写一个临时文件,这些进程定期扫描这个文件来获得消息。我认为这个解决方案不是那么好。



这将是一个更好的替代实现我想要什么。

解决方案

IPC 的多个选项:



Socket-Based(Bare-Bones)Networking




  • 不一定困难,但是:


    • 可能是冗长的,


  • 可能会为错误提供更多表面。依赖于现有框架,例如 Netty



< a href =http://en.wikipedia.org/wiki/Java_remote_method_invocation> RMI




  • 从技术上讲,



完全成熟的讯息传递架构





Java管理扩展(JMX)




  • 更适用于 JVM管理和监控,但可以帮助实现你想要的,如果你想要一个进程查询另一个数据,或发送它

  • 也适用于RMI(以及其他可能的协议)

  • 不是那么简单你的头一开始,但实际上很容易使用



文件共享/文件锁定





  • 这是可行的,但是处理很多问题



/ h2>


  • 您可以简单地向其他项目发送信号

  • 然而,实施翻译层( 是可行的),但是一个比任何严肃的玩具都更疯狂的想法。



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




  • 最具扩展性(在向您的

  • 添加新功能和工作流程方面)最轻便(就应用程序的内存占用而言)

  • 最简单(在设计方面)

  • 最具教育性(在学习如何实现IPC方面)。

    b $ b

    话虽如此,基于你的例子(简单地请求其他进程做一个动作),JMX也可以为你足够好。


    I have the following situation:

    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.

    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?

    解决方案

    Multiple options for IPC:

    Socket-Based (Bare-Bones) Networking

    • not necessarily hard, but:
      • might be verbose for not much,
      • might offer more surface for bugs, as you write more code.
    • you could rely on existing frameworks, like Netty

    RMI

    • Technically, that's also network communication, but that's transparent for you.

    Fully-fledged Message Passing Architectures

    • usually built on either RMI or network communications as well, but with support for complicated conversations and workflows
    • might be too heavy-weight for something simple
    • frameworks like ActiveMQ or JBoss Messaging

    Java Management Extensions (JMX)

    • 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

    File-sharing / File-locking

    • that's what you're doing right now
    • it's doable, but comes with a lot of problems to handle

    Signals

    • 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.

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

    • most extensible (in terms of adding new features and workflows to your
    • most lightweight (in terms of memory footprint for your app)
    • most simple (in terms of design)
    • most educative (in terms of learning how to implement IPC). (as you mentioned "socket is hard" in a comment, and it really is not and should be something you work on)

    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天全站免登陆