Search in sources :

Example 16 with ZContext

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

the class clonecli1 method run.

public void run() {
    ZContext ctx = new ZContext();
    Socket subscriber = ctx.createSocket(ZMQ.SUB);
    subscriber.connect("tcp://localhost:5556");
    subscriber.subscribe(ZMQ.SUBSCRIPTION_ALL);
    while (true) {
        kvsimple kvMsg = kvsimple.recv(subscriber);
        if (kvMsg == null)
            break;
        clonecli1.kvMap.put(kvMsg.getKey(), kvMsg);
        System.out.println("receiving " + kvMsg);
        sequence.incrementAndGet();
    }
    ctx.destroy();
}
Also used : ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket)

Example 17 with ZContext

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

the class clonecli5 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 publisher = ctx.createSocket(ZMQ.PUSH);
    publisher.connect("tcp://localhost:5558");
    Map<String, kvmsg> kvMap = new HashMap<String, kvmsg>();
    // get state snapshot
    snapshot.sendMore("ICANHAZ?");
    snapshot.send(SUBTREE);
    long sequence = 0;
    while (true) {
        kvmsg kvMsg = kvmsg.recv(snapshot);
        if (kvMsg == null)
            //  Interrupted
            break;
        sequence = kvMsg.getSequence();
        if ("KTHXBAI".equalsIgnoreCase(kvMsg.getKey())) {
            System.out.println("Received snapshot = " + kvMsg.getSequence());
            kvMsg.destroy();
            // done
            break;
        }
        System.out.println("receiving " + kvMsg.getSequence());
        kvMsg.store(kvMap);
    }
    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)) {
            kvmsg kvMsg = kvmsg.recv(subscriber);
            if (kvMsg == null)
                //  Interrupted
                break;
            if (kvMsg.getSequence() > sequence) {
                sequence = kvMsg.getSequence();
                System.out.println("receiving " + sequence);
                kvMsg.store(kvMap);
            } else
                kvMsg.destroy();
        }
        if (System.currentTimeMillis() >= alarm) {
            kvmsg kvMsg = new kvmsg(0);
            kvMsg.fmtKey("%s%d", SUBTREE, random.nextInt(10000));
            kvMsg.fmtBody("%d", random.nextInt(1000000));
            kvMsg.setProp("ttl", "%d", random.nextInt(30));
            kvMsg.send(publisher);
            kvMsg.destroy();
            alarm = System.currentTimeMillis() + 1000;
        }
    }
    ctx.destroy();
}
Also used : Random(java.util.Random) HashMap(java.util.HashMap) ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Example 18 with ZContext

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

the class clonesrv3 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);
            clonesrv3.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;
            String request = snapshot.recvStr();
            if (!request.equals("ICANHAZ?")) {
                System.out.println("E: bad request, aborting");
                break;
            }
            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, 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)

Example 19 with ZContext

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

the class titanic method main.

//  .split worker task
//  This is the main thread for the Titanic worker. It starts three child
//  threads; for the request, reply, and close services. It then dispatches
//  requests to workers using a simple brute force disk queue. It receives
//  request UUIDs from the {{titanic.request}} service, saves these to a disk
//  file, and then throws each request at MDP workers until it gets a
//  response.
public static void main(String[] args) {
    boolean verbose = (args.length > 0 && "-v".equals(args[0]));
    ZContext ctx = new ZContext();
    Socket requestPipe = ZThread.fork(ctx, new TitanicRequest());
    ZThread.start(new TitanicReply());
    ZThread.start(new TitanicClose());
    Poller poller = ctx.createPoller(1);
    poller.register(requestPipe, ZMQ.Poller.POLLIN);
    //  Main dispatcher loop
    while (true) {
        //  We'll dispatch once per second, if there's no activity
        int rc = poller.poll(1000);
        if (rc == -1)
            //  Interrupted
            break;
        if (poller.pollin(0)) {
            //  Ensure message directory exists
            new File(TITANIC_DIR).mkdirs();
            //  Append UUID to queue, prefixed with '-' for pending
            ZMsg msg = ZMsg.recvMsg(requestPipe);
            if (msg == null)
                //  Interrupted
                break;
            String uuid = msg.popString();
            BufferedWriter wfile = null;
            try {
                wfile = new BufferedWriter(new FileWriter(TITANIC_DIR + "/queue", true));
                wfile.write("-" + uuid + "\n");
            } catch (IOException e) {
                e.printStackTrace();
                break;
            } finally {
                try {
                    if (wfile != null)
                        wfile.close();
                } catch (IOException e) {
                }
            }
            msg.destroy();
        }
        //  Brute force dispatcher
        //"?........:....:....:....:............:";
        byte[] entry = new byte[37];
        RandomAccessFile file = null;
        try {
            file = new RandomAccessFile(TITANIC_DIR + "/queue", "rw");
            while (file.read(entry) > 0) {
                //  UUID is prefixed with '-' if still waiting
                if (entry[0] == '-') {
                    if (verbose)
                        System.out.printf("I: processing request %s\n", new String(entry, 1, entry.length - 1, ZMQ.CHARSET));
                    if (serviceSuccess(new String(entry, 1, entry.length - 1, ZMQ.CHARSET))) {
                        //  Mark queue entry as processed
                        file.seek(file.getFilePointer() - 37);
                        file.writeBytes("+");
                        file.seek(file.getFilePointer() + 36);
                    }
                }
                //  Skip end of line, LF or CRLF
                if (file.readByte() == '\r')
                    file.readByte();
                if (Thread.currentThread().isInterrupted())
                    break;
            }
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (file != null) {
                try {
                    file.close();
                } catch (IOException e) {
                }
            }
        }
    }
}
Also used : FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) BufferedWriter(java.io.BufferedWriter) RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Example 20 with ZContext

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

the class pathosub method main.

public static void main(String[] args) {
    ZContext context = new ZContext();
    Socket subscriber = context.createSocket(ZMQ.SUB);
    if (args.length == 1)
        subscriber.connect(args[0]);
    else
        subscriber.connect("tcp://localhost:5556");
    Random rand = new Random(System.currentTimeMillis());
    String subscription = String.format("%03d", rand.nextInt(1000));
    subscriber.subscribe(subscription.getBytes(ZMQ.CHARSET));
    while (true) {
        String topic = subscriber.recvStr();
        if (topic == null)
            break;
        String data = subscriber.recvStr();
        assert (topic.equals(subscription));
        System.out.println(data);
    }
    context.destroy();
}
Also used : Random(java.util.Random) ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket)

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