This sample demonstrates how to use the Windows Azure Service Bus and its message buffer feature.
The sample shows two console applications: the first is a producer of messages (Producer), the second is a consumer of messages (Consumer). The Consumer application creates the message buffer and then waits to read messages from the message buffer. The Producer application retrieves the message buffer, then sends messages to it.
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 and how to configure your environment.
The Consumer
The Consumer application creates the message buffer and then retrieves messages from it that are sent by the Producer. Creating a message buffer requires the credentials for the endpoint, the message buffer URI, and the message buffer policy. The Consumer creates these prerequisite objects.
C# | |
---|---|
Console.Write("Your Service Namespace: "); string serviceNamespace = Console.ReadLine(); Console.Write("Your Issuer Name: "); string issuerName = Console.ReadLine(); Console.Write("Your Issuer Secret: "); string issuerSecret = Console.ReadLine(); // create the policy for the message buffer this.policy = new MessageBufferPolicy(); this.policy.Authorization = AuthorizationPolicy.Required; this.policy.MaxMessageCount = 10; this.policy.ExpiresAfter = TimeSpan.FromMinutes(5); // messages in the message buffer expire after 5 mins this.policy.TransportProtection = TransportProtectionPolicy.AllPaths; // create the credentials object for the endpoint this.credential = new TransportClientEndpointBehavior(); this.credential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret); // create the URI for the message buffer this.uri = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "MessageBuffer"); |
After creating the credentials for the endpoint, the message buffer URI, and the message buffer policy the Consumer creates the message buffer.
C# | |
---|---|
this.client = MessageBufferClient.CreateMessageBuffer(this.credential, this.uri, this.policy); Console.WriteLine("Message buffer created at '{0}'.", this.client.MessageBufferUri); |
After the message buffer is created and the Producer has sent messages to this message buffer, the Consumer retrieves the first two messages.
C# | |
---|---|
Message retrievedMessage; // retrieve the first two messages from the message buffer Console.Write("Press [Enter] to retrieve the first message in the message buffer: "); Console.ReadLine(); retrievedMessage = this.client.Retrieve(); Console.WriteLine("Retrieved message with body '{0}'.", retrievedMessage.GetBody<string>()); retrievedMessage.Close(); Console.Write("Press [Enter] to retrieve the next message from the message buffer: "); Console.ReadLine(); retrievedMessage = this.client.Retrieve(); Console.WriteLine("Retrieved message with body '{0}'.", retrievedMessage.GetBody<string>()); retrievedMessage.Close(); |
Next, the Consumer peek/locks the last message and deletes this locked message.
C# | |
---|---|
// lock and peek at the next message; then delete it Console.Write("Press [Enter] to lock, peek, and delete the next message from the message buffer: "); Console.ReadLine(); Message lockedMessage = this.client.PeekLock(); this.client.DeleteLockedMessage(lockedMessage); Console.WriteLine("Message with body '{0}' locked, peeked, and then deleted.", lockedMessage.GetBody<string>()); lockedMessage.Close(); |
After retrieving all messages, the Consumer deletes the message buffer.
C# | |
---|---|
// delete the message buffer this.client.DeleteMessageBuffer(); Console.WriteLine("Message buffer at {0} was deleted.", this.client.MessageBufferUri); |
The Producer
The Producer application accesses the message buffer that the Consumer created and sends three messages to it. Connecting to a message buffer requires the credentials for the endpoint and the message buffer URI. The Producer creates these prerequisite objects first.
C# | |
---|---|
Console.Write("Your Service Namespace: "); string serviceNamespace = Console.ReadLine(); Console.Write("Your Issuer Name: "); string issuerName = Console.ReadLine(); Console.Write("Your Issuer Secret: "); string issuerSecret = Console.ReadLine(); // create the credentials object for the endpoint this.credential = new TransportClientEndpointBehavior(); this.credential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerSecret); // create the URI for the message buffer this.uri = ServiceBusEnvironment.CreateServiceUri("https", serviceNamespace, "MessageBuffer"); |
After creating the endpoint credentials and the message buffer URI, the Producer connects to the message buffer.
C# | |
---|---|
this.client = MessageBufferClient.GetMessageBuffer(this.credential, this.uri); Console.WriteLine("Connected to the message buffer at '{0}'.", this.client.MessageBufferUri); |
After establishing a connection to the message buffer, the Producer sends three messages to it.
C# | |
---|---|
// send three messages to the message buffer for (int i = 1; i <= 3; ++i) { Message message = Message.CreateMessage(MessageVersion.Default, string.Empty, string.Format("<<MESSAGE {0}>>", i)); this.client.Send(message); Console.WriteLine("Sent message with body '<<MESSAGE {0}>>' to the message buffer.", i); message.Close(); } |
Running the Sample
To run the sample, build the solution in Visual Studio or from the command line, then run the Consumer application first to create the message buffer. The Producer application accesses this message buffer later on to send messages to it that are later retrieved by the Consumer.
After starting the Consumer application, it prompts you for the service namespace and credentials. Enter these values in the Consumer console window. Next, press ENTER to create the message buffer. This message buffer is used by the Producer to send messages to.
After the message buffer has been created, run the Producer application. In the Producer console window, enter the service namespace and credentials. Then press ENTER to connect to the message buffer previously created by the Consumer. Next, press ENTER three times to send three messages to the message buffer.
After having sent the messages to the message buffer, switch back to the Consumer console window and press ENTER to retrieve the first message. The body of the first message is displayed. Then, press ENTER to retrieve the next message. The body of the second message is displayed. Next, press ENTER to lock, peek, and delete the last message. The body of the last message is displayed along with the operation carried out. Finally, press ENTER to delete the message buffer.
Close both console windows to end the Consumer and Producer applications.
The following is the expected output for the Consumer and Producer applications.
Expected Output - Consumer
THE CONSUMER Your Service Namespace: <service-namespace> Your Issuer Name: <issuer-name> Your Issuer Secret: <issuer-secret> Press [Enter] to create the message buffer: Message buffer created at 'http://<service-namespace>.servicebus.windows.net/MessageBuffer/'. Press [Enter] to retrieve the first message in the message buffer: Retrieved message with body '<<MESSAGE 1>>'. Press [Enter] to retrieve the next message from the message buffer: Retrieved message with body '<<MESSAGE 2>>'. Press [Enter] to lock, peek, and delete the next message from the message buffer: Message with body '<<MESSAGE 3>>' locked, peeked, and then deleted. Press [Enter] to delete the message buffer: Message buffer at http://<service-namespace>.servicebus.windows.net/MessageBuffer/ was deleted. Press any key to continue . . . |
Expected Output - Producer
THE PRODUCER Your Service Namespace: <service-namespace> Your Issuer Name: <issuer-name> Your Issuer Secret: <issuer-name> Press [Enter] to retrieve the message buffer: Message buffer at 'http://<service-namespace>.servicebus.windows.net/MessageBuffer/' retrieved. Press [Enter] to send messages to the message buffer: Sent message with body '<<MESSAGE 1>>' to the message buffer. Sent message with body '<<MESSAGE 2>>' to the message buffer. Sent message with body '<<MESSAGE 3>>' to the message buffer. Press any key to continue . . . |
Did you find this information useful? Please send your suggestions and comments about the documentation.