Esempio n. 1
0
  @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();
  }
Esempio n. 2
0
  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();
  }
  /** 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;
  }
Esempio n. 4
0
  //  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();
  }
 public void destroy() {
   publisher.close();
   context.term();
 }