use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.
the class TestZPoller method testPollerPollout.
@Test
public void testPollerPollout() throws IOException, InterruptedException {
final int port = Utils.findOpenPort();
final ZContext context = new ZContext();
final ZPoller poller = new ZPoller(context.createSelector());
final ZMQ.Socket receiver = context.createSocket(ZMQ.PULL);
final Server client = new Server(context, port);
client.start();
try {
receiver.connect("tcp://127.0.0.1:" + port);
final AtomicReference<ZMsg> msg = new AtomicReference<>();
poller.register(receiver, new EventsHandlerAdapter() {
@Override
public boolean events(Socket s, int events) {
if (receiver.equals(s)) {
msg.set(ZMsg.recvMsg(receiver));
return false;
} else {
return true;
}
}
}, ZPoller.IN);
int maxAttempts = 100;
while (!Thread.currentThread().isInterrupted() && maxAttempts-- > 0) {
int rc = poller.poll(1000);
if (rc < 0) {
break;
}
}
client.join();
Assert.assertNotNull("unable to receive msg after several cycles", msg);
} finally {
receiver.close();
context.close();
poller.close();
}
}
use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.
the class TestZStar method testNoStar.
@Test
public void testNoStar() {
System.out.print("No star: ");
ZStar.Fortune fortune = new BlackHole();
ZStar.Entourage entourage = new ZStar.Entourage() {
@Override
public void breakaleg(ZContext ctx, Fortune fortune, Socket phone, Object[] bags) {
// Crepi il lupo!
}
@Override
public void party(ZContext ctx) {
// right now there are some random closing issues
ZStar.party(30, TimeUnit.MILLISECONDS);
// waited a bit here seems to arrange that.
// no user penalty cost, the show is over.
}
};
ZStar star = new ZStar(fortune, "motdelafin", Arrays.asList("TEST", entourage).toArray());
ZMsg msg = star.recv();
Assert.assertNull("Able to receive a message from a black hole", msg);
boolean rc = star.sign();
Assert.assertFalse("Able to detect the presence of a black hole", rc);
rc = star.send("whatever");
Assert.assertFalse("Able to send a command to a black hole", rc);
// don't try it
// rc = star.pipe().send("boom ?!");
// star.retire();
System.out.println(".");
}
use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.
the class TestZThread method testDetached.
@Test
public void testDetached() {
final CountDownLatch stopped = new CountDownLatch(1);
ZThread.IDetachedRunnable detached = new ZThread.IDetachedRunnable() {
@Override
public void run(Object[] args) {
ZContext ctx = new ZContext();
assert (ctx != null);
Socket push = ctx.createSocket(ZMQ.PUSH);
assert (push != null);
ctx.close();
stopped.countDown();
}
};
ZThread.start(detached);
try {
stopped.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
}
use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.
the class msgqueue method main.
public static void main(String[] args) {
// Prepare our context and sockets
Context context = ZMQ.context(1);
// Socket facing clients
Socket frontend = context.socket(ZMQ.ROUTER);
frontend.bind("tcp://*:5559");
// Socket facing services
Socket backend = context.socket(ZMQ.DEALER);
backend.bind("tcp://*:5560");
// Start the proxy
ZMQ.proxy(frontend, backend, null);
// We never get here but clean up anyhow
frontend.close();
backend.close();
context.term();
}
use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.
the class mtrelay method main.
public static void main(String[] args) {
Context context = ZMQ.context(1);
// Bind to inproc: endpoint, then start upstream thread
Socket receiver = context.socket(ZMQ.PAIR);
receiver.bind("inproc://step3");
// Step 2 relays the signal to step 3
Thread step2 = new Step2(context);
step2.start();
// Wait for signal
receiver.recv(0);
receiver.close();
System.out.println("Test successful!");
context.term();
}
Aggregations