Search code examples
javazeromqjzmq

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.


Solution

  • 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 (e.g. 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.