Search in sources :

Example 11 with Socket

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

the class psenvsub method main.

public static void main(String[] args) {
    // Prepare our context and subscriber
    Context context = ZMQ.context(1);
    Socket subscriber = context.socket(ZMQ.SUB);
    subscriber.connect("tcp://localhost:5563");
    subscriber.subscribe("B".getBytes(ZMQ.CHARSET));
    while (!Thread.currentThread().isInterrupted()) {
        // Read envelope with address
        String address = subscriber.recvStr();
        // Read message contents
        String contents = subscriber.recvStr();
        System.out.println(address + " : " + contents);
    }
    subscriber.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket)

Example 12 with Socket

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

the class mtserver method main.

public static void main(String[] args) {
    Context context = ZMQ.context(1);
    Socket clients = context.socket(ZMQ.ROUTER);
    clients.bind("tcp://*:5555");
    Socket workers = context.socket(ZMQ.DEALER);
    workers.bind("inproc://workers");
    for (int thread_nbr = 0; thread_nbr < 5; thread_nbr++) {
        Thread worker = new Worker(context);
        worker.start();
    }
    //  Connect work threads to client threads via a queue
    ZMQ.proxy(clients, workers, null);
    //  We never get here but clean up anyhow
    clients.close();
    workers.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket)

Example 13 with Socket

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

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

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

the class clonesrv2 method run.

public void run() {
    ZContext ctx = new ZContext();
    Socket publisher = ctx.createSocket(ZMQ.PUB);
    publisher.bind("tcp://*:5557");
    Socket updates = ZThread.fork(ctx, new StateManager());
    Random random = new Random();
    long sequence = 0;
    while (!Thread.currentThread().isInterrupted()) {
        long currentSequenceNumber = ++sequence;
        int key = random.nextInt(10000);
        int body = random.nextInt(1000000);
        ByteBuffer b = ByteBuffer.allocate(4);
        b.asIntBuffer().put(body);
        kvsimple kvMsg = new kvsimple(key + "", currentSequenceNumber, b.array());
        kvMsg.send(publisher);
        // send a message to State Manager thead.
        kvMsg.send(updates);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }
    System.out.printf(" Interrupted\n%d messages out\n", sequence);
    ctx.destroy();
}
Also used : Random(java.util.Random) ZContext(org.zeromq.ZContext) ByteBuffer(java.nio.ByteBuffer) 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