Package org.zeromq

Examples of org.zeromq.ZContext


    private ZContext ctx;
    private Socket sock;

    public Consumer(String addr, String topic, int bufsize)
    {
        ctx = new ZContext();
        ctx.setLinger(-1);
        sock = ctx.createSocket(ZMQ.DEALER);
        sock.setReceiveBufferSize(bufsize);
        sock.setIdentity(ZPUtils.genTopicIdentity(topic, 0));
        sock.connect("tcp://" + addr);
View Full Code Here


        this.conf = conf;
    }

    public void start()
    {
        context = new ZContext();
        // fix lazy creation
        context.setContext(ZMQ.context(Integer.parseInt(conf.getProperty("io_threads", "1"))));


        ZPWriter writer = new ZPWriter(context, conf);
View Full Code Here

    private ZContext ctx;
    private Socket sock;

    public Producer(String topic, Properties props)
    {
        ctx = new ZContext();
        ctx.setLinger(-1);
        sock = ctx.createSocket(ZMQ.DEALER);
        sock.setSndHWM(0);
        sock.setIdentity(ZPUtils.genTopicIdentity(topic, 0));
        for (String addr : props.getProperty("writer.list").split(",")) {
View Full Code Here

//  Subscribes to one random topic and prints received messages
public class pathosub
{
    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());

        while (true) {
            String topic = subscriber.recvStr();
            if (topic == null)
                break;
            String data = subscriber.recvStr();
            assert(topic.equals(subscription));
            System.out.println(data);
        }
        context.destroy();
    }
View Full Code Here

    }

    public clonesrv5 ()
    {
        port = 5556;
        ctx = new ZContext();
        kvmap = new HashMap<String, kvmsg>();
        loop = new ZLoop();
        loop.verbose(false);

        //  Set up our clone server sockets
View Full Code Here

    private ZContext ctx;        //  Our context wrapper
    private Socket pipe;         //  Pipe through to flcliapi agent

    public flcliapi()
    {
        ctx = new ZContext();
        FreelanceAgent agent = new FreelanceAgent();
        pipe = ZThread.fork(ctx, agent);
    }
View Full Code Here

public class tripping {

    static class Broker implements Runnable {
        @Override
        public void run() {
            ZContext ctx = new ZContext();
            Socket frontend = ctx.createSocket(ZMQ.ROUTER);
            Socket backend = ctx.createSocket(ZMQ.ROUTER);
            frontend.setHWM (-1);
            backend.setHWM (-1);
            frontend.bind("tcp://*:5555");
            backend.bind("tcp://*:5556");

            while (!Thread.currentThread().isInterrupted()) {
                ZMQ.Poller items = new ZMQ.Poller(2);
                items.register(frontend, ZMQ.Poller.POLLIN);
                items.register(backend, ZMQ.Poller.POLLIN);
               
                if (items.poll() == -1)
                    break; // Interrupted
                if (items.pollin(0)) {
                    ZMsg msg = ZMsg.recvMsg(frontend);
                    if (msg == null)
                        break; // Interrupted
                    ZFrame address = msg.pop();
                    address.destroy();
                    msg.addFirst(new ZFrame("W"));
                    msg.send(backend);
                }
                if (items.pollin(1)) {

                    ZMsg msg = ZMsg.recvMsg(backend);
                    if (msg == null)
                        break; // Interrupted
                    ZFrame address = msg.pop();
                    address.destroy();
                    msg.addFirst(new ZFrame("C"));
                    msg.send(frontend);
                }
            }
            ctx.destroy();
        }
View Full Code Here

    static class Worker implements Runnable {

        @Override
        public void run() {
            ZContext ctx = new ZContext();
            Socket worker = ctx.createSocket(ZMQ.DEALER);
            worker.setHWM (-1);
            worker.setIdentity("W".getBytes());
            worker.connect("tcp://localhost:5556");
            while (!Thread.currentThread().isInterrupted()) {
                ZMsg msg = ZMsg.recvMsg(worker);
                msg.send(worker);
            }

            ctx.destroy();

        }
View Full Code Here

    static class Client implements Runnable {
        private static int SAMPLE_SIZE = 10000;

        @Override
        public void run() {
            ZContext ctx = new ZContext();
            Socket client = ctx.createSocket(ZMQ.DEALER);
            client.setHWM (-1);
            client.setIdentity("C".getBytes());
            client.connect("tcp://localhost:5555");
            System.out.println("Setting up test");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

            int requests;
            long start;

            System.out.println("Synchronous round-trip test");
            start = System.currentTimeMillis();

            for (requests = 0; requests < SAMPLE_SIZE; requests++) {
                ZMsg req = new ZMsg();
                req.addString("hello");
                req.send(client);
                ZMsg.recvMsg(client).destroy();
            }

            System.out.printf(" %d calls/second\n",
                    (1000 * SAMPLE_SIZE) / (System.currentTimeMillis() - start));

            System.out.println("Asynchronous round-trip test");
            start = System.currentTimeMillis();

            for (requests = 0; requests < SAMPLE_SIZE; requests++) {
                ZMsg req = new ZMsg();
                req.addString("hello");
                req.send(client);
            }
            for (requests = 0; requests < SAMPLE_SIZE
                    && !Thread.currentThread().isInterrupted(); requests++) {
                ZMsg.recvMsg(client).destroy();
            }

            System.out.printf(" %d calls/second\n",
                    (1000 * SAMPLE_SIZE) / (System.currentTimeMillis() - start));

            ctx.destroy();
        }
View Full Code Here

    private static final long REQUEST_TIMEOUT = 1000;    //  msecs
    private static final long SETTLE_DELAY = 2000;       //  Before failing over

    public static void main(String[] argv) throws Exception
    {
        ZContext ctx = new ZContext();

        String[] server = { "tcp://localhost:5001", "tcp://localhost:5002" };
        int serverNbr = 0;

        System.out.printf ("I: connecting to server at %s...\n", server [serverNbr]);
        Socket client = ctx.createSocket(ZMQ.REQ);
        client.connect(server[serverNbr]);

        int sequence = 0;
        while (!Thread.currentThread().isInterrupted()) {
            //  We send a request, then we work to get a reply
            String request = String.format("%d", ++sequence);
            client.send(request);

            boolean expectReply = true;
            while (expectReply) {
                //  Poll socket for a reply, with timeout
                PollItem items [] = { new PollItem(client, ZMQ.Poller.POLLIN) };
                int rc = ZMQ.poll(items, 1, REQUEST_TIMEOUT);
                if (rc == -1)
                    break;          //  Interrupted

                //  .split main body of client
                //  We use a Lazy Pirate strategy in the client. If there's no
                //  reply within our timeout, we close the socket and try again.
                //  In Binary Star, it's the client vote that decides which
                //  server is primary; the client must therefore try to connect
                //  to each server in turn:

                if (items[0].isReadable()) {
                    //  We got a reply from the server, must match sequence
                    String reply = client.recvStr();
                    if (Integer.parseInt(reply) == sequence) {
                        System.out.printf ("I: server replied OK (%s)\n", reply);
                        expectReply = false;
                        Thread.sleep(1000)//  One request per second
                    }
                    else
                        System.out.printf ("E: bad reply from server: %s\n", reply);
                }
                else {
                    System.out.printf ("W: no response from server, failing over\n");

                    //  Old socket is confused; close it and open a new one
                    ctx.destroySocket(client);
                    serverNbr = (serverNbr + 1) % 2;
                    Thread.sleep(SETTLE_DELAY);
                    System.out.printf("I: connecting to server at %s...\n",
                            server[serverNbr]);
                    client = ctx.createSocket(ZMQ.REQ);
                    client.connect(server[serverNbr]);

                    //  Send request again, on new socket
                    client.send(request);
                }
            }
        }
        ctx.destroy();

    }
View Full Code Here

TOP

Related Classes of org.zeromq.ZContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.