use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.
the class pathopub method main.
public static void main(String[] args) throws Exception {
ZContext context = new ZContext();
Socket publisher = context.createSocket(ZMQ.PUB);
if (args.length == 1)
publisher.connect(args[0]);
else
publisher.bind("tcp://*:5556");
// Ensure subscriber connection has time to complete
Thread.sleep(1000);
// Send out all 1,000 topic messages
int topicNbr;
for (topicNbr = 0; topicNbr < 1000; topicNbr++) {
publisher.send(String.format("%03d", topicNbr), ZMQ.SNDMORE);
publisher.send("Save Roger");
}
// Send one random update per second
Random rand = new Random(System.currentTimeMillis());
while (!Thread.currentThread().isInterrupted()) {
Thread.sleep(1000);
publisher.send(String.format("%03d", rand.nextInt(1000)), ZMQ.SNDMORE);
publisher.send("Off with his head!");
}
context.destroy();
}
use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.
the class flclient1 method tryRequest.
private static ZMsg tryRequest(ZContext ctx, String endpoint, ZMsg request) {
System.out.printf("I: trying echo service at %s...\n", endpoint);
Socket client = ctx.createSocket(ZMQ.REQ);
client.connect(endpoint);
// Send request, wait safely for reply
ZMsg msg = request.duplicate();
msg.send(client);
Poller poller = ctx.createPoller(1);
poller.register(client, Poller.POLLIN);
poller.poll(REQUEST_TIMEOUT);
ZMsg reply = null;
if (poller.pollin(0))
reply = ZMsg.recvMsg(client);
// Close socket in any case, we're done with it now
ctx.destroySocket(client);
return reply;
}
use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.
the class flserver1 method main.
public static void main(String[] args) {
if (args.length < 1) {
System.out.printf("I: syntax: flserver1 <endpoint>\n");
System.exit(0);
}
ZContext ctx = new ZContext();
Socket server = ctx.createSocket(ZMQ.REP);
server.bind(args[0]);
System.out.printf("I: echo service is ready at %s\n", args[0]);
while (true) {
ZMsg msg = ZMsg.recvMsg(server);
if (msg == null)
// Interrupted
break;
msg.send(server);
}
if (Thread.currentThread().isInterrupted())
System.out.printf("W: interrupted\n");
ctx.destroy();
}
use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.
the class lruqueue3 method run.
public void run() {
ZContext context = new ZContext();
// Prepare our context and sockets
Socket worker = context.createSocket(ZMQ.REQ);
worker.connect("ipc://backend.ipc");
ZFrame frame = new ZFrame(lruqueue3.LRU_READY);
// Tell backend we're ready for work
frame.send(worker, 0);
while (true) {
ZMsg msg = ZMsg.recvMsg(worker);
if (msg == null)
break;
msg.getLast().reset("OK".getBytes(ZMQ.CHARSET));
msg.send(worker);
System.out.println(Thread.currentThread().getName() + " Worker Sent OK");
}
context.destroy();
}
Aggregations