Search in sources :

Example 6 with ZContext

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

the class lruqueue3 method main.

public static void main(String[] args) {
    ZContext context = new ZContext();
    LRUQueueArg arg = new LRUQueueArg();
    //  Prepare our context and sockets
    Socket frontend = context.createSocket(ZMQ.ROUTER);
    Socket backend = context.createSocket(ZMQ.ROUTER);
    arg.frontend = frontend;
    arg.backend = backend;
    frontend.bind("ipc://frontend.ipc");
    backend.bind("ipc://backend.ipc");
    int client_nbr;
    for (client_nbr = 0; client_nbr < 10; client_nbr++) new ClientThread3().start();
    int worker_nbr;
    for (worker_nbr = 0; worker_nbr < 3; worker_nbr++) new WorkerThread3().start();
    //  Queue of available workers
    arg.workers = new LinkedList<ZFrame>();
    //  Prepare reactor and fire it up
    ZLoop reactor = new ZLoop(context);
    reactor.verbose(true);
    PollItem poller = new PollItem(arg.backend, ZMQ.Poller.POLLIN);
    reactor.addPoller(poller, handle_backend, arg);
    reactor.start();
    reactor.destroy();
    for (ZFrame frame : arg.workers) {
        frame.destroy();
    }
    context.destroy();
    System.exit(0);
}
Also used : ZFrame(org.zeromq.ZFrame) PollItem(org.zeromq.ZMQ.PollItem) ZLoop(org.zeromq.ZLoop) ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket)

Example 7 with ZContext

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

the class lvcache method main.

public static void main(String[] args) {
    ZContext context = new ZContext();
    Socket frontend = context.createSocket(ZMQ.SUB);
    frontend.bind("tcp://*:5557");
    Socket backend = context.createSocket(ZMQ.XPUB);
    backend.bind("tcp://*:5558");
    //  Subscribe to every single topic from publisher
    frontend.subscribe(ZMQ.SUBSCRIPTION_ALL);
    //  Store last instance of each topic in a cache
    Map<String, String> cache = new HashMap<String, String>();
    Poller poller = context.createPoller(2);
    poller.register(frontend, Poller.POLLIN);
    poller.register(backend, Poller.POLLIN);
    //  if anything:
    while (true) {
        if (poller.poll(1000) == -1)
            //  Interrupted
            break;
        //  Any new topic data we cache and then forward
        if (poller.pollin(0)) {
            String topic = frontend.recvStr();
            String current = frontend.recvStr();
            if (topic == null)
                break;
            cache.put(topic, current);
            backend.sendMore(topic);
            backend.send(current);
        }
        //  When we get a new subscription, we pull data from the cache:
        if (poller.pollin(1)) {
            ZFrame frame = ZFrame.recvFrame(backend);
            if (frame == null)
                break;
            //  Event is one byte 0=unsub or 1=sub, followed by topic
            byte[] event = frame.getData();
            if (event[0] == 1) {
                String topic = new String(event, 1, event.length - 1, ZMQ.CHARSET);
                System.out.printf("Sending cached topic %s\n", topic);
                String previous = cache.get(topic);
                if (previous != null) {
                    backend.sendMore(topic);
                    backend.send(previous);
                }
            }
            frame.destroy();
        }
    }
    context.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) HashMap(java.util.HashMap) ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Example 8 with ZContext

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

the class espresso method main.

//  .split main thread
//  The main task starts the subscriber and publisher, and then sets
//  itself up as a listening proxy. The listener runs as a child thread:
public static void main(String[] argv) {
    //  Start child threads
    ZContext ctx = new ZContext();
    ZThread.fork(ctx, new Publisher());
    ZThread.fork(ctx, new Subscriber());
    Socket subscriber = ctx.createSocket(ZMQ.XSUB);
    subscriber.connect("tcp://localhost:6000");
    Socket publisher = ctx.createSocket(ZMQ.XPUB);
    publisher.bind("tcp://*:6001");
    Socket listener = ZThread.fork(ctx, new Listener());
    ZMQ.proxy(subscriber, publisher, listener);
    System.out.println(" interrupted");
    //  Tell attached threads to exit
    ctx.destroy();
}
Also used : ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket)

Example 9 with ZContext

use of org.zeromq.ZContext 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 10 with ZContext

use of org.zeromq.ZContext 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)

Aggregations

ZContext (org.zeromq.ZContext)33 Socket (org.zeromq.ZMQ.Socket)30 Poller (org.zeromq.ZMQ.Poller)17 ZMsg (org.zeromq.ZMsg)14 ZFrame (org.zeromq.ZFrame)13 Random (java.util.Random)11 ArrayList (java.util.ArrayList)4 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 Entry (java.util.Map.Entry)2 ZLoop (org.zeromq.ZLoop)2 PollItem (org.zeromq.ZMQ.PollItem)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 LinkedList (java.util.LinkedList)1