Search in sources :

Example 1 with Context

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

the class TestZMQ method testPollerPollout.

@Test
public void testPollerPollout() throws Exception {
    int port = Utils.findOpenPort();
    ZMQ.Context context = ZMQ.context(1);
    Client client = new Client(context, port);
    //  Socket to send messages to
    ZMQ.Socket sender = context.socket(ZMQ.PUSH);
    sender.bind("tcp://127.0.0.1:" + port);
    ZMQ.Poller outItems;
    outItems = context.poller();
    outItems.register(sender, ZMQ.Poller.POLLOUT);
    while (!Thread.currentThread().isInterrupted()) {
        outItems.poll(1000);
        if (outItems.pollout(0)) {
            sender.send("OK", 0);
            System.out.println("ok");
            break;
        } else {
            System.out.println("not writable");
            client.start();
        }
    }
    client.join();
    sender.close();
    context.term();
}
Also used : Socket(org.zeromq.ZMQ.Socket) Context(org.zeromq.ZMQ.Context) Test(org.junit.Test)

Example 2 with Context

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

the class TestZMQ method testEventBindFailed.

@Test
public void testEventBindFailed() {
    Context context = ZMQ.context(1);
    ZMQ.Event event;
    Socket helper = context.socket(ZMQ.REP);
    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_BIND_FAILED));
    monitor.connect("inproc://monitor.socket");
    try {
        socket.bind("tcp://127.0.0.1:" + port);
    } catch (ZMQException ex) {
    }
    event = ZMQ.Event.recv(monitor);
    assertNotNull("No event was received", event);
    assertEquals(ZMQ.EVENT_BIND_FAILED, event.getEvent());
    helper.close();
    socket.close();
    monitor.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 3 with Context

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

the class TestZMQ method testEventAccepted.

@Test
public void testEventAccepted() {
    Context context = ZMQ.context(1);
    ZMQ.Event event;
    Socket socket = context.socket(ZMQ.REP);
    Socket monitor = context.socket(ZMQ.PAIR);
    Socket helper = context.socket(ZMQ.REQ);
    monitor.setReceiveTimeOut(100);
    assertTrue(socket.monitor("inproc://monitor.socket", ZMQ.EVENT_ACCEPTED));
    monitor.connect("inproc://monitor.socket");
    int port = socket.bindToRandomPort("tcp://127.0.0.1");
    helper.connect("tcp://127.0.0.1:" + port);
    event = ZMQ.Event.recv(monitor);
    assertNotNull("No event was received", event);
    assertEquals(ZMQ.EVENT_ACCEPTED, event.getEvent());
    helper.close();
    socket.close();
    monitor.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 4 with Context

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

the class TestZMQ method testByteBufferRecv.

@Test
public void testByteBufferRecv() throws InterruptedException, IOException, CharacterCodingException {
    int port = Utils.findOpenPort();
    ZMQ.Context context = ZMQ.context(1);
    ByteBuffer bb = ByteBuffer.allocate(6).order(ByteOrder.nativeOrder());
    ZMQ.Socket push = null;
    ZMQ.Socket pull = null;
    try {
        push = context.socket(ZMQ.PUSH);
        pull = context.socket(ZMQ.PULL);
        pull.bind("tcp://*:" + port);
        push.connect("tcp://localhost:" + port);
        push.send("PING".getBytes(ZMQ.CHARSET), 0);
        pull.recvByteBuffer(bb, 0);
        bb.flip();
        byte[] b = new byte[bb.remaining()];
        bb.duplicate().get(b);
        assertEquals("PING", new String(b, ZMQ.CHARSET));
    } finally {
        try {
            push.close();
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }
        try {
            pull.close();
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }
        try {
            context.term();
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }
    }
}
Also used : Socket(org.zeromq.ZMQ.Socket) Context(org.zeromq.ZMQ.Context) ByteBuffer(java.nio.ByteBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with Context

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

the class TestZMQ method testEventConnectRetried.

@Test
public void testEventConnectRetried() throws InterruptedException, IOException {
    Context context = ZMQ.context(1);
    ZMQ.Event event;
    Socket socket = context.socket(ZMQ.REP);
    Socket monitor = context.socket(ZMQ.PAIR);
    monitor.setReceiveTimeOut(100);
    assertTrue(socket.monitor("inproc://monitor.socket", ZMQ.EVENT_CONNECT_RETRIED));
    monitor.connect("inproc://monitor.socket");
    int randomPort = Utils.findOpenPort();
    socket.connect("tcp://127.0.0.1:" + randomPort);
    // on windows, this is required, otherwise test fails
    Thread.sleep(1000L);
    event = ZMQ.Event.recv(monitor);
    assertNotNull("No event was received", event);
    assertEquals(ZMQ.EVENT_CONNECT_RETRIED, event.getEvent());
    socket.close();
    monitor.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Aggregations

Context (org.zeromq.ZMQ.Context)35 Test (org.junit.Test)33 Socket (org.zeromq.ZMQ.Socket)33 IOException (java.io.IOException)5 ByteBuffer (java.nio.ByteBuffer)4 CharacterCodingException (java.nio.charset.CharacterCodingException)4 ExecutorService (java.util.concurrent.ExecutorService)3 ZFrame (org.zeromq.ZFrame)2 ZMsg (org.zeromq.ZMsg)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1 Ignore (org.junit.Ignore)1