Skip to content

Instantly share code, notes, and snippets.

@davipt davipt/gist:7361623
Created Nov 7, 2013

Embed
What would you like to do?
zmq proxy java sample
package test;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;
public class Proxy {
public static void main(String[] args) throws InterruptedException {
if (args.length == 0)
args = new String[] {"tcp://*:8881", "tcp://*:8882", "1"};
// Simple message queuing broker
// Same as request-reply broker but using QUEUE device
final int t = args.length > 2 ? Integer.parseInt(args[2]) : 0;
if (t == 1)
System.out.println(String.format("Proxy type=PULL/PUSH in=%s out=%s\n", args[0], args[1]));
else if (t == 2)
System.out.println(String.format("Proxy type=ROUTER/DEALER in=%s out=%s\n", args[0], args[1]));
else
System.out.println(String.format("Proxy type=XSUB/XPUB in=%s out=%s\n", args[0], args[1]));
final Context ctx = ZMQ.context(1);
// Socket facing clients
final Socket frontend = ctx.socket(t == 1 ? ZMQ.PULL : t == 2 ? ZMQ.ROUTER : ZMQ.XSUB);
int rc = frontend.bind(args[0]);
assert ( rc == 0 );
// Socket facing services
final Socket backend = ctx.socket(t == 1 ? ZMQ.PUSH : t == 2 ? ZMQ.DEALER : ZMQ.XPUB);
rc = backend.bind(args[1]);
assert ( rc == 0 );
// Start the proxy
ZMQ.proxy(frontend, backend, null);
// We never get here...
frontend.close();
backend.close();
ctx.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.