Azure Service Bus - Message Peek on Subscription

1.9k views Asked by At

I have a listen access on Topic/Subscription for Azure Service Bus. Is there a way to verify if a subscription has a message and how many messages are there. I don't have manage connection string and I think I cannot run Service Bus Explorer. I don't intent to read the message out of the subscription.

2

There are 2 answers

0
Tom Sun On BEST ANSWER

I don't intent to read the message out of the subscription.

The short answer is no. Manage access is required to get the topic or subscription message information.

If you want to get the subscription message count, you could have a try following 2 ways:

1.You have manage access, you could use the following demo code to do that.

var topicName = "topic1";
var subscriptionName = "subscription";
var address = "sb://xxx.servicebus.windows.net/"; //base address of namespace you are connecting to.
MessagingFactorySettings MsgFactorySettings = new MessagingFactorySettings
 {
     TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("SharedAccessKeyName", "xxxxxx"),
 };
MessagingFactory msgFactory = MessagingFactory.Create(address, MsgFactorySettings);
NamespaceManager manager = new NamespaceManager(msgFactory.Address,msgFactory.GetSettings().TokenProvider);
var subscriptioncDescription = manager.GetSubscription(topicName, subscriptionName);
var count = subscriptioncDescription.MessageCountDetails.ActiveMessageCount;

2.Use Azure Microsoft.Azure.Management.ServiceBus.Fluent sdk. You need to registry Azure AD application and assign corresponding role it. For more information please refer to another SO thread.

 var subscriptionId = "Azure subscriptionId";
 var resourceGroupName = "rgName";
 var nameSpace = "tomtestsb";
 var topicName = "topic1";
 var subscriptionName = "subscription";
 var credentials = SdkContext.AzureCredentialsFactory.FromFile(@"D:\Tom\Documents\azureCred.txt");
 var client = new ServiceBusManagementClient(credentials);
 client.SubscriptionId = subscriptionId;
 var topic = client.Topics.GetAsync(resourceGroupName, nameSpace, topicName).Result;
 var topicMessagecount = topic.CountDetails.ActiveMessageCount;
 var subscription = client.Subscriptions.GetAsync(resourceGroupName, nameSpace, topicName, subscriptionName).Result;
 var subscriptionMessagecount = subscription.CountDetails.ActiveMessageCount;
0
Arunprabhu On

The count of the messages in a Topic Subscription can be retrieved only using Manage Connection string. With the Listen Connection string, you cannot read the properties or count of messages.

But, you can peek the messages in the Subscription using Listen Connection string. Peeking the messages will not lock or remove the messages from the Subscription. You can peek the messages as many times as you need.

Thus, with the help of Listen Connection string, you can find the number of messages in a Subscription by peeking the messages one by one inside a while loop and add a counter inside the loop. The counter value is the number of messages inside the Subscription.