Package org.zeromq.ZMQ

Examples of org.zeromq.ZMQ.Socket


        public void run (Object ... args)
        {
            ZContext context = new ZContext();

            //  Prepare our context and sockets
            Socket client  = context.createSocket (ZMQ.REQ);
            ZHelper.setId (client);     //  Set a printable identity

            client.connect("ipc://frontend.ipc");

            //  Send request, get reply
            client.send("HELLO");
            String reply = client.recvStr ();
            System.out.println("Client: " + reply);

            context.destroy ();
        }
View Full Code Here


        public void run (Object ... args)
        {
            ZContext context = new ZContext();

            //  Prepare our context and sockets
            Socket worker  = context.createSocket (ZMQ.REQ);
            ZHelper.setId (worker);     //  Set a printable identity

            worker.connect("ipc://backend.ipc");

            //  Tell backend we're ready for work
            ZFrame frame = new ZFrame (WORKER_READY);
            frame.send (worker, 0);
View Full Code Here

{
    public static void main(String[] args) throws Exception {
        Context context = ZMQ.context(1);

        //  Socket to talk to server
        Socket responder = context.socket(ZMQ.REP);
        responder.connect("tcp://localhost:5560");

        while (!Thread.currentThread().isInterrupted()) {
            //  Wait for next request from client
            String string = responder.recvStr(0);
            System.out.printf("Received request: [%s]\n", string);

            //  Do some 'work'
            Thread.sleep(1000);

            //  Send reply back to client
            responder.send("World");
        }

        //  We never get here but clean up anyhow
        responder.close();
        context.term();
    }
View Full Code Here

     * it easier to start and stop the example. Each thread has its own
     * context and conceptually acts as a separate process.
     */
    public static void main (String[] args) throws Exception {
        Context context = ZMQ.context(1);
        Socket broker = context.socket(ZMQ.ROUTER);
        broker.bind("tcp://*:5671");

        for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++)
        {
            Thread worker = new Worker ();
            worker.start ();
        }

        //  Run for five seconds and then tell workers to end
        long endTime = System.currentTimeMillis () + 5000;
        int workersFired = 0;
        while (true) {
            //  Next message gives us least recently used worker
            String identity = broker.recvStr ();
            broker.sendMore (identity);
            broker.recvStr ();     //  Envelope delimiter
            broker.recvStr ();     //  Response from worker
            broker.sendMore ("");

            //  Encourage workers until it's time to fire them
            if (System.currentTimeMillis () < endTime)
                broker.send ("Work harder");
            else {
                broker.send ("Fired!");
                if (++workersFired == NBR_WORKERS)
                    break;
            }
        }

        broker.close();
        context.term();
    }
View Full Code Here

    @Test
    public void bindAndConnectAssociationsExists() throws URISyntaxException {
        ZmqAssociation b = ZmqAssociation.fromURI("bind:inproc://1");
        ZmqAssociation c = ZmqAssociation.fromURI("connect:inproc://1");
        Socket reqSocket = createSocket(ZMQ.REQ);
        Socket repSocket = createSocket(ZMQ.REP);

        b.associate(reqSocket);
        c.associate(repSocket);

        byte[] msg = new byte[]{1};
        reqSocket.send(msg, 0);
        byte[] recv = repSocket.recv(0);
        assertEquals(msg, recv);

    }
View Full Code Here

    }

    @Test
    public void bindReversing() throws URISyntaxException {
        ZmqAssociation bindAssoc = ZmqAssociation.fromURI("bind:inproc://2");
        Socket reqSocket = createSocket(ZMQ.REQ);
        Socket repSocket = createSocket(ZMQ.REP);

        bindAssoc.associate(reqSocket);
        bindAssoc.reverse().associate(repSocket);

        byte[] msg = new byte[]{1};
        reqSocket.send(msg, 0);
        byte[] recv = repSocket.recv(0);
        assertEquals(msg, recv);


    }
View Full Code Here

    @Test
    public void connectReversing() throws URISyntaxException {
        ZmqAssociation connectAssoc = ZmqAssociation.fromURI("connect:inproc://3");

        Socket reqSocket = createSocket(ZMQ.REQ);
        Socket repSocket = createSocket(ZMQ.REP);

        connectAssoc.reverse().associate(repSocket);
        connectAssoc.associate(reqSocket);

        byte[] msg = new byte[]{1};
        reqSocket.send(msg, 0);
        byte[] recv = repSocket.recv(0);
        assertEquals(msg, recv);

    }
View Full Code Here

    @Test
    public void multipleAddressesCanBeDefined() throws URISyntaxException {
        ZmqAssociation assoc = ZmqAssociation.fromURI("connect:inproc://place1;connect:inproc://place2");

        Socket pushSocket1 = createSocket(ZMQ.PUSH);
        Socket pushSocket2 = createSocket(ZMQ.PUSH);
        Socket pullSocket = createSocket(ZMQ.PULL);

        pushSocket1.bind("inproc://place1");
        pushSocket2.bind("inproc://place2");
        assoc.associate(pullSocket);

        byte[] msg1 = new byte[]{1};
        byte[] msg2 = new byte[]{2, 2};
        pushSocket1.send(msg1, 0);
        byte[] recv1 = pullSocket.recv(0);
        assertEquals(recv1, msg1);
        pushSocket2.send(msg2, 0);
        byte[] recv2 = pullSocket.recv(0);
        assertEquals(recv2, msg2);


    }
View Full Code Here

            socket.close();
        }
    }

    protected Socket createSocket(int socketType) {
        final Socket socket = context.socket(socketType);
        sockets.add(socket);
        return socket;
    }
View Full Code Here

    {
        System.out.printf(" * kvmsg: ");

        //  Prepare our context and sockets
        ZContext ctx = new ZContext();
        Socket output = ctx.createSocket(ZMQ.DEALER);
        output.bind("ipc://kvmsg_selftest.ipc");
        Socket input = ctx.createSocket(ZMQ.DEALER);
        input.connect("ipc://kvmsg_selftest.ipc");

        Map<String,kvmsg> kvmap = new HashMap<String, kvmsg>();

        //  .until
        //  Test send and receive of simple message
View Full Code Here

TOP

Related Classes of org.zeromq.ZMQ.Socket

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.