/** * Bind to network interface to a random port. Start listening for new connections. * * @param addr the endpoint to bind to. * @param min_port The minimum port in the range of ports to try. * @param max_port The maximum port in the range of ports to try. * @param max_tries The number of attempt to bind. */ public int bindToRandomPort(String addr, int min_port, int max_port, int max_tries) { int port; Random rand = new Random(); for (int i = 0; i < max_tries; i++) { port = rand.nextInt(max_port - min_port + 1) + min_port; try { bind(String.format("%s:%s", addr, port)); return port; } catch (ZMQException e) { if (e.getErrorCode() != ZMQ.EADDRINUSE()) { throw e; } continue; } } throw new ZMQException("Could not bind socket to random port.", (int) ZMQ.EADDRINUSE()); }
public ZMQException(ZMQException cause) { super(cause.getMessage(), cause); this.errorCode = cause.errorCode; }
private void mayRaise() { int errno = base.errno(); if (errno != 0 && errno != zmq.ZError.EAGAIN) { throw new ZMQException(errno); } }
if (e.getErrorCode() == ZMQ.Error.ETERM.getCode()) { break;
@Override public void start(Socket pipe, List<Socket> sockets, ZPoller poller) { boolean rc; try { rc = replies.bind(repliesAddress); assert (rc); Socket handler = sockets.get(0); rc = handler.bind("inproc://zeromq.zap.01"); assert (rc); rc = poller.register(handler, ZPoller.POLLIN); assert (rc); rc = pipe.send(OK); assert (rc); } catch (ZMQException e) { System.out.println("ZAuth: Error"); e.printStackTrace(); rc = pipe.send("ERROR"); assert (rc); } }
@Override public boolean configure(Properties properties) { try { listeningSocket = ctx.newSocket(method, type, listen); listeningSocket.setImmediate(false); listeningSocket.setReceiveTimeOut(-1); listeningSocket.setHWM(hwm); if(type == Sockets.SUB){ listeningSocket.subscribe(new byte[] {}); } boolean configured = super.configure(properties); stopPair = ctx.getPair(getName() + "/" + UUID.randomUUID()); return configured; } catch (org.zeromq.ZMQException e) { ZMQHelper.logZMQException(logger, "failed to start ZMQ input " + listen + ":", e); logger.catching(Level.DEBUG, e.getCause()); listeningSocket = null; return false; } }
private void mayRaise() { final int errno = socketBase.errno(); if (errno != 0 && errno != ZError.EAGAIN) { throw new ZMQException(errno); } }
@Override public void run() { LOG.info("Started Worker " + identity); worker = context.createSocket(ZMQ.DEALER); worker.setIdentity(identity.getBytes()); worker.connect(bindAddr); try { loop(); } catch (ZMQException e) { if (e.getErrorCode() != ZMQ.Error.ETERM.getCode()) throw e; } context.destroy(); LOG.info("Ended Reader Worker " + identity); }
/** * Bind to network interface to a random port. Start listening for new * connections. * * @param addr * the endpoint to bind to. * @param min * The minimum port in the range of ports to try. * @param max * The maximum port in the range of ports to try. */ public int bindToRandomPort(String addr, int min, int max) { int port; Random rand = new Random(); // int port = min; // while (port <= max) { for (int i = 0; i < 100; i++) { // hardcoded to 100 tries. should this be parametrised port = rand.nextInt(max - min + 1) + min; if (base.bind(String.format("%s:%s", addr, port))) { return port; } // port++; } throw new ZMQException("Could not bind socket to random port.", ZError.EADDRINUSE); }
@Override public void run() { LOG.info("Started Worker " + identity); worker = context.createSocket(ZMQ.DEALER); worker.setRcvHWM(2000); worker.setIdentity(identity.getBytes()); worker.connect(bindAddr); try { loop(); } catch (ZMQException e) { if (e.getErrorCode() != ZMQ.Error.ETERM.getCode()) throw e; } LOG.info("Ended Writer Worker " + identity); context.destroy(); }
public ZMQRestResponse process(ZMQRestRequest request){ final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<ZMQRestResponse> ref = new AtomicReference<ZMQRestResponse>(); this.restController.dispatchRequest(request, new RestChannel() { @Override public void sendResponse(RestResponse response) { try { if(logger.isTraceEnabled()){ logger.info("Response to ØMQ client: {}", new String(response.content())); } ref.set(convert(response)); } catch (IOException e) { // ignore } latch.countDown(); } }); try { latch.await(); return ref.get(); } catch (Exception e) { throw new ZMQException("failed to generate response", 0); } }
@Override public void run() { if (attachedRunnable != null) { try { attachedRunnable.run(args, ctx, pipe); } catch (ZMQException e) { if (e.getErrorCode() != Error.ETERM.getCode()) { throw e; } } ctx.destroy(); } else { detachedRunnable.run(args); } } }
private void raiseZMQException() { int errno = zmq.zmq_errno(); String reason = zmq.zmq_strerror(errno); throw new ZMQException(reason, errno); }
public static void logZMQException(Logger l, String prefix, RuntimeException e) { ERRNO errno; Supplier<String> message; if (e instanceof ZError.IOException) { IOException cause = (java.io.IOException) e.getCause(); errno = ERRNO.get(exccode(cause)); message = () -> errno.toString(prefix, e); } else if (e instanceof ZError.CtxTerminatedException) { errno = ERRNO.ETERM; message = () -> errno.toString(prefix, e, new RuntimeException("Context terminated")); } else if (e instanceof ZError.InstantiationException) { errno = ERRNO.EOTHER; message = () -> errno.toString(prefix, e, e.getCause()); } else if (e instanceof ZMQException) { errno = ERRNO.get(((ZMQException)e).getErrorCode()); message = () -> errno.toString(prefix, e); } else { throw e; } l.log(errno.level, message); }
private void raiseZMQException() { int errno = zmq.zmq_errno(); String reason = zmq.zmq_strerror(errno); throw new ZMQException(reason, errno); }
if (e.getErrorCode() == ZError.ETERM) { break;
private void raiseZMQException() { int errno = zmq.zmq_errno(); String reason = zmq.zmq_strerror(errno); throw new ZMQException(reason, errno); }
private void raiseZMQException() { int errno = zmq.zmq_errno(); String reason = zmq.zmq_strerror(errno); throw new ZMQException(reason, errno); }
throw new ZMQException(errno);