Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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

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.

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.

The javazmq project started in Aug of 2011 claims to implement a subset of ZeroMQ in pure Java. Not sure of the completeness of it at this point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top