简体   繁体   中英

How to handle errors with JZMQ?

The documentation for Socket#recv() reads:

Returns: [...] null on error.

How can I tell what the error was? I want to handle EAGAIN specifically.

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."

I followed the method calls and arrived at do_read in Socket.cpp where it gets interesting on line 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).
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?

On a side note, support for the EAGAIN error code was added in this commit on May 15, 2015.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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