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;
}
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();
}
}
}
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) {
}
}
}
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();
}
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();
}
Aggregations