Search in sources :

Example 6 with ZFrame

use of org.zeromq.ZFrame in project jeromq by zeromq.

the class ppqueue method main.

//  The main task is an LRU queue with heartbeating on workers so we can
//  detect crashed or blocked worker tasks:
public static void main(String[] args) {
    ZContext ctx = new ZContext();
    Socket frontend = ctx.createSocket(ZMQ.ROUTER);
    Socket backend = ctx.createSocket(ZMQ.ROUTER);
    //  For clients
    frontend.bind("tcp://*:5555");
    //  For workers
    backend.bind("tcp://*:5556");
    //  List of available workers
    ArrayList<Worker> workers = new ArrayList<Worker>();
    //  Send out heartbeats at regular intervals
    long heartbeat_at = System.currentTimeMillis() + HEARTBEAT_INTERVAL;
    Poller poller = ctx.createPoller(2);
    poller.register(backend, Poller.POLLIN);
    poller.register(frontend, Poller.POLLIN);
    while (true) {
        boolean workersAvailable = workers.size() > 0;
        int rc = poller.poll(HEARTBEAT_INTERVAL);
        if (rc == -1)
            //  Interrupted
            break;
        //  Handle worker activity on backend
        if (poller.pollin(0)) {
            //  Use worker address for LRU routing
            ZMsg msg = ZMsg.recvMsg(backend);
            if (msg == null)
                //  Interrupted
                break;
            //  Any sign of life from worker means it's ready
            ZFrame address = msg.unwrap();
            Worker worker = new Worker(address);
            worker.ready(workers);
            //  Validate control message, or return reply to client
            if (msg.size() == 1) {
                ZFrame frame = msg.getFirst();
                String data = new String(frame.getData(), ZMQ.CHARSET);
                if (!data.equals(PPP_READY) && !data.equals(PPP_HEARTBEAT)) {
                    System.out.println("E: invalid message from worker");
                    msg.dump(System.out);
                }
                msg.destroy();
            } else
                msg.send(frontend);
        }
        if (workersAvailable && poller.pollin(1)) {
            //  Now get next client request, route to next worker
            ZMsg msg = ZMsg.recvMsg(frontend);
            if (msg == null)
                //  Interrupted
                break;
            msg.push(Worker.next(workers));
            msg.send(backend);
        }
        if (System.currentTimeMillis() >= heartbeat_at) {
            for (Worker worker : workers) {
                worker.address.send(backend, ZFrame.REUSE + ZFrame.MORE);
                ZFrame frame = new ZFrame(PPP_HEARTBEAT);
                frame.send(backend, 0);
            }
            heartbeat_at = System.currentTimeMillis() + HEARTBEAT_INTERVAL;
        }
        Worker.purge(workers);
    }
    //  When we're done, clean up properly
    while (workers.size() > 0) {
        Worker worker = workers.remove(0);
    }
    workers.clear();
    ctx.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) ArrayList(java.util.ArrayList) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Example 7 with ZFrame

use of org.zeromq.ZFrame in project jeromq by zeromq.

the class ppworker method main.

