amqp / rhea

A reactive messaging library based on the AMQP protocol
Apache License 2.0
273 stars 80 forks source link

Is it possible to accept null or undefined message body #387

Closed tobias-zeptio closed 1 year ago

tobias-zeptio commented 1 year ago

We are messaging between different applications, and have message without a body defined. Today I noticed that it's not possible to send message using rhea with a undefined body.

After some research into AMQP 1.0 specification I'm still uncertain if it's valid or not. Would it be possible to accept undefined as a body value in rhea?

tobias-zeptio commented 1 year ago

Reading section 3.2 in https://www.amqp.org/sites/amqp.org/files/amqp.pdf the body is defined as follows:

But we use ActiveMQ Artemis as our broker and that accepts a undefined body.

grs commented 1 year ago

A message with no body would in AMQP 1.0 be encoded as a body with a single amqp-value section that held a null value. What is the client that is producing the message with an undefined body?

tobias-zeptio commented 1 year ago

The Java library is org.apache.qpid.proton-j version 0.33.2, where it's possible to set message body as null.

Digging deeper into the proton code I arrive at this method, in AmqpJmsTextMessageFacade and AmqpValue.class. Here value can be null.

class AmqpValue {
    ...
    public String toString()
    {
        return "AmqpValue{" + _value + '}';
    }
    ...
}

So the issue might be that undefined and null are not the same. undefined would mean no body, which is not supported. But if Is set the type in rhea as follows, it will accept sending a message body with value null.

interface Message<T = any> {
  body: T | null;
}
grs commented 1 year ago

I now realise I misunderstood the original question, sorry! (I thought by 'accept' you meant able to receive).

For sending, rhea should do the right thing if body is null or undefined.

Is the issue with the typescript definitions?

tobias-zeptio commented 1 year ago

I was confused myself, so this issue is now resolved. The issue was that I mixed up undefined and null.

Setting the body type to T | null works, so no update required in the library.