Package org.zeromq

Examples of org.zeromq.ZMsg


        //  If the client already closed request, treat as successful
        if (!new File(filename).exists())
            return true;

        DataInputStream file = null;
        ZMsg request;
        try {
            file = new DataInputStream(new FileInputStream(filename));
            request = ZMsg.load(file);
        } catch (IOException e) {
            e.printStackTrace();
            return true;
        } finally {
            try {
                if (file != null)
                    file.close();
            } catch (IOException e) {
            }
        }
        ZFrame service = request.pop();
        String serviceName = service.toString();

        //  Create MDP client session with short timeout
        mdcliapi client = new mdcliapi("tcp://localhost:5555", false);
        client.setTimeout(1000)//  1 sec
        client.setRetries(1);     //  only 1 retry

        //  Use MMI protocol to check if service is available
        ZMsg mmiRequest = new ZMsg();
        mmiRequest.add(service);
        ZMsg mmiReply = client.send("mmi.service", mmiRequest);
        boolean serviceOK = (mmiReply != null
                && mmiReply.getFirst().toString().equals("200"));
       
        if(mmiReply != null)
            mmiReply.destroy();

        boolean result = false;
        if (serviceOK) {
            ZMsg reply = client.send(serviceName, request);
            if (reply != null) {
                filename = replyFilename(uuid);
                DataOutputStream ofile = null;
                try {
                    ofile = new DataOutputStream(new FileOutputStream(filename));
                    ZMsg.save(reply, ofile);
                } catch (IOException e) {
                    e.printStackTrace();
                    return true;
                } finally {
                    try {
                        if (file != null)
                            file.close();
                    } catch (IOException e) {
                    }
                }
                result = true;
                reply.destroy();
            }
           
        }
        else
            request.destroy();
View Full Code Here


        @Override
        public void run(Object[] args, ZContext ctx, Socket pipe)
        {
            mdwrkapi worker = new mdwrkapi(
                    "tcp://localhost:5555", "titanic.request", false);
            ZMsg reply = null;

            while (true) {
                //  Send reply if it's not null
                //  And then get next request from broker
                ZMsg request = worker.receive(reply);
                if (request == null)
                    break;      //  Interrupted, exit

                //  Ensure message directory exists
                new File(TITANIC_DIR).mkdirs();

                //  Generate UUID and save message to disk
                String uuid = generateUUID();
                String filename = requestFilename(uuid);
                DataOutputStream file = null;
                try {
                    file = new DataOutputStream(new FileOutputStream(filename));
                    ZMsg.save(request, file);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (file != null)
                            file.close();
                    } catch (IOException e) {
                    }
                }
                request.destroy();

                //  Send UUID through to message queue
                reply = new ZMsg();
                reply.add(uuid);
                reply.send(pipe);

                //  Now send UUID back to client
                //  Done by the mdwrk_recv() at the top of the loop
                reply = new ZMsg();
                reply.add("200");
                reply.add(uuid);
            }
            worker.destroy();
        }
View Full Code Here

        @Override
        public void run(Object[] args)
        {
            mdwrkapi worker = new mdwrkapi(
                    "tcp://localhost:5555", "titanic.reply", false);
            ZMsg reply = null;

            while (true) {
                ZMsg request = worker.receive(reply);
                if (request == null)
                    break;      //  Interrupted, exit

                String uuid = request.popString();
                String reqFilename = requestFilename(uuid);
                String repFilename = replyFilename(uuid);

                if (new File(repFilename).exists()) {
                    DataInputStream file = null;
                    try {
                        file = new DataInputStream(new FileInputStream(repFilename));
                        reply = ZMsg.load(file);
                        reply.push("200");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (file != null)
                                file.close();
                        } catch (IOException e) {
                        }
                    }
                }
                else {
                    reply = new ZMsg();
                    if (new File(reqFilename).exists())
                        reply.push("300"); //Pending
                    else
                        reply.push("400"); //Unknown
                }
                request.destroy();
            }
            worker.destroy();
        }
View Full Code Here

        @Override
        public void run(Object[] args)
        {
            mdwrkapi worker = new mdwrkapi(
                    "tcp://localhost:5555", "titanic.close", false);
            ZMsg reply = null;

            while (true) {
                ZMsg request = worker.receive(reply);
                if (request == null)
                    break;      //  Interrupted, exit

                String uuid = request.popString();
                String req_filename = requestFilename(uuid);
                String rep_filename = replyFilename(uuid);
                new File(rep_filename).delete();
                new File(req_filename).delete();

                request.destroy();
                reply = new ZMsg();
                reply.add("200");
            }
            worker.destroy();
        }
View Full Code Here

            ZFrame frame = new ZFrame (WORKER_READY);
            frame.send (worker, 0);

            while(true)
            {
                ZMsg msg = ZMsg.recvMsg (worker);
                if (msg == null)
                    break;

                msg.getLast ().reset ("OK");
                msg.send (worker);
            }
            context.destroy ();
        }
View Full Code Here

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg_) {

            LBBroker arg = (LBBroker)arg_;
            ZMsg msg = ZMsg.recvMsg (arg.frontend);
            if (msg != null) {
                msg.wrap(arg.workers.poll());
                msg.send(arg.backend);

                //  Cancel reader on frontend if we went from 1 to 0 workers
                if (arg.workers.size() == 0) {
                    loop.removePoller (new PollItem (arg.frontend, 0));
                }
View Full Code Here

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg_) {

            LBBroker arg = (LBBroker)arg_;
            ZMsg msg = ZMsg.recvMsg(arg.backend);
            if (msg != null) {
                ZFrame address = msg.unwrap();
                //  Queue worker address for load-balancing
                arg.workers.add(address);

                //  Enable reader on frontend if we went from 0 to 1 workers
                if (arg.workers.size() == 1) {
                    PollItem newItem = new PollItem (arg.frontend, ZMQ.Poller.POLLIN);
                    loop.addPoller (newItem, frontendHandler, arg);
                }

                //  Forward message to client if it's not a READY
                ZFrame frame = msg.getFirst();
                if (Arrays.equals (frame.getData(), WORKER_READY))
                    msg.destroy();
                else
                    msg.send(arg.frontend);
            }
            return 0;
        }
View Full Code Here

    private void processEvents() {
        while (running.get()) {
            try {
                final StatsEMsgBuilder msg = events.take();
                final ZMsg zmsg = makeZMsg();
                zmsg.addString(msg.getHeader());
                zmsg.addString(msg.getBody());
                zmsg.send(publisher);
            } catch (InterruptedException e) {
                LOG.warn("StatsE sender interrupted.");
            } catch (Throwable t) {
                LOG.error("Internal StatsE/ZMQ error", t);
                stopZeroMQ();
View Full Code Here

            }
        }
    }

    protected ZMsg makeZMsg() {
        return new ZMsg();
    }
View Full Code Here

  @Override
  protected void write(ByteBuffer data, final Promise<Void> onComplete, boolean flush) {
    byte[] bytes = new byte[data.remaining()];
    data.get(bytes);
    boolean isNewMsg = MSG_UPD.compareAndSet(this, null, new ZMsg());
    ZMsg msg = MSG_UPD.get(this);
    if (isNewMsg) {
      switch (socket.getType()) {
        case ZMQ.ROUTER:
          msg.add(new ZFrame(connectionId));
          break;
        default:
      }
    }
    msg.add(new ZFrame(bytes));

    if (flush) {
      doFlush(onComplete);
    }
  }
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.