rguerreiro
rguerreiro

Reputation: 2683

How to get the total number of subscribers in NServiceBus?

I'm using NServiceBus and I need to know how many clients are subscribed to a specific message type (even better the names of the subscribers). I'm talking in a pub\sub scenario.

Is it possible to get this information in NServiceBus?

Thx

Upvotes: 1

Views: 503

Answers (2)

Morten Teinum
Morten Teinum

Reputation: 26

I have used ISubscriptionStorage with success.

public class SubscribersForMessageHandler :
             IHandleMessages<SubscribersForMessageRequest>
{
    public ISubscriptionStorage Storage { get; set; }
    public IBus Bus { get; set; }

    public void Handle(SubscribersForMessageRequest message)
    {
        Bus.Reply<SubscribersForMessageResponse>(m=>
        {
            m.SagaId = message.SagaId;
            m.MessageType = message.MessageType;
            m.SubscriberEndpoints = GetSubscribersForMessage(message.MessageType);
        });
    }

    private List<string> GetSubscribersForMessage(string type)
    {
        return Storage.GetSubscribersForMessage(
          new List<string> { type }).ToList();
    }
}

Upvotes: 1

Adam Fyles
Adam Fyles

Reputation: 6050

You can pull this right out of your subscription storage. Either a query to the database or a .GetAllMessages() on the queue will get you a count and the subscribers address. If you are looking to do this in code, you could write a handler for the subscription message and count them up that way.

Upvotes: 3

Related Questions