Is it safe to reuse messages without calling Reset()?

Options
allmhhuran
allmhhuran Member Posts: 41 ✭✭✭
edited August 2020 in PubSub+ Event Broker #1

Suppose I have a Publisher class which encapsulates the details of the solace broker. For any given application, it will receive an instance of a Publisher for a given topic to which it wants to send guaranteed messages.

This means that the IMessage properties Destination and DeliveryMode are guaranteed to be constant for the lifetime of a particular Publisher instance, and certain other default settings will also be constant (such as TimeToLive).

In the Solace PubSub+ docs I have seen advice that messages be reused. I take this to refer to the allocation and garbage collection advantage of not wrapping every individualISession.Send() in a using var message = ContextFactory.Instance.CreateMessage() block.

Is it safe to maintain a single instance of an IMesssage, and only change message specific properties between Send() operations (ie, obviously the BinaryAttachment, and things like ApplicationMessageID) without calling Reset() on the message?

This is more for code brevity than performance, since assigning the topic and delivery mode for each message are of course operations likely to take essentially no time. It would just eliminate a few apparently redundant assignments in the publisher code.

Tagged:

Comments

  • Aaron
    Aaron Member, Administrator, Moderator, Employee Posts: 527 admin
    Options

    Hey hey! @allmhhuran . I'm back from vacation, and I got an answer back from our Support team. Then I did a bit of back-and-forth with them. Anyhow, short answer is: yes, it is safe to use. Calling reset() is more of a convenience feature to allow the resetting of the message state back to default/uninitialized.

    The only thing I can think of is that if you get a NACK when publishing a Guaranteed message, and you've "overwritten" (essentially) the message, then you'll have to reconstruct it or something if you want to try resending.

  • allmhhuran
    allmhhuran Member Posts: 41 ✭✭✭
    Options

    Great, thanks for investigating that!