@Test public void testProxy() throws Exception { int frontendPort = Utils.findOpenPort(); int backendPort = Utils.findOpenPort(); Context ctx = ZMQ.context(1); assert (ctx != null); Main mt = new Main(ctx, frontendPort, backendPort); mt.start(); new Dealer(ctx, "AA", backendPort).start(); new Dealer(ctx, "BB", backendPort).start(); Thread.sleep(1000); Thread c1 = new Client(ctx, "X", frontendPort); c1.start(); Thread c2 = new Client(ctx, "Y", frontendPort); c2.start(); c1.join(); c2.join(); ctx.term(); }
public static void main(String[] args) { Context context = ZMQ.context(1); // First, connect our subscriber socket Socket subscriber = context.socket(ZMQ.SUB); subscriber.connect("tcp://localhost:5561"); subscriber.subscribe(ZMQ.SUBSCRIPTION_ALL); // Second, synchronize with publisher Socket syncclient = context.socket(ZMQ.REQ); syncclient.connect("tcp://localhost:5562"); // - send a synchronization request syncclient.send(ZMQ.MESSAGE_SEPARATOR, 0); // - wait for synchronization reply syncclient.recv(0); // Third, get our updates and report how many we got int update_nbr = 0; while (true) { String string = subscriber.recvStr(0); if (string.equals("END")) { break; } update_nbr++; } System.out.println("Received " + update_nbr + " updates."); subscriber.close(); syncclient.close(); context.term(); }
public static List<Socket> buildZPipe(final Context ctx) { final Socket socket1 = ctx.socket(ZMQ.PAIR); socket1.setLinger(0); socket1.setHWM(1); final Socket socket2 = ctx.socket(ZMQ.PAIR); socket2.setLinger(0); socket2.setHWM(1); final String iface = "inproc://" + new BigInteger(130, rand).toString(32); socket1.bind(iface); socket2.connect(iface); return Arrays.asList(socket1, socket2); }
public Dealer(Context ctx, String name, int port) { s = ctx.socket(ZMQ.DEALER); this.name = name; this.port = port; s.setIdentity(name.getBytes(ZMQ.CHARSET)); }
public Client(Context ctx, String name, int port) { s = ctx.socket(ZMQ.REQ); this.name = name; this.port = port; s.setIdentity(name.getBytes(ZMQ.CHARSET)); }
@Override public void run() { Socket frontend = ctx.socket(ZMQ.ROUTER); assertNotNull(frontend); frontend.bind("tcp://127.0.0.1:" + frontendPort); Socket backend = ctx.socket(ZMQ.DEALER); assertNotNull(backend); backend.bind("tcp://127.0.0.1:" + backendPort); ZMQ.proxy(frontend, backend, null); frontend.close(); backend.close(); assert true; }
public DistributedTraversal() { ImgGraph graph = ImgGraph.getInstance(); context = graph.getZMQContext(); if (context == null) System.out.println("context was null in DistributedTraversal"); // Socket to talk to server requester = context.socket(ZMQ.REQ); managerIndex = graph.getNextTraversalManagerIndex(); requester.connect("tcp://" + graph.getTraversalManagerIps()[managerIndex]); }
// We will do this all in one thread to emphasize the getSequence // of events public static void main(String[] args) { Context context = ZMQ.context(1); Socket client = context.socket(ZMQ.ROUTER); client.bind("ipc://routing.ipc"); Socket worker = context.socket(ZMQ.REP); worker.setIdentity("A".getBytes(ZMQ.CHARSET)); worker.connect("ipc://routing.ipc"); // Wait for the worker to connect so that when we send a message // with routing envelope, it will actually match the worker try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // Send papa address, address stack, empty part, and request client.send("A", ZMQ.SNDMORE); client.send("address 3", ZMQ.SNDMORE); client.send("address 2", ZMQ.SNDMORE); client.send("address 1", ZMQ.SNDMORE); client.send("", ZMQ.SNDMORE); client.send("This is the workload", 0); // Worker should get just the workload ZHelper.dump(worker); // We don't play with envelopes in the worker worker.send("This is the reply", 0); // Now dump what we got off the ROUTER socket ZHelper.dump(client); client.close(); worker.close(); context.term(); }
@Override public void run() { ZMQ.Socket socket = context.socket(ZMQ.PULL); socket.connect(this.address); while (true) { String request = socket.recvStr(0); System.out.println(Thread.currentThread().getName() + " Received request: [" + request + "]"); // Do some 'work' try { Thread.sleep(1000); } catch (InterruptedException e) { } } }
/** Destructor. Call this to gracefully terminate context and close any managed 0MQ sockets */ public void destroy() { for (Socket socket : sockets) { try { socket.setLinger(linger); } catch (ZError.CtxTerminatedException e) { } socket.close(); } sockets.clear(); // Only terminate context if we are on the main thread if (isMain() && context != null) { context.term(); } context = null; }
public void destroy() { publisher.close(); context.term(); }
public void init() { context = ZMQ.context(1); publisher = context.socket(ZMQ.PUB); publisher.bind("tcp://*:5563"); }
public static Socket socket(Context context, int type) { return context.socket(type); }