Package org.zeromq.ZMQ

Examples of org.zeromq.ZMQ.Socket


        raf.close();
        ch.close();

        Context ctx = ZMQ.context(1);
        Socket router = ctx.socket(ZMQ.ROUTER);
        router.bind("tcp://127.0.0.1:6003");
        router.setEncoder(Persistence.PersistEncoder.class);

        Socket dealer = ctx.socket(ZMQ.DEALER);
        dealer.setIdentity("A".getBytes());
        dealer.connect("tcp://127.0.0.1:6003");

        Thread.sleep(1000);
        router.sendMore("A");
        router.sendMore(new byte[] {Persistence.MESSAGE_FILE});
        router.sendMore(new byte[] {STATUS_OK});
        router.sendMore(path);
        router.sendMore(ByteBuffer.wrap(new byte[8]).putLong(0).array());
        router.send(ByteBuffer.wrap(new byte[8]).putLong(329).array());

        assertEquals(dealer.recv()[0], STATUS_OK);
        ByteBuffer content = ByteBuffer.wrap(dealer.recv());

        assertEquals(content.limit(), 329);
        assertEquals(0, content.get());
        int length = content.get();
        assertEquals(5, length);
        byte[] data = new byte[length];
        content.get(data);
        assertEquals("12345", new String(data));

        assertEquals(1, content.get());
        length = content.get();
        assertEquals(11, length);
        data = new byte[length];
        content.get(data);
        assertEquals("67890abcdef", new String(data));

        assertEquals(2, content.get());
        length = (int) content.getLong();
        assertEquals(300, length);
        data = new byte[length];
        content.get(data);
        assertEquals(longdata, new String(data));

        assertEquals(false, content.hasRemaining());

        dealer.close();
        router.close();
        ctx.term();
    }
View Full Code Here


    @Test
    public void testSend() throws Exception
    {
        Context ctx = ZMQ.context(1);
        Socket sock = ctx.socket(ZMQ.DEALER);

        ZLog zlog = ZLogManager.instance().get(topic);
        long offset = zlog.offset();
        String data = "hello";

        System.out.println("previous offset " + zlog.path().getAbsolutePath() + ":" + offset);

        sock.setIdentity(ZPUtils.genTopicIdentity(topic, 0));
        sock.setLinger(100);

        sock.connect("tcp://127.0.0.1:5557");


        boolean ret = sock.send(data);
        assertTrue(ret);

        // wait until flush
        Thread.sleep(1000);
        zlog.flush();

        assertEquals(zlog.offset(), offset + data.length() + 2);

        sock.close();
        ctx.term();
    }
View Full Code Here

    @Test
    public void testSendPush() throws Exception
    {
        Context ctx = ZMQ.context(1);
        Socket sock = ctx.socket(ZMQ.PUSH);

        ZLog zlog = ZLogManager.instance().get(topic);
        long offset = zlog.offset();
        String data = "hello";

        System.out.println("previous offset " + zlog.path().getAbsolutePath() + ":" + offset);

        sock.setIdentity(ZPUtils.genTopicIdentity(topic, 0));
        sock.setLinger(100);

        sock.connect("tcp://127.0.0.1:5557");


        boolean ret = sock.send(data);
        assertTrue(ret);

        // wait until flush
        Thread.sleep(1000);
        zlog.flush();

        assertEquals(zlog.offset(), offset + data.length() + 2);

        sock.close();
        ctx.term();
    }
View Full Code Here

    {
        @Override
        public void run()
        {
            ZContext ctx = new ZContext();
            Socket worker = ctx.createSocket(ZMQ.REQ);
            worker.connect(String.format("ipc://%s-localbe.ipc", self));

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

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 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();
    }
View Full Code Here

    {
        @Override
        public int handle(ZLoop loop, PollItem item, Object arg)
        {
            clonesrv5 srv = (clonesrv5) arg;
            Socket socket = item.getSocket();

            byte[] identity = socket.recv();
            if (identity != null) {
                //  Request is in second frame of message
                String request = socket.recvStr();
                String subtree = null;
                if (request.equals("ICANHAZ?")) {
                    subtree = socket.recvStr();
                }
                else
                    System.out.printf("E: bad request, aborting\n");

                if (subtree != null) {
                    //  Send state socket to client
                    for (Entry<String, kvmsg> entry: srv.kvmap.entrySet()) {
                        sendSingle(entry.getValue(), identity, subtree, socket);
                    }

                    //  Now send END message with getSequence number
                    System.out.printf("I: sending shapshot=%d\n", srv.sequence);
                    socket.send(identity, ZMQ.SNDMORE);
                    kvmsg kvmsg = new kvmsg(srv.sequence);
                    kvmsg.setKey("KTHXBAI");
                    kvmsg.setBody(subtree.getBytes());
                    kvmsg.send(socket);
                    kvmsg.destroy();
View Full Code Here

    {
        @Override
        public int handle(ZLoop loop, PollItem item, Object arg)
        {
            clonesrv5 srv = (clonesrv5) arg;
            Socket socket = item.getSocket();

            kvmsg msg = kvmsg.recv(socket);
            if (msg != null) {
                msg.setSequence(++srv.sequence);
                msg.send(srv.publisher);
View Full Code Here

    public static void main(String[] argv) throws Exception
    {
        Random rand = new Random(System.nanoTime());

        Context context = ZMQ.context(1);
        Socket server = context.socket(ZMQ.REP);
        server.bind("tcp://*:5555");

        int cycles = 0;
        while (true) {
            String request = server.recvStr();
            cycles++;

            //  Simulate various problems, after a few cycles
            if (cycles > 3 && rand.nextInt(3) == 0) {
                System.out.println("I: simulating a crash");
                break;
            } else if (cycles > 3 && rand.nextInt(3) == 0) {
                System.out.println("I: simulating CPU overload");
                Thread.sleep(2000);
            }
            System.out.printf("I: normal request (%s)\n", request);
            Thread.sleep(1000);              //  Do some heavy work
            server.send(request);
        }
        server.close();
        context.term();
    }
View Full Code Here

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

        //  Socket to talk to server
        Socket requester = context.socket(ZMQ.REQ);
        requester.connect("tcp://localhost:5559");
       
        System.out.println("launch and connect client.");

        for (int request_nbr = 0; request_nbr < 10; request_nbr++) {
            requester.send("Hello", 0);
            String reply = requester.recvStr(0);
            System.out.println("Received reply " + request_nbr + " [" + reply + "]");
        }
       
        //  We never get here but clean up anyhow
        requester.close();
        context.term();
    }
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.