Testing Azure ServiceBus Queues

Here’s a quick example of using Microsoft Azure’s ServiceBus message queue to decouple communication between two applications.

In this case it’s two console applications for ease of demonstration although you would never (I hope) use cloud infrastructure for on-premise communication.

In the Write Corner…

…an application that connects to the message bus using Microsoft.ServiceBus.Messaging.MessagingFactory.CreateFromConnectionString and sends Microsoft.ServiceBus.Messaging.BrokeredMessages using the Microsoft.ServiceBus.Messaging.MessageSender class.

writer

In the Read Corner…

…an application that reads from the message bus using the Microsoft.ServiceBus.Messaging.MessageReceiver class.

reader 1

Totally, like, random

In both applications I have introduced a bit of randomness in sending and receiving to demonstrate that you can run more than instance of the reader application and each instance should pick up and process their own share of messages without interfering with each other.

reader 2

This sample also demonstrates the use of the Defer method to allow an instance to claim a message to work on, complete some process then come back to the message, retrieve it from the queue and mark it as complete.