本文整理匯總了Java中org.zeromq.ZMQ.poll方法的典型用法代碼示例。如果您正苦於以下問題:Java ZMQ.poll方法的具體用法?Java ZMQ.poll怎麽用?Java ZMQ.poll使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.zeromq.ZMQ
的用法示例。
在下文中一共展示了ZMQ.poll方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: receive
import org.zeromq.ZMQ; //導入方法依賴的package包/類
/**
* Block until a message can be read
*
* @return
* @throws IOException
*/
public Envelope receive(long timeout) throws IOException {
ZMQ.poll(items, timeout);
if (items[0].isReadable()) {
// System.out.println("Receiving");
// The DEALER socket gives us the address envelope and message
ZMsg msg = ZMsg.recvMsg(socket);
// System.out.println("Recvd " + msg);
ZFrame content = msg.getLast();
assert (content != null);
// System.out.println("Content " + content);
return Envelope.parseFrom(content.getData());
} else
return null; // timed out
}
示例2: execute
import org.zeromq.ZMQ; //導入方法依賴的package包/類
@Override
public void execute(ServiceContext serviceContext) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
Context context = ZMQ.context(1);
// Socket to talk to server
Socket responder = context.socket(ZMQ.REP);
responder.connect(zeroMqBrokerAddress);
ZMQ.PollItem items[] = {new ZMQ.PollItem(responder, ZMQ.Poller.POLLIN)};
while (!Thread.currentThread().isInterrupted() && !serviceContext.isCancelled()) {
// Wait for next request from client
int rc = ZMQ.poll(items, 1000);
if (rc == -1) {
continue;
}
if (items[0].isReadable()) {
String reqStr = responder.recvStr(0);
System.out.printf("Received request: [%s]\n", reqStr);
ValidateRequest req = objectMapper.readValue(reqStr, ValidateRequest.class);
ValidateResponse result = validateOperation(req.getAccount(), req.getSum());
System.out.printf("send response request: [%s]\n", result);
responder.send(objectMapper.writeValueAsString(result));
}
}
System.out.println("Stop async read!");
// We never get here but clean up anyhow
responder.close();
context.term();
}
示例3: sendRequest
import org.zeromq.ZMQ; //導入方法依賴的package包/類
@Override
public void sendRequest(String request) throws Exception {
int retriesLeft = REQUEST_RETRIES;
while (retriesLeft > 0 && !Thread.currentThread().isInterrupted()) {
LOGGER.info("Sending request " + request);
requester.send(request);
int expect_reply = 1;
while (expect_reply > 0) {
PollItem items[] = {new PollItem(requester, Poller.POLLIN)};
int rc = ZMQ.poll(items, REQUEST_TIMEOUT);
if (rc == -1) {
break; // Interrupted
}
if (items[0].isReadable()) {
try {
String response = requester.recvStr();
retriesLeft = 0;
expect_reply = 0;
LOGGER.info("Handling response");
this.handler.handle(response);
} catch (Exception e) {
LOGGER.warn("Error during request/response", e);
if (e instanceof ZMQException) {
LOGGER.error("Will close request socket");
requester.close();
ctx.close();
LOGGER.info("Closed request socket, which was connected to " + responder);
throw new Exception("Error during request/response", e);
}
}
} else if (--retriesLeft == 0) {
LOGGER.error("Service " + responder + " seems to be unavailable");
throw new Exception("Service " + responder + " seems to be unavailable");
} else {
LOGGER.warn("No response from " + responder);
requester.close();
LOGGER.info("Reconnecting request socket to " + responder);
connect();
}
}
}
}