Java Code Examples for org.zeromq.ZMQ.Socket#setReceiveTimeOut()

The following examples show how to use org.zeromq.ZMQ.Socket#setReceiveTimeOut() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MsgExecutor.java    From aion_api with MIT License 6 votes vote down vote up
private void callbackRun(Context ctx) {
    Socket cbWorker = ctx.socket(ZMQ.DEALER);
    cbWorker.setReceiveTimeOut(RECVTIMEOUT);
    cbWorker.connect(CB_BIND_ADDR + addrBindNumber);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("connected!");
    }

    while (true) {
        byte[] rsp = cbWorker.recv(ZMQ.PAIR);
        if (this.running) {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace(
                        "recv msg: [{}]", (rsp != null ? IUtils.bytes2Hex(rsp) : "= null"));
            }
            process(rsp);
        } else {
            break;
        }
    }

    LOGGER.info("closing!");
    cbWorker.close();
    LOGGER.info("closed!");
}
 
Example 2
Source File: MsgExecutor.java    From aion_api with MIT License 4 votes vote down vote up
private void heartBeatRun(Context ctx) {
    Socket hbWorker = ctx.socket(ZMQ.DEALER);
    hbWorker.connect(HB_BIND_ADDR + addrBindNumber);
    hbWorker.setReceiveTimeOut(1000);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("hbWorker connected!");
    }

    int hbTolerance = HB_TOLERANCE;
    byte[] hbMsg = ApiUtils.toReqHeader(this.ver, Message.Servs.s_hb, Message.Funcs.f_NA);

    while (this.running && hbTolerance > 0) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("send hb!");
        }
        if (!hbWorker.send(hbMsg, ZMQ.DONTWAIT)) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("send heartbeat msg failed.");
            }
            continue;
        }

        byte[] rsp = hbWorker.recv(ZMQ.PAIR);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("recv msg: [{}]", (rsp != null ? IUtils.bytes2Hex(rsp) : "null"));
        }

        if (checkNotHbRspMsg(rsp)) {
            hbTolerance--;
        } else {
            hbTolerance = HB_TOLERANCE;
        }

        try {
            Thread.sleep(HB_POLL_MS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    if (this.running) {
        this.running = false;
        LOGGER.warn("timeout, disconnect the connection!");
        hbWorker.close();
        terminate();
        LOGGER.info("closed!");
    }
}
 
Example 3
Source File: MsgExecutor.java    From aion_api with MIT License 4 votes vote down vote up
private void workerRun(Context ctx) {
    Socket worker = ctx.socket(ZMQ.DEALER);
    worker.connect(WK_BIND_ADDR + addrBindNumber);
    worker.setReceiveTimeOut(RECVTIMEOUT);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("connected!");
    }

    while (true) {
        MsgReq msg = null;
        try {
            msg = queue.poll(RECVTIMEOUT, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (!this.running) {
            break;
        }

        if (msg != null && msg.req != null) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("poll q: [{}]", IUtils.bytes2Hex(msg.hash));
            }

            if (!worker.send(msg.req, ZMQ.PAIR)) {
                if (LOGGER.isErrorEnabled()) {
                    LOGGER.error("send msg failed. Msg: [{}]", IUtils.bytes2Hex(msg.req));
                }
                continue;
            }

            byte[] rsp = worker.recv(ZMQ.PAIR);
            if (this.running) {
                if (rsp == null) {
                    if (LOGGER.isErrorEnabled()) {
                        LOGGER.error("recv msg: [null]");
                    }
                    return;
                }

                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("recv msg: [{}]", IUtils.bytes2Hex(rsp));
                }
                process(rsp);
            } else {
                break;
            }
        }
    }

    LOGGER.info("closing!");
    worker.close();
    LOGGER.info("closed!");
}