1

After reading benchmark results of zeromq , I was wondering if I can give it a try for accessing a ReSTFul API via HTTP GET calls over zeromq. I want to test if zeromq can provide a Low-Latency Request-Reply mechanism as compared other alternatives like jersey client API, HTTPurl , Apache hc etc. I want to have this implemented in java so here is what I am trying, as per a tutorial

XREP -> HTTP client With this you could re-use existing web-applications running on a HTTP server as backend workers for a work queue. This is like a reverse HTTP proxy which load-balances across multiple backend HTTP servers.

ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket requester2 = context.socket(ZMQ.XREP);
requester2.connect("http://domainURL/path");

But this is wrong as I am getting

java.lang.UnsupportedOperationException: http

I know I haven't coded this properly but it will be helpful if any guidelines or code snippets in java are provided ?

EDIT I found a resource where a python code is calling remote web service as

import json
import zmq

# set up zmq socket
sock = zmq.Context.instance().socket(zmq.REQ)
sock.connect('ipc:///tmp/zurl-req')

# send request
req = {
  'method': 'GET',
  'uri': 'http://example.com/path'
}
sock.send('J' + json.dumps(req))

# print response
print json.loads(sock.recv()[1:])

So I am trying to modify my java code as below:

import org.zeromq.ZMQ;

public class hwclient {

    public static void main(String[] args) {

        ZMQ.Context context2 = ZMQ.context(1);
        ZMQ.Socket sock = context2.socket(ZMQ.REQ);
        sock.connect("ipc:///tmp/zurl-req");
        String req = "{\n" +
                "  'method': 'GET',\n" +
                "  'uri': 'http://domainURI/path/" +
                "}\n";
        sock.send(req);
        System.out.println("-->" + new String(sock.recv(0)));
        context2.term();
    }
}

But now the problem is I do not know what should be the value in place of "ipc:///tmp/zurl-req"

Saurabh Bhoomkar
  • 595
  • 1
  • 9
  • 29

1 Answers1

0

Zeromq does not provide high level http protocol tools.

Use tcp with STREAM socket type.

ZMQ.Context ctx = ZMQ.context(1);
ZMQ.Socket sock = ctx.socket(ZMQ.STREAM);
sock.connect("tcp://ip:port");
String req = "GET / HTTP/1.0\r\n\r\n";
sock.send(req);

something like this.

You better read the docs about native patterns: click

marsgpl
  • 552
  • 2
  • 12
  • 1
    Thanks ! But is there any way to specify HTTP Dynamic headers for GET request ? As in .header("name":"value") – Saurabh Bhoomkar Dec 01 '15 at 17:36
  • 1
    Also @marsgpl can you check if `ctx.socket(ZMQ.STREAM);` is working as I didn't find any dependency for `ZMQ.STREAM` in java. Can you provide any import statement or library name? I have following org.zeromq jeromq 0.3.5 – Saurabh Bhoomkar Dec 02 '15 at 05:44
  • @SaurabhBhoomkar, in java binding there is no STREAM type lol https://github.com/zeromq/jzmq/blob/master/src/main/java/org/zeromq/ZMQ.java – marsgpl Dec 02 '15 at 12:56
  • @SaurabhBhoomkar, ZMQ_STREAM is int 11 in zmq.h, you can try to pass it like number: ctx.socket(11); – marsgpl Dec 02 '15 at 12:58
  • 1
    @SaurabhBhoomkar, about headers: surely you can make a string from your set of headers. – marsgpl Dec 02 '15 at 12:59
  • Guess this can't be done in java , there are upstream and downstream socket types present in java though ! – Saurabh Bhoomkar Dec 02 '15 at 14:41