次の方法で共有


Learn how to create a queue, place and read a message using Azure Service Bus Queues in 5 minutes

This is the first of two post that will illustrate how to write and read messages from Azure service bus queues. The second post will actually illustrate how to read these messages using Node.js.

What I like about this post is that it only took a few minutes to complete.

This post is here to support an MSDN article that I am writing with Steven Edouard.

Creating the Service Bus Namespace and Queue

The assumption here of course is that you have opened an account with Windows Azure.

The first order of business is to actually create the service. You will need to go to the Azure portal click on the service bus , then New in the lower left corner.

image001

Figure 1: Creating a new service bus queue

Select Queue , Custom Create .

image002

Figure 2: Selecting Custom Create

You will need to provide a queue name and you will need to create a namespace name.

image003

Figure 3: Configuring the service bus queue

We will accept the default values here.

image004

Figure 4: Specifying the queue name and namespace

You can see the created namespace in the portal once it is complete.

image005

Figure 5: Validating the creation of a service bus queue

In the figure above you can click on connection information . Specifically, you will need the connection string . The connection string will be pasted into the app . config file of a Visual Studio project we are about to download .

image

Figure 6: Viewing connection information

Downloading the Sample

Here is the sample to illustrate how to write and read to Azure Service Bus Queues. This is the download link:

https://code.msdn.microsoft.com/windowsazure/Getting-Started-Brokered-aa7a0ac3

image007

Figure 7: Downloading the Service Bus Sample

Modifying the Sample

The modifications will be quite simple. The only thing that we will need to do is modify the connection string in app.config

image008

Figure 8: Viewing the solution

App.config

You can see below that we have modified the connection string . You will need to paste in the connection string you obtained from the portal .

image009

Figure 9: Viewing and modifying App.config

Viewing the Code

Let's take a brief look at the actual code that reads and writes to the message queue.

Creating, Writing to, an Reading from the message queue

  

Service Bus Queue Sample
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 public class program {     private static QueueClient queueClient;     private static string QueueName = "SampleQueue";     const Int16 maxTrials = 4;     static void Main(string[] args)     {         Console.WriteLine("Creating a Queue");                      CreateQueue();         Console.WriteLine("Press anykey to start sending messages ...");         Console.ReadKey();         SendMessages();         Console.WriteLine("Press anykey to start receiving messages that you just sent ...");         Console.ReadKey();         ReceiveMessages();         Console.WriteLine("\nEnd of scenario, press anykey to exit.");         Console.ReadKey();     }     private static void CreateQueue()     {         NamespaceManager namespaceManager = NamespaceManager.Create();         Console.WriteLine("\nCreating Queue '{0}'...", QueueName);         // Delete if exists         if (namespaceManager.QueueExists(QueueName))         {             namespaceManager.DeleteQueue(QueueName);         }         namespaceManager.CreateQueue(QueueName);     }     private static void SendMessages()     {         queueClient = QueueClient.Create(QueueName);         List<BrokeredMessage> messageList = new List<BrokeredMessage>();         messageList.Add(CreateSampleMessage("1", "First message information"));         messageList.Add(CreateSampleMessage("2", "Second message information"));         messageList.Add(CreateSampleMessage("3", "Third message information"));         Console.WriteLine("\nSending messages to Queue...");         foreach (BrokeredMessage message in messageList)         {             while (true)             {                 try                 {                     queueClient.Send(message);                 }                 catch (MessagingException e)                 {                     if (!e.IsTransient)                     {                         Console.WriteLine(e.Message);                         throw;                     }                     else                     {                         HandleTransientErrors(e);                     }                 }                 Console.WriteLine(string.Format("Message sent: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));                 break;             }         }     }     private static void ReceiveMessages()     {         Console.WriteLine("\nReceiving message from Queue...");         BrokeredMessage message = null;         NamespaceManager namespaceManager = NamespaceManager.Create();         queueClient = QueueClient.Create(QueueName);         while (true)         {             try             {                 //receive messages from Queue                 message = queueClient.Receive(TimeSpan.FromSeconds(5));                 if (message != null)                 {                     Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));                     // Further custom message processing could go here...                     message.Complete();                 }                 else                 {                     //no more messages in the queue                     break;                 }             }             catch (MessagingException e)             {                 if (!e.IsTransient)                 {                     Console.WriteLine(e.Message);                     throw;                 }                 else                 {                     HandleTransientErrors(e);                 }             }         }         queueClient.Close();     }     private static BrokeredMessage CreateSampleMessage(string messageId, string messageBody)     {         BrokeredMessage message = new BrokeredMessage(messageBody);         message.MessageId = messageId;         return message;     }     private static void HandleTransientErrors(MessagingException e)     {         //If transient error/exception, let's back-off for 2 seconds and retry         Console.WriteLine(e.Message);         Console.WriteLine("Will retry sending the message in 2 seconds");         Thread.Sleep(2000);     } }

Running the Sample

Screen below shows messages that are being written to the message queue.

image010

Figure 10: Viewing queue creating and message sending

As the program progresses, it will actually display the messages that are read from the message queue . In a future post we will show how to do this from node.JS

image011

Figure 11: Viewing messages in the queue

When messages are read from the message queue , they are removed . Because we want these messages to remain in the queue so that our node.JS program can read them, you should think about commenting out the code that reads messages from the queue . Alternatively, you can use the Peek () method to read messages, but not remove them from the queue.

Comments

  • Anonymous
    June 08, 2015
    Can you please help me , how to send these messages to an email id Please Replay

  • Anonymous
    October 04, 2015
    I think there's a bug in your Sending logic. It is not going to retry sending the message in 2 seconds if it is a Transient error, it will always break out of the while loop. Please move the break inside the try block. Other than that, a very helpful post. Thanks!