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.BrokeredMessages using the
In the Read Corner…
…an application that reads from the message bus using the
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.
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.