Package org.zeromq

Examples of org.zeromq.ZMsg


  protected synchronized void flush() {
    doFlush(null);
  }

  private void doFlush(final Promise<Void> onComplete) {
    ZMsg msg = MSG_UPD.get(ZeroMQNetChannel.this);
    MSG_UPD.compareAndSet(ZeroMQNetChannel.this, msg, null);
    if (null != msg) {
      boolean success = msg.send(socket);
      if (null != onComplete) {
        if (success) {
          onComplete.onNext((Void) null);
        } else {
          onComplete.onError(new RuntimeException("ZeroMQ Message could not be sent"));
View Full Code Here


      byte[] data = new byte[128];
      random.nextBytes(data);

      socket.send(data);

      ZMsg reply = ZMsg.recvMsg(socket);
      log.info("reply: {}", reply);
      latch.countDown();

      //zmq.destroySocket(socket);
    }
View Full Code Here

     * @param command
     * @param option
     * @param msg
     */
    void sendToBroker(MDP command, String option, ZMsg msg) {
        msg = msg != null ? msg.duplicate() : new ZMsg();

        // Stack protocol envelope to start of message
        if (option != null)
            msg.addFirst(new ZFrame(option));

View Full Code Here

            items.register(worker, ZMQ.Poller.POLLIN);
            if (items.poll(timeout * 1000) == -1)
                break; // Interrupted

            if (items.pollin(0)) {
                ZMsg msg = ZMsg.recvMsg(worker);
                if (msg == null)
                    break; // Interrupted
                if (verbose) {
                    log.format("I: received message from broker: \n");
                    msg.dump(log.out());
                }
                liveness = HEARTBEAT_LIVENESS;
                // Don't try to handle errors, just assert noisily
                assert (msg != null && msg.size() >= 3);

                ZFrame empty = msg.pop();
                assert (empty.getData().length == 0);
                empty.destroy();

                ZFrame header = msg.pop();
                assert (MDP.W_WORKER.frameEquals(header));
                header.destroy();

                ZFrame command = msg.pop();
                if (MDP.W_REQUEST.frameEquals(command)) {
                    // We should pop and save as many addresses as there are
                    // up to a null part, but for now, just save one...
                    replyTo = msg.unwrap();
                    command.destroy();
                    return msg; // We have a request to process
                } else if (MDP.W_HEARTBEAT.frameEquals(command)) {
                    // Do nothing for heartbeats
                } else if (MDP.W_DISCONNECT.frameEquals(command)) {
                    reconnectToBroker();
                } else {
                    log.format("E: invalid input message: \n");
                    msg.dump(log.out());
                }
                command.destroy();
                msg.destroy();
            } else if (--liveness == 0) {
                if (verbose)
                    log.format("W: disconnected from broker - retrying...\n");
                try {
                    Thread.sleep(reconnect);
View Full Code Here

                items.register(frontend, ZMQ.Poller.POLLIN);
                items.register(backend, ZMQ.Poller.POLLIN);
                if (items.poll() == -1)
                    break; // Interrupted
                if (items.pollin(0)) {
                    ZMsg msg = ZMsg.recvMsg(frontend);
                    if (msg == null)
                        break; // Interrupted
                    ZFrame address = msg.pop();
                    address.destroy();
                    msg.addFirst(new ZFrame("W"));
                    msg.send(backend);
                }
                if (items.pollin(1)) {
                    ZMsg msg = ZMsg.recvMsg(backend);
                    if (msg == null)
                        break; // Interrupted
                    ZFrame address = msg.pop();
                    address.destroy();
                    msg.addFirst(new ZFrame("C"));
                    msg.send(frontend);
                }
            }
            ctx.destroy();
        }
View Full Code Here

            ZContext ctx = new ZContext();
            Socket worker = ctx.createSocket(ZMQ.DEALER);
            worker.setIdentity("W".getBytes());
            worker.connect("tcp://localhost:5556");
            while (!Thread.currentThread().isInterrupted()) {
                ZMsg msg = ZMsg.recvMsg(worker);
                msg.send(worker);
            }

            ctx.destroy();

        }
View Full Code Here

            System.out.println("Synchronous round-trip test...");
            start = System.currentTimeMillis();

            for (requests = 0; requests < SAMPLE_SIZE; requests++) {
                ZMsg req = new ZMsg();
                req.addString("hello");
                req.send(client);
                ZMsg.recvMsg(client).destroy();
            }

            System.out.printf(" %d calls/second\n",
                    (1000 * SAMPLE_SIZE) / (System.currentTimeMillis() - start));

            System.out.println("Asynchronous round-trip test...");
            start = System.currentTimeMillis();

            for (requests = 0; requests < SAMPLE_SIZE; requests++) {
                ZMsg req = new ZMsg();
                req.addString("hello");
                req.send(client);
            }
            for (requests = 0; requests < SAMPLE_SIZE
                    && !Thread.currentThread().isInterrupted(); requests++) {
                ZMsg.recvMsg(client).destroy();
            }
View Full Code Here

            if (items.poll(HEARTBEAT_INTERVAL * 1000) == -1)
                break; // Interrupted

            if (items.pollin(0)) {
                ZMsg msg = ZMsg.recvMsg(worker);
                if (msg == null)
                    break; // Interrupted

                if (msg.size() == 3) { // serving a client request
                    if (!doTheWork(cycles++))
                        break; // crashed
                    liveness = HEARTBEAT_LIVENESS;
                    msg.send(worker);
                } else if (msg.size() == 1) { // heartbeat
                    ZFrame frame = msg.getFirst();
                    if (Arrays.equals(frame.getData(), PPP_HEARTBEAT)) {
                        liveness = HEARTBEAT_LIVENESS;
                    } else {
                        System.out.printf("E: invalid message (%s)\n",
                                frame.toString());
                    }
                    frame.destroy();
                } else {
                    System.out.printf("E: invalid message (%s)\n",
                            msg.toString());
                }
                interval = INTERVAL_INIT;
            } else if (--liveness == 0) {
                System.out.printf("W: heartbeat failure, can't reach queue\n");
                System.out.printf("W: reconnecting in %d msec...\n", interval);
View Full Code Here

        boolean verbose = (args.length > 0 && "-v".equals(args[0]));
        mdcliapi clientSession = new mdcliapi("tcp://localhost:5555", verbose);

        int count;
        for (count = 0; count < 100000; count++) {
            ZMsg request = new ZMsg();
            request.addString("Hello world");
            ZMsg reply = clientSession.send("echo", request);
            if (reply != null)
                reply.destroy();
            else
                break; // Interrupt or failure
        }

        System.out.printf("%d requests/replies processed\n", count);
View Full Code Here

                items.register(frontend, ZMQ.Poller.POLLIN);

            items.poll();
            if (items.pollin(0)) {
                // receive whole message (all ZFrames) at once
                ZMsg msg = ZMsg.recvMsg(backend);
                if (msg == null)
                    break; // Interrupted

                // Any sign of life from worker means it's ready
                ZFrame address = msg.unwrap();
                workers.workerReady(new Worker(address));

                // Validate control message, or return reply to client
                if (msg.size() == 1) {
                    ZFrame frame = msg.getFirst();
                    if (!(Arrays.equals(frame.getData(), PPP_HEARTBEAT) || Arrays
                            .equals(frame.getData(), PPP_READY))) {
                        System.out.printf("E: invalid message from worker "
                                + msg.toString());
                    }
                    msg.destroy();
                } else
                    msg.send(frontend);
            }
            if (items.pollin(1)) {
                // Now get next client request, route to next worker
                ZMsg msg = ZMsg.recvMsg(frontend);
                if (msg == null)
                    break; // Interrupted
                msg.push(workers.next());
                msg.send(backend);
            }

            workers.sendHeartbeats(backend);
            workers.purge();
View Full Code Here

TOP

Related Classes of org.zeromq.ZMsg

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.