[rabbitmq-discuss] Java RabbitMQ API bug in response.getHeaders()?

Mario Leyton mleyton at niclabs.cl
Thu Oct 27 19:44:10 BST 2011

On 10/27/11 2:49 PM, Tony Garnock-Jones wrote:
> Regarding the issue of the null check, I'd suggest looking at it
> differently: instead of seeing the null check as *polluting* your code,
> look at it as *paying attention to what the sender actually said*. The
> check is necessary to fully understand the intention of the sender as
> expressed using the facilities AMQP offers.
In that case I suggest you don't force your library users to assigning a
non-explicit semantic to Java's "null", and have to document this
non-explicit behavior in their code.

If you really think that checking the headers before accessing them is
an essential part of the AMQP specification (which I don't),  then an
alternative way could be to explicitly enforce this as:

Map getSentHeaders() throws HeadersNotSentException{...}

Which will throw a HeadersNotSentException  when the headers are not
present. This will still force me to add more code, but at least there
won't be an overloading of null, which is really unconfortable, and the
correct error message will be raised instead of the cryptic
NullPointerException later on the code when I actually use the header

To satisfy both users (those concerned or not with the distinction of
empty versus unsent headers you could actually provide two such methods
to access the headers:

public Map getHeaders() {}
public Map getSentHeaders() throws HeadersNotSentException{...}

Neither of them returning null.

Mario Leyton

More information about the rabbitmq-discuss mailing list