简体   繁体   English

如何使用JZMQ处理错误?

[英]How to handle errors with JZMQ?

The documentation for Socket#recv() reads: Socket#recv()文档为

Returns: [...] null on error. 返回:错误时返回[...] null。

How can I tell what the error was? 我怎么知道错误是什么? I want to handle EAGAIN specifically. 我想专门处理EAGAIN

I have very limited knowledge here but from the looks of it, the answer could be: 我在这里的知识非常有限,但是从它的外观来看,答案可能是:
"If Socket#recv() returns null and no ZMQException was thrown, an EAGAIN error occurred." “如果Socket#recv()返回null,并且未引发ZMQException则发生EAGAIN错误。”

I followed the method calls and arrived at do_read in Socket.cpp where it gets interesting on line 83 : 我跟随方法调用,到达Socket.cpp中的do_read ,在第83行它变得很有趣:

rc = zmq_recv (socket, message, flags);
int err = zmq_errno();
if (rc < 0 && err == EAGAIN) {
    rc = zmq_msg_close (message);
    err = zmq_errno();
    if (rc != 0) {
        raise_exception (env, err);
        return NULL;
    }
    return NULL;
}
if (rc < 0) {
    raise_exception (env, err);
    rc = zmq_msg_close (message);
    err = zmq_errno();
    if (rc != 0) {
        raise_exception (env, err);
        return NULL;
    }
    return NULL;
}
return message;

What I read here is that if something goes wrong, you get an ZMQException in Java unless the error was EAGAIN and zmq_msg_close does not go wrong (I am not sure what zmq_msg_close does, but I assume it rarely goes wrong). 我在这里读到的是,如果出现问题,除非错误是EAGAIN并且zmq_msg_close不会出错,否则您将在Java中获得ZMQException (我不确定zmq_msg_close做什么,但我认为它很少出错)。
But I don't have the environment to test this and I also don't really understand how raise_exception works (source in util.cpp ): what happens if two exceptions are raised/thrown in the same code-path (eg when err is not EAGAIN and rc < 0 ) and you can only catch one runtime-exception in Java? 但是我没有环境对此进行测试,而且我也并不太真正地理解raise_exception工作原理( util.cpp中的代码 ):如果在同一代码路径中引发/抛出两个异常(例如,当err为不是EAGAIN和rc < 0 ),并且您只能在Java中捕获一个运行时异常?

On a side note, support for the EAGAIN error code was added in this commit on May 15, 2015. 附带说明, 此提交于2015年5月15日添加了对EAGAIN错误代码的支持。

source code are: 源代码是:

    /**
         * Receive a message.
         *
         * @return the message received, as an array of bytes; null on error.
         */ 
        public final byte[] recv()
        {
            return recv(0);
        }

        /**
         * Receive a message.
         *
         * @param flags
         *            the flags to apply to the receive operation.
         * @return the message received, as an array of bytes; null on error.
         */
        public final byte[] recv(int flags)
        {
            zmq.Msg msg = base.recv(flags);

            if (msg != null) {
                return msg.data();
            }

            mayRaise();
            return null;
        }

        private void mayRaise()
        {
            int errno = base.errno();
            if (errno != 0 && errno != zmq.ZError.EAGAIN) {
                throw new ZMQException(errno);
            }
        } 

so you can change recv(int flags) and mayRaise() function 因此您可以更改recv(int标志)和mayRaise()函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM