Search in sources :

Example 6 with Socket

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

use of org.zeromq.ZMQ.Socket 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 8 with Socket

use of org.zeromq.ZMQ.Socket 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 9 with Socket

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

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

the class psenvpub method main.

public static void main(String[] args) throws Exception {
    // Prepare our context and publisher
    Context context = ZMQ.context(1);
    Socket publisher = context.socket(ZMQ.PUB);
    publisher.bind("tcp://*:5563");
    while (!Thread.currentThread().isInterrupted()) {
        // Write two messages, each with an envelope and content
        publisher.sendMore("A");
        publisher.send("We don't want to see this");
        publisher.sendMore("B");
        publisher.send("We would like to see this");
    }
    publisher.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket)

Aggregations

Socket (org.zeromq.ZMQ.Socket)84 Context (org.zeromq.ZMQ.Context)32 ZContext (org.zeromq.ZContext)30 Test (org.junit.Test)26 Poller (org.zeromq.ZMQ.Poller)20 ZMsg (org.zeromq.ZMsg)14 Random (java.util.Random)13 ZFrame (org.zeromq.ZFrame)13 PollItem (org.zeromq.ZMQ.PollItem)6 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 SelectableChannel (java.nio.channels.SelectableChannel)3 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 Entry (java.util.Map.Entry)2 Ignore (org.junit.Ignore)2 Actor (org.zeromq.ZActor.Actor)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1