Introducing Windows Azure- P24 pptx

5 321 0
Introducing Windows Azure- P24 pptx

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

Thông tin tài liệu

CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 88 if (e.CommandName == "DeleteEntry") { int index = Convert.ToInt32(e.CommandArgument); string blobName = (string)BlobLinksView.DataKeys[index].Value; if (_blobContainer.DoesBlobExist(blobName)) { _blobContainer.DeleteBlob(blobName); } } } catch { } DataBind(); } Listing 3-13. Command Handler Used to Interprete Which Blob Item Row Has Been Selected protected void RowCommandHandler(object sender, GridViewCommandEventArgs e) { try { if (e.CommandName == "DeleteEntry") { int index = Convert.ToInt32(e.CommandArgument); string blobName = (string)BlobLinksView.DataKeys[index].Value; if ( blobContainer.DoesBlobExist(blobName)) { blobContainer.DeleteBlob(blobName); } } } catch { } DataBind(); } In addition to using ListBlobs() to retrieve blob records in a C# class as I demonstrated, the blob record can also be retrieved using a REST query with tools or applications, such as Fiddler, that can generate web HTTP GET/POST/UPDATE requests. Figure 3-9 shows an example of the REST query results against our blob storage. The tool used to do the REST query is Fiddler 2. The example of syntax for the query string is as follows: http://127.0.0.1:10000/devstoreaccount1/blobpayload/caa95517-3414-4bc2-8f16-0a44a6f156e1xml The return code for a success REST query is 200. CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 89 Figure 3-9. REST query results of blob data using Fiddler 2 HTTP debug tool Creating a Loosely Coupled Event-Driven System Using both queue and blob storage, we can easily design loosely-coupled, event-driven applications. Figure 3-10 is the case study diagram of this type of application design. The concept is very simple and straightforward as the diagram shows. The following are the highlights of the design concepts. • Define an events listener to monitor a specific event. The event is fired at the time a message is delivered to the queue. A listener from the server side should be constructed with two components: a dedicated queue to accept a message with a specific topic (queue name) and an event handler. For example, if we need to handle actions that insert and delete data, we need to define two queues and implement two event handlers to deal with these two events respectively. • The domain for all listeners is the blob container. The blob container is the data access layer to the under-the-hood blob storage. • A listener queue may have no event handler if there is no need to access the data. For example, after a blob has been inserted successfully, we send a message to the results queue with the message body containing the blob name. In this case sending a response queue message is good enough. This allows us to remove the responsibility for the server sending notification to the client when a blob is inserted or deleted, and to avoid sending a big message over the Internet. A client application can implement its own event listener to monitor the result events from the results queue using a synchronized approach. CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 90 Create Blob Request Queue Delete Blob RequestQueue Blob Container Create Blob Message Event Handler Delete Blob Message Event Handler Results Queue Delete Blob Message Add Blob Message Figure 3-10. Case study diagrams for an event-driven system design, using both queue storage and blob storage ■ Note The code for this example is in the Exercise 3-3 bundle from the code download. To reach this goal, we need to accomplish the following steps. 1. Move all code related to blob creation and deletion from the Default.aspx.cs code behind into the worker role as shown in Listing 3-14. In the Initialization() method, we need to do the following: 1. Instantiate an instance of BlobContainer. 2. Instantiate an instance of QueueStorage. 3. Use the instance of QueueStorage to create a dedicated queue to listen to the blob create request message and register the event handler. CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 91 4. Create a dedicated queue to deliver the results (in other words, notification that a blob name has been created). For this queue we don’t need to register an event handler. Note: it is very important that you don’t call the StartReceiving() method to start polling the queue if a queue does have an event handler registered. This will cause an ObjectNullReference exception at runtime, and there is no way to catch the exception or trace down the error stack, even in the development environment. 2. Delete a message from the queue after it has been processed to avoid duplicate message appearing after the locking period. 3. Use XmlSerializer to serialize the object into an XML string and place it in the body of the blob storage. Listing 3-14. Worker Role to Implement Listener Queues to Handle Blob Create and Delete Requests using System.Drawing; using System.Configuration; using System.NET; using System.Xml; using System.Xml.Serialization; using System.Collections.Specialized; using System.Diagnostics; namespace CloudQueueStorageService WorkerRole { public class WorkerRole : RoleEntryPoint { public const string PAYLOAD RESULTS QUEUE NAME = "resultspayloadqueue"; public const string PAYLOAD CREATE REQUEST QUEUE NAME = "createblobrequestqueue"; public const string PAYLOAD DELETE QUEUE NAME = "deleteblobqueue"; public const string PAYLOAD BLOB CONTAINER NAME = "blobpayload"; public const string PAYLOAD BLOB SUFFIX = "xml"; public const int UPDATE TIMEOUT SEC = 5; private static BlobStorage blobStorage = null; private static QueueStorage queueStorage = null; private static BlobContainer blobContainer = null; private static bool initialized = false; private static object syncObj = new Object(); const int POLLING INTERVAL = 1000;// in milliseconds public void LogLevel(string logStream, string logString) { if (!RoleManager.IsRoleManagerRunning) { Trace.WriteLine(logString); } else { CHAPTER 3 ■ WORKING WITH CLOUD QUEUE AND BLOB STORAGE 92 RoleManager.WriteToLog(logStream, logString); } } static WorkerRole() { } public void Log(string logString) { LogLevel("Information", logString); } public void LogError(string logString) { LogLevel("Error", logString); } public override void Start() { while (! initialized) { try { Initialization(); } catch (WebException e) { if (e.Status == WebExceptionStatus.ConnectFailure) { RoleManager.WriteToLog( "Error", string.Format("Connect failure. Message: '{0}'", e.Message) ); System.Threading.Thread.Sleep(5000); } else { Throw e; } } } while (true) { Log(string.Format(" {0} heart beat ", this.ToString())); Thread.Sleep(1000); } } public override RoleStatus GetHealthStatus()

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

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

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

Tài liệu liên quan