Messages sent to Topic are getting lost if no subs

2019-08-02 01:55发布

问题:

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

回答1:

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
}