2

I've always used either ActiveMQ or RabbitMQ, but have started digging into ZeroMQ lately because of the attention its getting. If what I'm reading is correct, then it seems to be a message broker as well as a mechanism for interprocess communication (IPC)?

I have a situation where I have 2 Java processes that need to communicate with each other on the same machine. I don't want to use a shared file approach because the dialog/protocol between them is pretty sophisticated and a file solution seems clumsy.

So I was going to start heading down the road of using something like Netty or MINA to defines my own comm protocol, and have them converse over ports, but then I started reading about ZeroMQ and am wondering if I can accomplish the same but with less work.

So I ask: can ZeroMQ be used for IPC between the Java processes, and if so, are there any concrete code examples or articles explaining exactly how to do this?

AdjustingForInflation
  • 1,571
  • 2
  • 26
  • 50
  • 2
    `Zero` means zero brokers. ZeroMQ is for message based IPC and there are loads of examples on their website. – Peter Lawrey Mar 06 '14 at 01:42
  • 1
    Thanks @Peter Lawrey (+1) - but if you actually go to their site, you'll find: (1) plenty of examples in non-Java languages, and (2) of the Java examples, all of them are concerned with communicating between multiple threads **within the same JVM process**, not between 2 JVM processes... – AdjustingForInflation Mar 06 '14 at 01:45
  • 1
    I can't see anything in your statement that contradicts a word @PeterLawrey has told you. – user207421 Mar 06 '14 at 01:47
  • Thanks @EJP (+1) but Peter mentioned there are numerous examples of **IPC**. To me, inter-**process** communication is communication between 2+ *system* processes. I understand that ZeroMQ can help facilitate thread communication (that is, 2 threads inside the same JVM process), but what I'm really after is communication between 2 distinct Java processes (`myapp1.jar` and `myapp2.jar`, etc.). Understand the difference? – AdjustingForInflation Mar 06 '14 at 01:51
  • PeterLawrey and EJP, I'm guessing the answer is "no, ZeroMQ can't do that", judging by the *deafening* silence... – AdjustingForInflation Mar 06 '14 at 02:09
  • And the verdict is in, ZeroMQ **cannot** be used two communicate between two Java processes, despite what others are hasty to conclude. – AdjustingForInflation Mar 06 '14 at 02:34
  • 1
    The silence is a scratching of heads as to how anyone could doubt this in the first place. – Peter Lawrey Mar 06 '14 at 11:25
  • 4
    There is no evidence here that anybody doesn't understand the difference between process and threads, nor that ZeroMQ is confined to a single JVM. The conclusion-jumping here is of Olympic standard. – user207421 Mar 06 '14 at 11:48
  • Thanks @EJP (+1) - bee tee dubs, your app is down – AdjustingForInflation Mar 07 '14 at 10:51

2 Answers2

4

The first three lines of the web site tell you every thing you need to know.

Distributed Computing Made Simple

 Ø  The socket library that acts as a concurrency framework.
 Ø  Carries messages across inproc, IPC, TCP, and multicast.

I don't see any reason to suspect that this doesn't work over loopback, and it would be pretty bizzare if it couldn't.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

Yes, zmq can be used to connect two Java processes. You can use pure Java implementation JeroMq or ZeroMq with Java client. JeroMq is easier to install as you need only the appropriate dependency. Here is simple example for listener:

import org.zeromq.ZMQ;

public class Subscriber {

    public static void main(String[] a){
        final ZMQ.Context ctx = ZMQ.context(1);
        final ZMQ.Socket sub = ctx.socket(ZMQ.SUB);
//        sub.connect("tcp://localhost:6001");
        sub.connect("ipc://001");
        sub.subscribe("".getBytes());

        while (true){
            String msg = sub.recvStr();
            System.out.println(msg);
        }
    }
}

and for publisher:

import org.zeromq.ZMQ;

public class Publisher {

    public static void main(String[] a) throws InterruptedException {
        final ZMQ.Context ctx = ZMQ.context(1);
        final ZMQ.Socket pub = ctx.socket(ZMQ.PUB);
        pub.bind("ipc://001");
//        pub.bind("tcp://*:6001");

        while (true){
            System.out.println("Publishing");
            pub.send("Test");
            Thread.sleep(1000);
        }
    }
}

IPC and TCP both work.