Package org.zeromq.ZMQ

Examples of org.zeromq.ZMQ.Socket


        }
    }
    @Test
    public void testPollerUnregister() {
        Context context = ZMQ.context(1);
        Socket socketOne = context.socket(ZMQ.SUB);
        Socket socketTwo = context.socket(ZMQ.REP);
        Poller poller = new ZMQ.Poller(2);
        poller.register(socketOne, ZMQ.Poller.POLLIN);
        poller.register(socketTwo, ZMQ.Poller.POLLIN);

        socketOne.setLinger(0);
        socketOne.close();
        socketTwo.setLinger(0);
        socketTwo.close();

        poller.unregister(socketOne);
        poller.unregister(socketTwo);
       
        context.term();
View Full Code Here


    @Test(expected = ZMQException.class)
    public void testPollingInvalidSockets() {
        Context context = ZMQ.context(1);
        Poller poller = new ZMQ.Poller(1);
        Socket socketOne = context.socket(ZMQ.SUB);

        poller.register(socketOne, ZMQ.Poller.POLLIN);
        socketOne.close();
        poller.poll(100);

        context.term();
    }
View Full Code Here

            return;
       
        Context context = ZMQ.context(1);
        Event event;
       
        Socket helper = context.socket(ZMQ.REQ);
        int port = helper.bindToRandomPort("tcp://127.0.0.1");
       
        Socket socket = context.socket(ZMQ.REP);
        Socket monitor = context.socket(ZMQ.PAIR);
        monitor.setReceiveTimeOut(100);
       
        assertTrue(socket.monitor("inproc://monitor.socket", ZMQ.EVENT_CONNECTED));
        monitor.connect("inproc://monitor.socket");
       
        socket.connect("tcp://127.0.0.1:" + port);
        event = Event.recv(monitor);
        assertNotNull("No event was received", event);
        assertEquals(ZMQ.EVENT_CONNECTED, event.getEvent());
       
        helper.close();
        socket.close();
        monitor.close();
        context.term();
    }
View Full Code Here

    public Socket createSocket(int type) {
        if (context == null)
            context = ZMQ.context(ioThreads);

        // Create and register socket
        Socket socket = context.socket(type);
        sockets.add(socket);
        return socket;
    }
View Full Code Here

    @Override
    public void run()
    {
        String workerBind = "inproc://reader-worker";

        Socket router = context.createSocket(ZMQ.ROUTER);
        Socket inrouter = context.createSocket(ZMQ.ROUTER);

        router.setSendBufferSize(sendBufferSize);
        router.setEncoder(PersistEncoder.class);

        inrouter.bind(workerBind);

        for (int i = 0; i < numWorkers; i++) {
            String id = String.valueOf(i);
            ZPReaderWorker worker = new ZPReaderWorker(context, workerBind, id);
            worker.start();
View Full Code Here

    @Override
    public void run()
    {
        String workerBind = "inproc://writer-worker";

        Socket router = context.createSocket(ZMQ.ROUTER);
        Socket inrouter = context.createSocket(ZMQ.ROUTER);
        router.setRcvHWM(recvHWM);
        router.setRouterMandatory(true);
        router.setRouterHandlover(true);
        router.setReceiveBufferSize(recvBufferSize);
        router.setMaxMsgSize(maxMessageSize);
        inrouter.setRouterMandatory(true);

        if (decoder)
            router.setDecoder(PersistDecoder.class);
        inrouter.setRouterMandatory(true);

        inrouter.bind(workerBind);

        for (int i = 0; i < numWorkers; i++) {
            String id = String.valueOf(i);
            ZPWriterWorker worker = new ZPWriterWorker(context, workerBind, id, decoder);
            worker.start();
View Full Code Here

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

        ZLog zlog = ZLogManager.instance().get(topic);
        SegmentInfo[] infos = zlog.segments();

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

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

        //  Earliest offset
        sock.sendMore("OFFSET");
        sock.send(ByteBuffer.allocate(8).putLong(-2).array());

        Msg status = sock.base().recv(0);
        assertTrue(status.hasMore());
        assertEquals(100, status.data()[0]);

        Msg oldest = sock.base().recv(0);
        assertFalse(oldest.hasMore());
        assertEquals(infos[0].start(), oldest.buf().getLong());

        //  Latest offset
        sock.sendMore("OFFSET");
        sock.send(ByteBuffer.allocate(8).putLong(-1).array());

        status = sock.base().recv(0);
        assertTrue(status.hasMore());
        assertEquals(100, status.data()[0]);

        Msg latest = sock.base().recv(0);
        assertTrue(latest.hasMore());
        assertEquals(infos[infos.length - 1].start(), latest.buf().getLong());

        Msg last = sock.base().recv(0);
        assertFalse(last.hasMore());
        assertEquals(infos[infos.length - 1].offset(), last.buf().getLong());

        //  Modified before
        sock.sendMore("OFFSET");
        sock.send(ByteBuffer.allocate(8).putLong(System.currentTimeMillis()).array());

        status = sock.base().recv(0);
        assertTrue(status.hasMore());
        assertEquals(100, status.data()[0]);

        List<Long> offsetList = new ArrayList<Long>();

        while (true) {
            Msg msg = sock.base().recv(0);
            offsetList.add(msg.buf().getLong());
            if (!msg.hasMore())
                break;
        }
        assertEquals(infos.length + 1, offsetList.size());
        for (int i = 0; i < infos.length; i++) {
            assertEquals(infos[i].start(), (long) offsetList.get(i));
        }
        assertEquals(infos[infos.length - 1].offset(), (long) offsetList.get(infos.length));


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

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

        ZLog zlog = ZLogManager.instance().get(topic);
        SegmentInfo[] infos = zlog.segments();
        SegmentInfo last = infos[infos.length - 1];

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

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

        //  Latest offset
        sock.sendMore("FETCH");
        sock.sendMore(ByteBuffer.allocate(8).putLong(last.start()).array());
        sock.send(ByteBuffer.allocate(8).putLong(Integer.MAX_VALUE).array());

        Msg status = sock.base().recv(0);
        assertEquals(100, status.data()[0]);

        Msg result = sock.base().recv(0);
        assertEquals((int) (last.offset() - last.start()), result.size());

        Iterator<Msg> it = new MsgIterator(result.buf(), false);
        while (it.hasNext()) {
            it.next();
        }

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

    @Test
    public void testError() throws Exception
    {

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

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

        Thread.sleep(1000);
        router.sendMore("A");
        router.sendMore(new byte[] {Persistence.MESSAGE_ERROR});
        router.send(new byte[] {Persistence.STATUS_INTERNAL_ERROR});

        assertEquals(dealer.recv()[0], Persistence.STATUS_INTERNAL_ERROR);

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

    @Test
    public void testResponse() throws Exception
    {

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

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

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

        assertEquals(dealer.recv()[0], STATUS_OK);
        assertEquals(dealer.recv().length, 8);

        dealer.close();
        router.close();
        ctx.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.