This is the second part of my blog series on the recently released Service Bus preview features.
[This post series is based on preview features that are subject to change]
Auto-Expiration
There’s a new property, AutoDeleteOnIdle, added to Service Bus entity descriptions such as QueueDescription, TopicDescription, and SubscriptionDescription. When this property is set to a TimeSpan (from 1 minute to TimeSpan.MaxValue), the corresponding entity will be automatically deleted after being inactive during the given time period. AutoDeleteOnIdle attribute can make entity managements a lot easier in many cases. For instance, a chat server won’t need to monitor chat room activities to clean up topics and subscriptions it may have created for a chat room – the entities clean themselves up when the chat room becomes inactive. Now let’s walk through a sample to see how this property works.
- Create a new Windows Console application.
- Install the preview NuGet package:
install-package ServiceBus.Preview
- Implement the Main() method:
static void Main(string[] args) { var queuePath = "autodeletequeue"; NamespaceManager nm = new NamespaceManager( ServiceBusEnvironment.CreateServiceUri("https", "[Your SB namespace]", string.Empty), TokenProvider.CreateSharedSecretTokenProvider("owner", "[Your secret key]")); QueueDescription queue = new QueueDescription(queuePath); queue.AutoDeleteOnIdle = TimeSpan.FromMinutes(1); Console.WriteLine("Creating queue..."); nm.CreateQueue(queue); Console.WriteLine("Queue created!"); DateTime creation = DateTime.Now; while (nm.QueueExists(queuePath)) { Console.WriteLine("Queue exists after: " + (DateTime.Now - creation).TotalSeconds.ToString("#.#") + " seconds"); Thread.Sleep(1000); } Console.WriteLine("Queue removed!"); Console.ReadLine(); }
The code is very straightforward – it creates a queue with the AutoDeleteOnIdle property set to 1 minute. And it waits till the queue disappears. The following is a screenshot of the program when I ran it in a coffee shop (whose Internet isn’t that great):
As you can see the queue disappears in about 2 minutes. I tried the program several times and the queue went away in about 1.5 to 3 minutes (though the AutoDeleteOnIdel is set to 1 minute). This should not be a concern as in this case we are leaving the system to automatically recycle the entities for us. The entities being removed a bit later isn’t unacceptable. On the other hand, we need to have a better understanding of what “Idle” means. Does it count as “idle” if there are no messages passing through the pipeline, or there has to be absolutely no activities? It turned out, “Idle” means no attached clients are trying to send or receive messages. Let’s modify the Main() method a little:
... QueueClient client = QueueClient.CreateFromConnectionString("[Connection String]", queuePath); DateTime creation = DateTime.Now; while (nm.QueueExists(queuePath)) { client.Receive(TimeSpan.FromSeconds(3)); Console.WriteLine("Queue exists after: " + (DateTime.Now - creation).TotalSeconds.ToString("#.#") + " seconds"); Thread.Sleep(1000); } ...The Receive() calls make the queue active, though they don’t get any messages back. Now consider the following code:
... QueueClient client = QueueClient.CreateFromConnectionString("[Connection String]", queuePath); client.OnMessage((m) => {}, 1); DateTime creation = DateTime.Now; while (nm.QueueExists(queuePath)) { Console.Write("Queue exists after: " + (DateTime.Now - creation).TotalSeconds.ToString("#.#") + " seconds"); Thread.Sleep(1000); } ...In this version, I’m not calling Receive() method anymore. However, because I subscribed to OnMessage event, the queue is kept alive. If you want to release the queue in this case, you’ll need to close the attached client(s) by calling Close() method on QueueClient class.
And this concludes the second part of this series. I hope this helps. Thank you reading!
Add a comment