I am trying to use ZeroMQ's pub sub messaging, but the client side requires the code to be all Java. I understand that ZeroMQ has a Java binding but that still relies on a c library, therefore I am unable to use it. Is there a ZeroMQ client I can use to connect to the server, or is the implementation simple enough for me to do myself?
-
Did you ever use RabbitMQ? It has pure Java client. I never tried ZeroMQ but i think you will have a pretty class to correspond with ZeroMQ: http://github.com/zeromq/jzmq/blob/master/src/org/zeromq/ZMQ.java – Xupypr MV Aug 15 '10 at 10:33
-
Are you mandated for any reason in using ZeroMQ ? I had never heard of it, not that that implies it is bad. There are soooo many other options around that are better known and will provide what you ask for. – Romain Hippeau Aug 15 '10 at 14:17
-
Well I need a fast, lightweight messaging, and thought that ZeroMQ was a good fit. However, I might try something else if necessary. – fanar Aug 15 '10 at 21:02
-
I have the same question. ZeroMQ looks like exactly what I want on the server side, but I'm wanting to use Android for the client. Those desires leave me asking the same question as fanar. – Unoti Sep 06 '10 at 22:37
-
Any reason not to choose one of the answers? (I didn't write a definite "no", as someone may have written a Java-only library by looking at the wire protocol but if this is the case it hasn't been publicized) – Samuel Tardieu Jan 06 '11 at 20:22
-
I would be interested in this as well, and a Crossroads-IO client would be similarly acceptable. – Michael Ekstrand Jun 30 '12 at 16:21
7 Answers
I'm working on pure java ZeroMQ.
https://github.com/miniway/jeromq
Even it is very early stage and might not quite fit for production usage right now.
But I hope it would help, as I made it with a passion.

- 141
- 1
- 2
-
1This library is now also hosted at ZeroMQ's github profile. https://github.com/zeromq/jeromq . And the ZeroMQ wiki suggest this library over using the NDK solution. – driax Jul 23 '13 at 11:47
-
Min: Just wanted to say thanks for porting it to pure java. I have been using jeromq version 0.2.0 (will be upgrading to 0.3.1 soon) in prod for about 5 months now. Pub/Sub, Dealer/Router and a custom majordomo pattern, works like a charm! – CaptainHastings Nov 06 '13 at 22:28
-
If I had an old zeroMQ PUSH, would a jeromq PULL client be able to pick that up (given socket number etc.) ? – crogg01 Feb 08 '15 at 13:34
The zeromq web site claims support for android on the follow page:
http://www.zeromq.org/distro:android
which includes a tarball of zeromq pre-built for android, using the NDK (Native Development Kit) which bridges Java apps to standard c libraries.

- 457
- 5
- 6
-
A quick correction to this: it requires an *unofficial* NDK, not the one that comes from Google. – David Given Mar 16 '11 at 23:49
As of today, I think it is safe to assume that the answer to your question is no.
A pure Java client would need to use the same wire protocol as the native C++ implementation; documenting this protocol has not been done yet and was on the 0MQ developers TODO list last time I checked.

- 2,071
- 1
- 14
- 17
Note : I am assuming that you have installed ZeroMQ and google protoc successfully
I have one example where I am sending some messages from ZeroMQ Publisher to Subscriber using google protocol buffer..
test.proto file is
option java_package = "com.example.tutorial";
option java_outer_classname = "TestProtos";
message Test {
required string name = 1;
required int32 id = 2;
optional string email = 3;
}
now first compile it with protoc compiler as
$protoc -I=. --java_out=. test.proto
So this will generate TestProtos.java
file in your current directories
/com/example/tutorial
folder
---------------------------------end of protocol buffer steps--------------------------
Publisher code is file name : Publisher.java
import org.zeromq.ZMQ;
import com.example.tutorial.TestProtos.Test;
public class Publisher {
public static void main (String[] args) {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket publisher = context.socket(ZMQ.PUB);
// Subscriber tells us when it's ready here
ZMQ.Socket sync = context.socket(ZMQ.PULL);
sync.bind("tcp://*:5561");
// We send updates via this socket
publisher.bind("tcp://*:5562");
System.out.println("Publisher Running");
// Wait for synchronization request
sync.recv(0);
Test testzmq =
Test.newBuilder()
.setId(1234)
.setName("Pritam Kharat")
.setEmail("pritam@gmail.com")
.build();
long start = System.currentTimeMillis ();
int request_nbr;
for (request_nbr = 0; request_nbr != 10; request_nbr++) {
//System.out.println(request_nbr);
publisher.send(testzmq.toByteArray(), 0); //serialization
}
long end = System.currentTimeMillis ();
long diff = (end - start);
System.out.println("time taken to send messages "+ request_nbr +" is :" +diff);
}
}
Subscriber Code is file name : Subscriber.java
import org.zeromq.ZMQ;
import com.example.tutorial.TestProtos.Test;
public class Subscriber {
public static void main (String[] args) {
ZMQ.Context context = ZMQ.context(1);
// Connect our subscriber socket
ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
subscriber.setIdentity("hello".getBytes());
// Synchronize with the publisher
ZMQ.Socket sync = context.socket(ZMQ.PUSH);
subscriber.subscribe("".getBytes());
subscriber.connect("tcp://localhost:5562");
sync.connect("tcp://localhost:5561");
sync.send("".getBytes(), 0);
long start = System.currentTimeMillis ();
int request_nbr;
for (request_nbr = 0; request_nbr != 10; request_nbr++) {
byte[] rawBytes = subscriber.recv(0);
try{
Test data = Test.parseFrom(rawBytes); //deserialization
// System.out.println(data);
}
catch ( Exception e ) {
}
}
long end = System.currentTimeMillis ();
long diff = (end - start);
System.out.println("time taken to receive messages "+ request_nbr +" is :" +diff);
}
}
That is all..you are done with your code.. Now just run these Publisher and subscriber codes..

- 39
- 3
There is currently no pure Java implementation of 0mq
. As you say, there is a Java language binding but it requires a native library to function. A pure Java replacement for the library would not be trivial to implement.

- 5,247
- 6
- 30
- 38
There is a short spec on the wire protocol used by ZeroMQ at http://rfc.zeromq.org/spec:13. With that you should at least be able to write the protocol portions of the code. You would still need to write the parts that deal with ZeroMQ sockets and whatnot.

- 131
- 3