0
votes

I have a self-hosted NSB process that may start publishing events as soon as it is started. In my observation, when the internal NSB runtime starts up, it will start processing subscription requests on a separate thread (not the one where a publish request is received) with a delay of a couple of seconds. Then in this scenario, the new subscribers, although having their subscription request submitted before the "opening of the store", will still miss the first few publishes after "the store is opened".

How can I force NSB to do the first publish only after having completed all subscription requests already submitted in queue at the start of the process?

2

2 Answers

1
votes

Race conditions like this needs to be handled by "priming" the subscription storage with the subscription before the subscriber comes online.

In short: insert the row/document straight into the subscription database

0
votes

I would prefer @Gary Zhang's idea of MessageDrivenSubscriptionManager. While that's not still available(in v5).

This is my solution.

// Wait for the thread that handles subscription messages to finish before publishing anything to remove risk of missing subscibers  

StringBuilder QueueName = new StringBuilder(Environment.MachineName); 
QueueName.Append("\\private$\\"); 
QueueName.Append(MethodBase.GetCurrentMethod().DeclaringType.Namespace.ToLower()); 

using (System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(QueueName.ToString())) 
{ 
    while (queue.GetAllMessages().Length > 0) 
    { 
        Thread.Sleep(1000); 
        } 
}