Search in sources :

Example 21 with Context

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

the class ZContext method getContext.

/**
     * @return the context
     */
public Context getContext() {
    Context result = context;
    if (result == null) {
        synchronized (this) {
            result = context;
            if (result == null) {
                result = ZMQ.context(ioThreads);
                context = result;
            }
        }
    }
    return result;
}
Also used : Context(org.zeromq.ZMQ.Context)

Example 22 with Context

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

the class TestZMQ method testByteBufferLargeDirect.

@Test
public void testByteBufferLargeDirect() throws InterruptedException, IOException, CharacterCodingException {
    int port = Utils.findOpenPort();
    ZMQ.Context context = ZMQ.context(1);
    int[] array = new int[2048 * 2000];
    for (int i = 0; i < array.length; ++i) {
        array[i] = i;
    }
    ByteBuffer bSend = ByteBuffer.allocateDirect(Integer.SIZE / 8 * array.length).order(ByteOrder.nativeOrder());
    bSend.asIntBuffer().put(array);
    ByteBuffer bRec = ByteBuffer.allocateDirect(bSend.capacity()).order(ByteOrder.nativeOrder());
    int[] recArray = new int[array.length];
    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.sendByteBuffer(bSend, 0);
        pull.recvByteBuffer(bRec, 0);
        bRec.flip();
        bRec.asIntBuffer().get(recArray);
        assertArrayEquals(array, recArray);
    } 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 23 with Context

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

the class TestZMQ method testByteBufferSend.

@Test
public void testByteBufferSend() throws InterruptedException, IOException {
    int port = Utils.findOpenPort();
    ZMQ.Context context = ZMQ.context(1);
    ByteBuffer bb = ByteBuffer.allocate(4).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);
        bb.put("PING".getBytes(ZMQ.CHARSET));
        bb.flip();
        push.sendByteBuffer(bb, 0);
        String actual = new String(pull.recv(), ZMQ.CHARSET);
        assertEquals("PING", actual);
    } finally {
        try {
            push.close();
        } catch (Exception ignore) {
        }
        try {
            pull.close();
        } catch (Exception ignore) {
        }
        try {
            context.term();
        } catch (Exception ignore) {
        }
    }
}
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 24 with Context

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

the class TestZMQ method testEventListening.

@Test
public void testEventListening() {
    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_LISTENING));
    monitor.connect("inproc://monitor.socket");
    socket.bindToRandomPort("tcp://127.0.0.1");
    event = ZMQ.Event.recv(monitor);
    assertNotNull("No event was received", event);
    assertEquals(ZMQ.EVENT_LISTENING, event.getEvent());
    socket.close();
    monitor.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 25 with Context

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

the class TestZMQ method testEventMonitorStopped.

@Test
public void testEventMonitorStopped() {
    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_MONITOR_STOPPED));
    monitor.connect("inproc://monitor.socket");
    socket.monitor(null, 0);
    event = ZMQ.Event.recv(monitor);
    assertNotNull("No event was received", event);
    assertEquals(ZMQ.EVENT_MONITOR_STOPPED, 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