Search in sources :

Example 6 with Poller

use of org.zeromq.ZMQ.Poller 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 Poller

use of org.zeromq.ZMQ.Poller 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 Poller

use of org.zeromq.ZMQ.Poller in project jeromq by zeromq.

the class clonecli3 method run.

public void run() {
    ZContext ctx = new ZContext();
    Socket snapshot = ctx.createSocket(ZMQ.DEALER);
    snapshot.connect("tcp://localhost:5556");
    Socket subscriber = ctx.createSocket(ZMQ.SUB);
    subscriber.connect("tcp://localhost:5557");
    subscriber.subscribe(ZMQ.SUBSCRIPTION_ALL);
    Socket push = ctx.createSocket(ZMQ.PUSH);
    push.connect("tcp://localhost:5558");
    // get state snapshot
    long sequence = 0;
    snapshot.send("ICANHAZ?".getBytes(ZMQ.CHARSET), 0);
    while (true) {
        kvsimple kvMsg = kvsimple.recv(snapshot);
        if (kvMsg == null)
            //  Interrupted
            break;
        sequence = kvMsg.getSequence();
        if ("KTHXBAI".equalsIgnoreCase(kvMsg.getKey())) {
            System.out.println("Received snapshot = " + kvMsg.getSequence());
            // done
            break;
        }
        System.out.println("receiving " + kvMsg.getSequence());
        clonecli3.kvMap.put(kvMsg.getKey(), kvMsg);
    }
    Poller poller = ctx.createPoller(1);
    poller.register(subscriber);
    Random random = new Random();
    // now apply pending updates, discard out-of-getSequence messages
    long alarm = System.currentTimeMillis() + 5000;
    while (true) {
        int rc = poller.poll(Math.max(0, alarm - System.currentTimeMillis()));
        if (rc == -1)
            //  Context has been shut down
            break;
        if (poller.pollin(0)) {
            kvsimple kvMsg = kvsimple.recv(subscriber);
            if (kvMsg == null)
                //  Interrupted
                break;
            if (kvMsg.getSequence() > sequence) {
                sequence = kvMsg.getSequence();
                System.out.println("receiving " + sequence);
                clonecli3.kvMap.put(kvMsg.getKey(), kvMsg);
            }
        }
        if (System.currentTimeMillis() >= alarm) {
            int key = random.nextInt(10000);
            int body = random.nextInt(1000000);
            ByteBuffer b = ByteBuffer.allocate(4);
            b.asIntBuffer().put(body);
            kvsimple kvUpdateMsg = new kvsimple(key + "", 0, b.array());
            kvUpdateMsg.send(push);
            alarm = System.currentTimeMillis() + 1000;
        }
    }
    ctx.destroy();
}
Also used : Random(java.util.Random) ZContext(org.zeromq.ZContext) ByteBuffer(java.nio.ByteBuffer) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Example 9 with Poller

use of org.zeromq.ZMQ.Poller in project jeromq by zeromq.

the class clonecli4 method run.

public void run() {
    ZContext ctx = new ZContext();
    Socket snapshot = ctx.createSocket(ZMQ.DEALER);
    snapshot.connect("tcp://localhost:5556");
    Socket subscriber = ctx.createSocket(ZMQ.SUB);
    subscriber.connect("tcp://localhost:5557");
    subscriber.subscribe(SUBTREE.getBytes(ZMQ.CHARSET));
    Socket push = ctx.createSocket(ZMQ.PUSH);
    push.connect("tcp://localhost:5558");
    // get state snapshot
    snapshot.sendMore("ICANHAZ?");
    snapshot.send(SUBTREE);
    long sequence = 0;
    while (true) {
        kvsimple kvMsg = kvsimple.recv(snapshot);
        if (kvMsg == null)
            //  Interrupted
            break;
        sequence = kvMsg.getSequence();
        if ("KTHXBAI".equalsIgnoreCase(kvMsg.getKey())) {
            System.out.println("Received snapshot = " + kvMsg.getSequence());
            // done
            break;
        }
        System.out.println("receiving " + kvMsg.getSequence());
        clonecli4.kvMap.put(kvMsg.getKey(), kvMsg);
    }
    Poller poller = ctx.createPoller(1);
    poller.register(subscriber);
    Random random = new Random();
    // now apply pending updates, discard out-of-getSequence messages
    long alarm = System.currentTimeMillis() + 5000;
    while (true) {
        int rc = poller.poll(Math.max(0, alarm - System.currentTimeMillis()));
        if (rc == -1)
            //  Context has been shut down
            break;
        if (poller.pollin(0)) {
            kvsimple kvMsg = kvsimple.recv(subscriber);
            if (kvMsg == null)
                //  Interrupted
                break;
            if (kvMsg.getSequence() > sequence) {
                sequence = kvMsg.getSequence();
                System.out.println("receiving " + sequence);
                clonecli4.kvMap.put(kvMsg.getKey(), kvMsg);
            }
        }
        if (System.currentTimeMillis() >= alarm) {
            String key = String.format("%s%d", SUBTREE, random.nextInt(10000));
            int body = random.nextInt(1000000);
            ByteBuffer b = ByteBuffer.allocate(4);
            b.asIntBuffer().put(body);
            kvsimple kvUpdateMsg = new kvsimple(key, 0, b.array());
            kvUpdateMsg.send(push);
            alarm = System.currentTimeMillis() + 1000;
        }
    }
    ctx.destroy();
}
Also used : Random(java.util.Random) ZContext(org.zeromq.ZContext) ByteBuffer(java.nio.ByteBuffer) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Example 10 with Poller