//  We have a single task, which implements the worker side of the
//  Paranoid Pirate Protocol (PPP). The interesting parts here are
//  the heartbeating, which lets the worker detect if the queue has
//  died, and vice-versa:
public static void main(String[] args) {
    ZContext ctx = new ZContext();
    Socket worker = worker_socket(ctx);
    Poller poller = ctx.createPoller(1);
    poller.register(worker, Poller.POLLIN);
    //  If liveness hits zero, queue is considered disconnected
    int liveness = HEARTBEAT_LIVENESS;
    int interval = INTERVAL_INIT;
    //  Send out heartbeats at regular intervals
    long heartbeat_at = System.currentTimeMillis() + HEARTBEAT_INTERVAL;
    Random rand = new Random(System.nanoTime());
    int cycles = 0;
    while (true) {
        int rc = poller.poll(HEARTBEAT_INTERVAL);
        if (rc == -1)
            //  Interrupted
            break;
        if (poller.pollin(0)) {
            //  Get message
            //  - 3-part envelope + content -> request
            //  - 1-part HEARTBEAT -> heartbeat
            ZMsg msg = ZMsg.recvMsg(worker);
            if (msg == null)
                //  Interrupted
                break;
            //  first:
            if (msg.size() == 3) {
                cycles++;
                if (cycles > 3 && rand.nextInt(5) == 0) {
                    System.out.println("I: simulating a crash\n");
                    msg.destroy();
                    msg = null;
                    break;
                } else if (cycles > 3 && rand.nextInt(5) == 0) {
                    System.out.println("I: simulating CPU overload\n");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
                System.out.println("I: normal reply\n");
                msg.send(worker);
                liveness = HEARTBEAT_LIVENESS;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    break;
                }
            //  Do some heavy work
            } else //  queue was (recently) alive, so reset our liveness indicator:
            if (msg.size() == 1) {
                ZFrame frame = msg.getFirst();
                if (PPP_HEARTBEAT.equals(new String(frame.getData(), ZMQ.CHARSET)))
                    liveness = HEARTBEAT_LIVENESS;
                else {
                    System.out.println("E: invalid message\n");
                    msg.dump(System.out);
                }
                msg.destroy();
            } else {
                System.out.println("E: invalid message\n");
                msg.dump(System.out);
            }
            interval = INTERVAL_INIT;
        } else //  discarding any messages we might have sent in the meantime://
        if (--liveness == 0) {
            System.out.println("W: heartbeat failure, can't reach queue\n");
            System.out.printf("W: reconnecting in %sd msec\n", interval);
            try {
                Thread.sleep(interval);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (interval < INTERVAL_MAX)
                interval *= 2;
            ctx.destroySocket(worker);
            worker = worker_socket(ctx);
            liveness = HEARTBEAT_LIVENESS;
        }
        //  Send heartbeat to queue if it's time
        if (System.currentTimeMillis() > heartbeat_at) {
            heartbeat_at = System.currentTimeMillis() + HEARTBEAT_INTERVAL;
            System.out.println("I: worker heartbeat\n");
            ZFrame frame = new ZFrame(PPP_HEARTBEAT);
            frame.send(worker, 0);
        }
    }
    ctx.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) Random(java.util.Random) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Example 8 with ZFrame

use of org.zeromq.ZFrame in project jeromq by zeromq.

the class mdcliapi2 method recv.

/**
     * Returns the reply message or NULL if there was no reply. Does not attempt
     * to recover from a broker failure, this is not possible without storing
     * all unanswered requests and resending them all…
     */
public ZMsg recv() {
    ZMsg reply = null;
    // Poll socket for a reply, with timeout
    ZMQ.Poller items = ctx.createPoller(1);
    items.register(client, ZMQ.Poller.POLLIN);
    if (items.poll(timeout * 1000) == -1)
        // Interrupted
        return null;
    if (items.pollin(0)) {
        ZMsg msg = ZMsg.recvMsg(client);
        if (verbose) {
            log.format("I: received reply: \n");
            msg.dump(log.out());
        }
        // Don't try to handle errors, just assert noisily
        assert (msg.size() >= 4);
        ZFrame empty = msg.pop();
        assert (empty.getData().length == 0);
        empty.destroy();
        ZFrame header = msg.pop();
        assert (MDP.C_CLIENT.equals(header.toString()));
        header.destroy();
        ZFrame replyService = msg.pop();
        replyService.destroy();
        reply = msg;
    }
    return reply;
}
Also used : ZFrame(org.zeromq.ZFrame) ZMsg(org.zeromq.ZMsg) ZMQ(org.zeromq.ZMQ)

Example 9 with ZFrame

use of org.zeromq.ZFrame in project jeromq by zeromq.

the class mdwrkapi method sendToBroker.

/**
     * Send message to broker If no msg is provided, creates one internally
     *
     * @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));
    msg.addFirst(command.newFrame());
    msg.addFirst(MDP.W_WORKER.newFrame());
    msg.addFirst(new ZFrame(ZMQ.MESSAGE_SEPARATOR));
    if (verbose) {
        log.format("I: sending %s to broker\n", command);
        msg.dump(log.out());
    }
    msg.send(worker);
}
Also used : ZFrame(org.zeromq.ZFrame) ZMsg(org.zeromq.ZMsg)

Example 10 with ZFrame

use of org.zeromq.ZFrame in project jeromq by zeromq.

the class lruqueue3 method handle.

@Override
public int handle(ZLoop loop, PollItem item, Object arg_) {
    LRUQueueArg arg = (LRUQueueArg) arg_;
    ZMsg msg = ZMsg.recvMsg(arg.backend);
    if (msg != null) {
        ZFrame address = msg.unwrap();
        //  Queue worker address for LRU routing
        arg.workers.add(address);
        //  Enable reader on frontend if we went from 0 to 1 workers
        if (arg.workers.size() == 1) {
            PollItem poller = new PollItem(arg.frontend, ZMQ.Poller.POLLIN);
            loop.addPoller(poller, lruqueue3.handle_frontend, arg);
        }
        //  Forward message to client if it's not a READY
        ZFrame frame = msg.getFirst();
        if (new String(frame.getData(), ZMQ.CHARSET).equals(lruqueue3.LRU_READY))
            msg.destroy();
        else
            msg.send(arg.frontend);
    }
    return 0;
}
Also used : ZFrame(org.zeromq.ZFrame) PollItem(org.zeromq.ZMQ.PollItem) ZMsg(org.zeromq.ZMsg)

Aggregations

ZFrame (org.zeromq.ZFrame)27 ZMsg (org.zeromq.ZMsg)20 ZContext (org.zeromq.ZContext)13 Socket (org.zeromq.ZMQ.Socket)13 Poller (org.zeromq.ZMQ.Poller)7 ArrayList (java.util.ArrayList)4 Random (java.util.Random)4 ZMQ (org.zeromq.ZMQ)4 PollItem (org.zeromq.ZMQ.PollItem)3 IOException (java.io.IOException)2 ZLoop (org.zeromq.ZLoop)2 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1