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]

part 1: Message Pump

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.

  1. Create a new Windows Console application.
  2. Install the preview NuGet package:
    install-package ServiceBus.Preview
  3. 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):

image

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!

0

Add a comment

When we learn classic compute algorithms, we don't start with learning how a computer is built. We don't start with how a transistor works or how to build integrated circuits. Instead, we go straight with the abstraction - bits, commands, programs and such. I think we should take the same approach when we learn quantum computing. Instead of trying to understand the bizarre quantum world, we should take some quantum behaviors granted and go straight with higher-level abstracts such as qubits and quantum gates. And once we grasp these basic concepts, we should go even a level higher to use a high-level language like Q# and focus on how quantum algorithms work, and how we can apply quantum algorithms on practical problems.

Bono is an open source quantum algorithm visualizer I'm building in the open. This project is inspired by a few existing systems such as QuirkIBM Q Experience, and the Programming Quantum Computers book. Bono is a Javascript-based visualizer that features:

  • Drag-and-drop quantum circuit editing.
  • Dynamic circuit evaluation.
  • Works offline in a browser. No server is needed.
  •  Generates Q# code.
I've also created a YouTube channel dedicated to quantum algorithms. My goal is to create an easily digestable course for regular software developers to learn about quantum computing without needing to understand any underlying quantum physics. 

Bono is at its infancy. And I'm still a new student in the quantum world. I intentionally develop both Bono and the video series in the open. I hope the early results can inspire collaborations so that we can explore the quantum computing world together.

Last but not least, you can find more Q# related contents at the Q# Advent Calendar 2019.
0

Add a comment

Series
存档
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.