Introducing Windows Azure- P22 docx

5 175 0
Introducing Windows Azure- P22 docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 78 System.Diagnostics.Trace.WriteLine( string.Format( {0}: OnMessageReceive, message = <{1}>", this.ToString(), message.ContentAsString() ) ); } private MessageQueue GetXmlPayloadQueue() { Initialization(); return queueStorage.GetQueue(WorkerRole.XML PAYLOAD QUEUE NAME); } Delete a Message from the Queue To delete a message from a queue is straightforward as Listing 3-6 shows. The method btnDelete Click() is implemented in the Default.aspx.cs file and is the button click event handler used to delete a message from the queue. Listing 3-6. Listen and Delete Message from a Queue Implementation const int UPDATE TIMEOUT SEC = 5; protected void btnDelete Click(object sender, EventArgs e) { Message message = GetXmlPayloadQueue().GetMessage(UPDATE TIMEOUT SEC); if (message != null) { GetXmlPayloadQueue().DeleteMessage(message); } } The reason for specifying the timeout before calling to delete a message from the queue is that having received a message that has been put into queue, that message is locked for the specified timeout. If the message is not deleted within the timeout, it is unlocked and becomes visible again to other queue readers. Microsoft designs this to prevent a queue reader trying to process a message and then dying as a result of an error or exception, leaving the message locked. This approach is known as a loose transaction, since transactions in cloud queue storage are not supported. Parse a Message Received from the Queue When a message has been received, we need to parse it back to the original data format. The Message class from the Azure SDK provides functions to parse the raw message as either an array or a string as Listing 3-7 shows. CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 79 Listing 3-7. Parse Message Received from a Queue into String const int UPDATE TIMEOUT SEC = 5; Message message = GetXmlPayloadQueue().GetMessage(UPDATE TIMEOUT SEC); if (message != null) { btnDelete.Enabled = true; LabelMessage.Text = Server.HtmlEncode(message.ContentAsString()); } else { btnDelete.Enabled = false; } The results of Exercise 3-1 are shown in Figure 3-6. The messages put into the queue will be displayed in a loop one after another on the update panel. The automatic updating is controlled by the Ajax update manager component, which needs to be inserted in the Default.aspx page. Figure 3-6. Results of Exercise 3-1 displaying multiple data records in a loop CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 80 Query a Queue Using HTTP REST The syntax to use HTTP REST to query a queue is listed in Table 3-1. Table 3-1. Syntax Used to Query Queue Message with REST REST API HTTP Example Description Create Queue PUT http://myaccount.queue.core. windows.net/myqueue Delete Queue DELETE http://myaccount.queue.core. windows.net/myqueue Get Metadata GET/HEAD http://127.0.0.1:10001/myaccount/ myqueue?comp=metadata Also returns the message count; other properties are free-form name/value pairs Set Metadata PUT http://127.0.0.1:10001/myaccount/ myqueue?comp=metadata Get Messages GET http://127.0.0.1:10001/myaccount/ myqueue/messages Peek Messages GET http://myaccount.queue.core. windows.net/myqueue/messages? peekonly=true Delete Message DELETE http://127.0.0.1:10001/myaccount/ myqueue/messages/messageid?popreceipt =string-value"lightweight commit" Clear Messages DELETE http://myaccount.queue.core. windows.net/myqueue/messages Deletes all the messages in the queue Creating Cloud Blob Storage The following exercise demonstrates how to create, query, and delete blob data. This exercise uses the data from the queue message, which we created in Exercise 3-1, and stores data in a blob. ■ Note The code for this example is in the Exercise 3-2 bundle from the code download. Add blob-related member variables to the Default.aspx code as shown in Listing 3-8. CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 81 Listing 3-8. Blob-Storage-Related Member Variables Defined in Default.aspx public const string BLOB CONTAINER NAME = "blobcontainerpayload"; private static BlobStorage blobStorage = null; private static BlobContainer blobContainer = null; const int UPDATE TIMEOUT SEC = 5; const string SUFFIX = "xml"; Add initialization code to the Initialization handler as Listing 3-9 shows. The ContainerAccessControl should be set to Public so applications can query the blob using REST APIs. By design, to change the access scope there are two CreateContainer() methods. The first instantiates a blob container object by passing a blob container’s name (characters in the blob container’s name should be all in lowercase), and is called from the instance of the blob storage. The second is just a call to an overloaded function of the blob container class. This call must use a valid instance of a blob container by passing in the enumeration value ContainerAccessControl.Public. This call is optional and should not override the instance itself but modify the access scope. Listing 3-9. Initialization Code for Blob Storage Access private void Initialization() { if ( initialized) { return; } lock ( syncObj) { try { _blobStorage = BlobStorage.Create(StorageAccountInfo .GetDefaultBlobStorageAccountFromConfiguration()); _blobContainer = _blobStorage.GetBlobContainer(BLOB_CONTAINER_NAME); // Make the container public so that we can use REST API to query blob // via the URLs from the web _blobContainer.CreateContainer(new NameValueCollection(), ContainerAccessControl.Public); queueStorage = QueueStorage.Create(StorageAccountInfo .GetDefaultQueueStorageAccountFromConfiguration()); queueStorage.RetryPolicy = RetryPolicies.RetryN(3, TimeSpan.FromSeconds(5)); MessageQueue queue = queueStorage.GetQueue(BLOB PAYLOAD QUEUE NAME); queue.CreateQueue(); queue.MessageReceived += new MessageReceivedEventHandler( OnMessageReceive); CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 82 queue.PollInterval = 500; // in milliseconds queue.StartReceiving(); // start polling } catch (WebException ex) { throw new WebException( string.Format( " {0}: Initialization, Windows Azure failed to instatiate storage using current account information. Exception caught : {1}", this.ToString(), ex.Message ) ); } initialized = true; } } After the call to Initialization(), both the blob storage and blob container have been instantiated, and a blob record can be created using the instance of the blob container. Listing 3-10 shows the code to create a blob storage record. In this exercise, the name of the blob storage is created at the time a queue message has been received. The blob name is assigned when a blob is created. How to construct a blob’s name is flexible as long as the name meets the specification, except it must be unique. The specification for the blob name convention is listed in Appendix A. It is not recommended to include the Azure user account in the name string, because the account name could be changed via a configuration file when the application is deployed to the Azure cloud. Azure offers a service called Service Bus, which uses the end point to address a cloud component. To adapt blob storage to Azure .NET Service Bus, it is recommended that the blob name should be elaborately constructed. The following are the recommendations how to construct the blob name based upon my experience using other commercial information message buses. This makes it a lot easier not only for a cloud application but also for any REST API using the Azure .NET Service Bus to address cloud blob storage. • Use the .NET name space convention to compose the blob name. • Compose the blob name in a virtual hierarchy based upon the logical or relational structure of blob storages, although Azure blob storage in the cloud storage platform is not physically hierarchical. The metadata of Azure blob storage is constructed when the BlobProperties object has been instantiated. The metadata object is embedded into the object instance of BlobProperties. As we mentioned, the metadata is used as the set of attributes of blob storage. The metadata is in the name-value pair format. The actual class for the metadata is called NameValueCollection, which is a .NET class. The namespace System.Collections.Specialized must be included before this class can be instatiated. Blob containers take two parameters for blob storage creation, BlobContents and BlobProperties. The first parameter holds the body of the information, and the second parameter holds the attribute information. Only two data types can be used for these two parameters, Stream and Byte array. The information body must be transformed into either of these two types before instantiation of a BlobContents object. As Listing 3-10 shows, in this exercise we create the blob . Description Create Queue PUT http://myaccount.queue.core. windows. net/myqueue Delete Queue DELETE http://myaccount.queue.core. windows. net/myqueue Get Metadata GET/HEAD http://127.0.0.1:10001/myaccount/. http://127.0.0.1:10001/myaccount/ myqueue/messages Peek Messages GET http://myaccount.queue.core. windows. net/myqueue/messages? peekonly=true Delete Message DELETE http://127.0.0.1:10001/myaccount/. myqueue/messages/messageid?popreceipt =string-value"lightweight commit" Clear Messages DELETE http://myaccount.queue.core. windows. net/myqueue/messages Deletes all the messages in the queue Creating Cloud Blob Storage

Ngày đăng: 05/07/2014, 01:20

Tài liệu cùng người dùng

  • Đang cập nhật ...