Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have 2 JVMs talk to one another

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?

like image 717
tnk_peka Avatar asked Jun 08 '12 02:06

tnk_peka


People also ask

How to communicate between two JVMs?

One java app can run the other via Runtime. exec() and control it (communicate) using the input stream. One could use JNI and a OS specific feature like named pipes or shared memory. You can also use java RMI to communicate between two applications, one calling methods of the other.

How process communicate with each other in Java?

The client program connects to the server with a socket. Once the socket connection is established, either program can send or receive information. Another option is to use stream files for communication between programs. To do this, use the System.in, System.


4 Answers

I've added a library on github called Mappedbus (http://github.com/caplogic/mappedbus) which enable two (or many more) Java processes/JVMs to communicate by exchanging messages. The library uses a memory mapped file and makes use of fetch-and-add and volatile read/writes to synchronize the different readers and writers. I've measured the throughput between two processes using this library to 40 million messages/s with an average latency of 25 ns for reading/writing a single message.

like image 34
MikaelJ Avatar answered Oct 12 '22 08:10

MikaelJ


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.

like image 69
haylem Avatar answered Oct 12 '22 08:10

haylem


What you are looking for is inter-process communication. Java provides a simple IPC framework in the form of Java RMI API. There are several other mechanisms for inter-process communication such as pipes, sockets, message queues (these are all concepts, obviously, so there are frameworks that implement these).

I think in your case Java RMI or a simple custom socket implementation should suffice.

like image 7
Strelok Avatar answered Oct 12 '22 08:10

Strelok


Sockets with DataInput(Output)Stream, to send java objects back and forth. This is easier than using disk file, and much easier than Netty.

like image 3
Alexei Kaigorodov Avatar answered Oct 12 '22 10:10

Alexei Kaigorodov