2

In Azure Service Bus, if below is the sequence of events, then all's fine -

  1. Create Topic
  2. Create Subscriptions inside Topic
  3. Send Messages to Topic

With above, the subscriptions are triggered when a message is sent. This is expected.

However, if we modify the above sequence like this

  1. Create Topic
  2. Send Messages to Topic
  3. Create Subscriptions inside Topic

In this case, as messages are sent to a topic whilst no subscriptions were in place, when the subscriptions are indeed created, the previously sent messages don't show up in their list. Those messages are essentially 'lost'. Am not able to see those messages in Service Bus Explorer too.

The above sequence flow is relevant because we have detached publishers and subscribers, where the publisher just sends a message and subscribers, when they come online, create the subscriptions and handle them. The order in which the publisher and subscriber come online is not guaranteed.

How can I access/process messages sent to the topic before the subscriptions are created? What happens to such messages in the first place?

Thanks

sppc42
  • 2,994
  • 2
  • 31
  • 49
  • 1
    Correct, this is the "expected" behavior, I believe it's by design. In our app new subscriptions can be added quite frequently in any of the new deployments, so we are dealing with this by being very careful and deliberate at what parts of the app get deployed first. I.e. the message subscribers need to be there ahead of the senders. I don't have an easy solution / answer to overcoming this. Only idea: you can try adding custom implementation to check if subscription is there before sending. But you'll likely need to optimize it or it will be a big performance penalty to do it every time. – Slava Asipenko Nov 15 '16 at 15:39

1 Answers1

4

It turns out that the above behavior is by design - if no subscriptions are there, then the message is lost.

To overcome this, Azure Service Bus provides a property on topic to enable the pre-filtering of messages before they are sent. So, if no filters/subscriptions are available, it'll throw an exception

Set the option on the Topic

namespaceManager.CreateTopicAsync(new TopicDescription(topicName)
{
    EnableFilteringMessagesBeforePublishing = true
});

Whilst sending the message, check for exception

try
{
    await topicClient.SendAsync(brokeredMessage);
}
catch (NoMatchingSubscriptionException ex)
{
 // handle the exception, maybe send it to dead letter queue using DeadLetterAsync
}
sppc42
  • 2,994
  • 2
  • 31
  • 49
  • 3
    Please note that this feature is only for use in development and testing, and it not meant to be used in production. https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-performance-improvements#development--testing-features – JTaub Nov 16 '16 at 15:57
  • Taking in consideration, the above comment about not using EnableFilteringMessagesBeforePublishing in production, there is no real solution for this. – paul van bladel Apr 21 '21 at 05:53