Azure Service Bus Queues

Lately, I have been experimenting with Azure Service Bus, and especially Queues, but  the Azure Service Bus also have features like Event Hub, Topics and Relay. I have most experience with the concept of queues, and I started my journey there. A queue is called a FIFO data structure (FIFO, First-In-First-Out). This means that the first element added to the queue will be the first one to be removed. This ensures that elements come in the right order they were put into the queue. This can be illustrated with following figure:

sb-queues-08

In the middle, the queue receieved messages (items) from one or more message senders. These messages might reside the queue until they are taken out of the queue by one or more receiers, or automatically disposed into the “dead letter” (garbage bin). You also be able to take a look at the first element by a “peek” operation.

When you are working with queues in Microsoft Azure, you first have to create a namespace container where all the queues shall be located and grouped together. A namespace might conatin queues and topics. The code below shows the basic code for writing a test message to a Azure Service Bus Queue.

           const string qName = "SampleQueue";

            var nsmgr = NamespaceManager.Create();
            if (!nsmgr.QueueExists(qName))
                nsmgr.CreateQueue(qName);

            var qClient = QueueClient.Create(qName);

            var msg = new BrokeredMessage("test message");
            msg.MessageId = "1";

            qClient.Send(msg);

            //var msg1 = qClient.Peek();
            //var msg2 = qClient.Receive();
            //msg2.Complete();

            qClient.Close();

After I had written my first element to a Azure Queue, I thought: “How do I look at the element in the queue without writing the code for retrieving the element. Paolo Salvatori has written a nice Service Bus Explorer for this purpose. As you might see, I have commented the Peek and Receive operations. The receive require a Complete operation as well.

Happy coding….