基于云存儲實(shí)現用Windows Azure Storage增強應用程序的引擎
因為我們的存儲機制已經(jīng)確定,所以我們需要一個(gè)工作者角色作為引擎;以便在我們的電子商務(wù)站點(diǎn)的后臺處理消息。為此,我們定義了一個(gè)從 Microsoft.ServiceHosting.ServiceRuntime.RoleEntryPoint 類(lèi)繼承的類(lèi),并將其與云服務(wù)項目中的工作者角色關(guān)聯(lián)(請參見(jiàn)圖 5)。
圖 5 作為引擎的工作者角色
public class WorkerRole : RoleEntryPoint
{
ShoppingCartQueue cartQueue;
ToastQueue toastQueue;
UserTextNotificationRepository toastRepository;
public override void Run()
{
// This is a sample worker implementation.
//Replace with your logic.
Trace.WriteLine(WorkerRole1 entry point called,
Information);
toastRepository = new UserTextNotificationRepository();
InitQueue();
while (true)
{
Thread.Sleep(10000);
Trace.WriteLine(Working, Information);
ProcessNewTextNotifications();
ProcessShoppingCarts();
}
}
private void InitQueue()
{
cartQueue = new ShoppingCartQueue();
toastQueue = new ToastQueue();
}
private void ProcessNewTextNotifications()
{
CloudQueueMessage cqm = toastQueue.GetMessage();
while (cqm != null)
{
ToastQueueMessage message =
QueueMessageBase.FromMessageToastQueueMessage>(cqm);
toastRepository.AddNotification(new
UserTextNotification()
{
MessageText = message.MessageText,
MessageDate = DateTime.Now,
TargetUserName = message.TargetUserName,
Title = message.Title
});
toastQueue.DeleteMessage(cqm);
cqm = toastQueue.GetMessage();
}
}
private void ProcessShoppingCarts()
{
// We will add this later in the article!
}
public override bool OnStart()
{
// Set the maximum number of concurrent connections
ServicePointManager.DefaultConnectionLimit = 12;
DiagnosticMonitor.Start(DiagnosticsConnectionString);
// For information on handling configuration changes
// see the MSDN topic at
//http://go.microsoft.com/fwlink/?LinkId=166357.
RoleEnvironment.Changing += RoleEnvironmentChanging;
return base.OnStart();
}
private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
{
// If a configuration setting is changing
if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
{
// Set e.Cancel to true to restart this role instance
e.Cancel = true;
}
}
}
讓我們看一下工作者角色代碼。在初始化和設置所需的隊列和表存儲之后,此代碼將進(jìn)入一個(gè)循環(huán)。每 10 秒鐘,它就會(huì )處理一次隊列中的消息。每次我們通過(guò)處理循環(huán)時(shí)都將獲取隊列中的消息,直到最終返回 null,這表示隊列為空。
您從隊列中看到的消息永遠不會(huì )超過(guò) 20 個(gè),如果不信,您可以反復嘗試來(lái)驗證一下。對隊列進(jìn)行的任何處理都有時(shí)間限制,必須在該時(shí)間范圍內對每個(gè)隊列消息執行有意義的操作,否則隊列消息將被視為超時(shí),并在隊列中顯示備份,以便可以由其他工作者來(lái)處理此消息。每個(gè)消息都會(huì )作為用戶(hù)通知添加到表存儲中。關(guān)于工作者角色需要記住的重要一點(diǎn)是:一旦入口點(diǎn)方法完成,工作者角色也就結束了。這就是您需要在一個(gè)循環(huán)中保持邏輯運行的原因。
從客戶(hù)端的角度來(lái)說(shuō),我們需要能夠以 JSON 形式返回消息,以便 jQuery 可以異步輪詢(xún)并顯示新的用戶(hù)通知。為此,我們會(huì )將一些代碼添加到消息控制器中,以便可以訪(fǎng)問(wèn)這些通知(請參見(jiàn)圖 6)。
圖 6 以 JSON 形式返回消息
public JsonResult GetMessages()
{
if (User.Identity.IsAuthenticated)
{
UserTextNotification[] userToasts =
toastRepository.GetNotifications(User.Identity.Name);
object[] data =
(from UserTextNotification toast in userToasts
select new { title = toast.Title ?? Notification,
text = toast.MessageText }).ToArray();
return Json(data, JsonRequestBehavior.AllowGet);
}
else
return Json(null);
}
在 Visual Studio 2010 beta 2 下的 ASP.NET MVC 2(我們用于撰寫(xiě)本文的環(huán)境)中,如果沒(méi)有 JsonRequestBehavior.AllowGet 選項,您無(wú)法將 JSON 數據返回到 jQuery 或其他客戶(hù)端。在 ASP.NET MVC 1 中,不需要此選項?,F在,我們可以編寫(xiě) JavaScript,它每 15 秒將調用一次 GetMessages 方法并將以 toast 形式消息顯示通知(請參見(jiàn)圖 7)。
評論