This sample demonstrates how to use Windows Azure Service Bus to send and receive messages from a topic with multiple subscriptions.
Similar to a queue, a topic provides decoupled, asynchronous communication between a sender and any number of receivers. Furthermore, a topic allows duplication and filtering of messages.
Imagine a scenario where a company tracks issues sent by customers, using two different systems - one for resolving the opened issues and one that logs the issues for audit purposes. This sample demonstrates how to do this with a topic and two subscriptions, AgentSubscription and AuditSubscription. Both subscriptions use the default settings, so messages sent into the topic will be delivered to both subscriptions. (See the AdvancedFilters sample for how to set up a subscription so that only certain messages are delivered, or a custom action is performed as those messages are delivered.)
The sample has a single message receiver processing messages from each subscription, but additional receivers can be added on that same subscription. In that case, the subscription behaves like a queue, with competing consumers.
Prerequisites
If you haven't already done so, please read the release notes document that explains how to sign up for a Windows Azure account.
Sample Flow
The sample flows in the following manner:
- Start a sender.
- Sender prompts for user Service Bus credentials.
- Sender creates topic and subscriptions.
- Sender sends messages to topic.
- Start a receiver.
- Receiver creates two message receivers, on per subscription.
- Receiver retrieves messages from each subscription using the associated message receiver.
- Close receiver.
- Close sender (deletes topic and subscriptions).
Running the Sample
To run the sample:
- Build the solution in Visual Studio.
- Run the sender, inputting service namespace, issuer name, and issuer key when prompted to do so.
- Run the receiver, inputting service namespace, issuer name, and issuer key when prompted to do so.
Expected Output - Sender
Please provide the service namespace to use: <service_namespace> Please provide the issuer name to use: <issuer_name> Please provide the issuer key to use: <issuer_key> Creating Topic 'IssueTrackingTopic'... Creating Subscriptions 'AuditSubscription' and 'AgentSubscription'... Sending messages to topic... Message sent: Id = 1, Body = First message information Message sent: Id = 2, Body = Second message information Message sent: Id = 3, Body = Third message information Finished sending messages, press ENTER to clean up and exit. |
Expected Output - Receiver
Please provide the service namespace to use: <service_namespace> Please provide the issuer name to use: <issuer_name> Please provide the issuer key to use: <issuer_key> Receiving messages from AgentSubscription... Message received: Id = 1, Body = First message information Message received: Id = 2, Body = Second message information Message received: Id = 3, Body = Third message information Receiving messages from AuditSubscription... Message received: Id = 1, Body = First message information Message received: Id = 2, Body = Second message information Message received: Id = 3, Body = Third message information End of scenario, press ENTER to exit. |