How to purge messages for Service Bus Topic Subscription

a 夏天 提交于 2019-12-10 17:14:08

问题


Just wondering the best way (even if via Portal, Powershell, or C#) to purge the messages off of a Service Bus Topic's Subscription.

Imagine we have a topic with 4 subscriptions, and we only want to purge the messages from one of the subscriptions.

I have a feeling the only way may be to read the messages in a while loop, but hoping for something better.

UPDATE:

Apart from using code, you can use the Server Explorer as suggested in the answer - right click subscription and purge messages:


回答1:


You can most certainly do it via code. If you're using Service Bus SDK, you could do something like the following:

    static void PurgeMessagesFromSubscription()
    {
        var connectionString = "Endpoint=sb://account-name.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=access key";
        var topic = "topic-name";
        var subscription = "subscription-name";
        int batchSize = 100;
        var subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionString, topic, subscription, ReceiveMode.ReceiveAndDelete);
        do
        {
            var messages = subscriptionClient.ReceiveBatch(batchSize);
            if (messages.Count() == 0)
            {
                break;
            }
        }
        while (true);
    }

What this code will do is fetch messages from the subscription (100 at a time) in Receive & Delete mode so that as soon as messages are fetched, they are deleted from the subscription automatically.

I believe Service Bus Explorer tool also has the capability to purge messages. You can use that as well instead of writing the code.




回答2:


If you have a lot of messages and can tolerate a bit of downtime on subscriber side, it might be faster to just drop the subscription and create a new one with the same name.



来源:https://stackoverflow.com/questions/46922689/how-to-purge-messages-for-service-bus-topic-subscription

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!