use of org.zeromq.ZMQ.Poller in project jeromq by zeromq.

the class clonesrv4 method run.

public void run() {
    ZContext ctx = new ZContext();
    Socket snapshot = ctx.createSocket(ZMQ.ROUTER);
    snapshot.bind("tcp://*:5556");
    Socket publisher = ctx.createSocket(ZMQ.PUB);
    publisher.bind("tcp://*:5557");
    Socket collector = ctx.createSocket(ZMQ.PULL);
    collector.bind("tcp://*:5558");
    Poller poller = ctx.createPoller(2);
    poller.register(collector, Poller.POLLIN);
    poller.register(snapshot, Poller.POLLIN);
    long sequence = 0;
    while (!Thread.currentThread().isInterrupted()) {
        if (poller.poll(1000) < 0)
            //  Context has been shut down
            break;
        // apply state updates from main thread
        if (poller.pollin(0)) {
            kvsimple kvMsg = kvsimple.recv(collector);
            if (//  Interrupted
            kvMsg == null)
                break;
            kvMsg.setSequence(++sequence);
            kvMsg.send(publisher);
            clonesrv4.kvMap.put(kvMsg.getKey(), kvMsg);
            System.out.printf("I: publishing update %5d\n", sequence);
        }
        // execute state snapshot request
        if (poller.pollin(1)) {
            byte[] identity = snapshot.recv(0);
            if (identity == null)
                //  Interrupted
                break;
            //  .until
            //  Request is in second frame of message
            String request = snapshot.recvStr();
            if (!request.equals("ICANHAZ?")) {
                System.out.println("E: bad request, aborting");
                break;
            }
            String subtree = snapshot.recvStr();
            Iterator<Entry<String, kvsimple>> iter = kvMap.entrySet().iterator();
            while (iter.hasNext()) {
                Entry<String, kvsimple> entry = iter.next();
                kvsimple msg = entry.getValue();
                System.out.println("Sending message " + entry.getValue().getSequence());
                this.sendMessage(msg, identity, subtree, snapshot);
            }
            // now send end message with getSequence number
            System.out.println("Sending state snapshot = " + sequence);
            snapshot.send(identity, ZMQ.SNDMORE);
            kvsimple message = new kvsimple("KTHXBAI", sequence, ZMQ.SUBSCRIPTION_ALL);
            message.send(snapshot);
        }
    }
    System.out.printf(" Interrupted\n%d messages handled\n", sequence);
    ctx.destroy();
}
Also used : Entry(java.util.Map.Entry) ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Aggregations

Poller (org.zeromq.ZMQ.Poller)22 Socket (org.zeromq.ZMQ.Socket)20 ZContext (org.zeromq.ZContext)17 ZMsg (org.zeromq.ZMsg)10 Random (java.util.Random)7 ZFrame (org.zeromq.ZFrame)7 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Entry (java.util.Map.Entry)2 Context (org.zeromq.ZMQ.Context)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 RandomAccessFile (java.io.RandomAccessFile)1