Skip to content

Instantly share code, notes, and snippets.

@stofte
Created October 23, 2017 12:59
Show Gist options
  • Save stofte/03d6799ba665069aee343d594693aea4 to your computer and use it in GitHub Desktop.
Save stofte/03d6799ba665069aee343d594693aea4 to your computer and use it in GitHub Desktop.
Dump deadletter queues in Azure Service Bus
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpQueues
{
class Program
{
static void Main(string[] args)
{
var connStr = "Endpoint=sb://blablabla/;SharedAccessKeyName=foo;SharedAccessKey=bar";
var topicPrefix = "myprefix-";
string connectionString = ConfigurationManager.AppSettings["connectionString"];
ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connStr);
MessagingFactory factory = MessagingFactory.CreateFromConnectionString(builder.ToString());
new List<SubscriptionDescription>();
var namespaceManager = NamespaceManager.CreateFromConnectionString(connStr);
foreach(var topic in namespaceManager.GetTopics())
{
if (topic.Path.StartsWith(topicPrefix))
{
Console.WriteLine("Emptying topic {0}", topic.Path);
foreach(var sub in namespaceManager.GetSubscriptions(topic.Path))
{
EmptySubscription(factory, topic.Path, sub.Name);
}
}
}
}
static void EmptySubscription(MessagingFactory factory, string path, string name)
{
SubscriptionClient deadletterClient = factory.CreateSubscriptionClient(path, name + "/$DeadLetterQueue", ReceiveMode.ReceiveAndDelete);
deadletterClient.PrefetchCount = 10000;
int count = 0;
while (true)
{
var msg = deadletterClient.Peek();
if (msg == null)
{
break;
}
IEnumerable<BrokeredMessage> msgs = deadletterClient.ReceiveBatch(10000, new TimeSpan(1));
if (msgs != null && msgs.Count() > 0)
{
count += msgs.Count();
}
}
Console.WriteLine("Emptied {0} from {1}", count, name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment