1

I have written a request-reply broker using zeromq and the C programming language. The broker routes client requests to the appropriate services, and then routes the reply back to the client. The services are written in JAVA.

Can someone please explain how to have the services communicate with the broker. I am sure that this must be a common scenario, but I don't have much experience, so can someone please help me with making my code inter-operable.

Please assume that the services will not be zeromq aware. Is node.js to be used in such a scenario? Will I have to write an http front end?

user1274878
  • 1,275
  • 4
  • 25
  • 56
  • The services are Java, but are they ZeroMq clients as well? Can they consume messages from the C-based ZeroMq broker? – raffian Oct 08 '13 at 17:24
  • Clients and services will be different entities. They haven't been written yet, but they will be( in JAVA). For testing, I wrote simple clients and services in C itself. My point is that I don't want the clients and services to know about the msg formats that the broker understands. They should communicate with the broker using JSON maybe. – user1274878 Oct 08 '13 at 18:22
  • Understood, but will the Java services be ZeroMq clients? – raffian Oct 08 '13 at 18:29
  • I am not sure, but I think that the requirement is that they should not have to worry about zeromq. If that is not feasible, what would be the solution assuming that the JAVA services will use zeromq? – user1274878 Oct 08 '13 at 18:37
  • If the Java services are ZeroMq-aware, the solution is easy since ZeroMq has language bindings for C++ and Java. You can use json as the message payload in between. Will the Java services reside inside the C++ app, or in their own standalone process? – raffian Oct 08 '13 at 19:01

1 Answers1

0

Here's one way you can do it using async PUSH/PULL sockets. I'm psuedo-coding this, so fill in the blanks yourself:

Assuming the Java services are POJO's residing in their own process, let's say we have a simple service with no zmq dependencies:

public class MyJavaService{
   public Object invokeService(String params){
   }
}

Now we build a Java delegate layer that pulls in messages from the broker, delegating requests to the Java service methods, and returning the response on a separate socket:

//receive on this
Socket pull = ctx.createSocket(ZMQ.PULL)
pull.connect("tcp://localhost:5555")

//respond on this
Socket push = ctx.createSocket( ZMQ.PUSH)
psuch.connect("tcp://localhost:5556")

while( true){
  ZMsg msg = pull.recvMsg( pull)

  //assume the msg has 2 frames,
  //one for service to invoke,
  //the other with arguments
  String svcToInvoke = msg.popString()
  String svcArgs = msg.popString()

  if( "MyJavaService".equals(svcToInvoke)){
    ZMsg respMsg = new ZMsg()
    respMsg.push( (new MyJavaService()).invokeService( svcArgs))
    respMsg.send( push)
  }
 }

On the broker side, just create the PUSH/PULL sockets to communicate with the Java services layer (I'm not a C++ programmer, so forgive me)

int main () {
    zmq::context_t context(1);

    zmq::socket_t push(context, ZMQ_PUSH);
    push.bind( "tcp://localhost:5555");

    // First allow 0MQ to set the identity
    zmq::socket_t pull(context, ZMQ_PULL);
    pull.bind( "tcp://localhost:5556");

    //code here to handle request/response,
    //to clients
    ..
}

Using PUSH/PULL works for this approach, but the ideal approach is to use ROUTER on the server, and DEALER on the client, for full asynchronous communication, example here.

Hope it helps!

raffian
  • 31,267
  • 26
  • 103
  • 174