新聞中心

        EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于云存儲(chǔ)實(shí)現(xiàn)用Windows Azure Storage增強(qiáng)應(yīng)用程序的引擎

        基于云存儲(chǔ)實(shí)現(xiàn)用Windows Azure Storage增強(qiáng)應(yīng)用程序的引擎

        作者: 時(shí)間:2018-09-11 來(lái)源:網(wǎng)絡(luò) 收藏

        本文引用地址:http://www.104case.com/article/201809/388914.htm

        因?yàn)槲覀兊拇鎯?chǔ)機(jī)制已經(jīng)確定,所以我們需要一個(gè)工作者角色作為引擎;以便在我們的電子商務(wù)站點(diǎn)的后臺(tái)處理消息。為此,我們定義了一個(gè)從 Microsoft.ServiceHosting.ServiceRuntime.RoleEntryPoint 類(lèi)繼承的類(lèi),并將其與云服務(wù)項(xiàng)目中的工作者角色關(guān)聯(lián)(請(qǐng)參見(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; 
            } 
          } 
        }

        讓我們看一下工作者角色代碼。在初始化和設(shè)置所需的隊(duì)列和表存儲(chǔ)之后,此代碼將進(jìn)入一個(gè)循環(huán)。每 10 秒鐘,它就會(huì)處理一次隊(duì)列中的消息。每次我們通過(guò)處理循環(huán)時(shí)都將獲取隊(duì)列中的消息,直到最終返回 null,這表示隊(duì)列為空。

          您從隊(duì)列中看到的消息永遠(yuǎn)不會(huì)超過(guò) 20 個(gè),如果不信,您可以反復(fù)嘗試來(lái)驗(yàn)證一下。對(duì)隊(duì)列進(jìn)行的任何處理都有時(shí)間限制,必須在該時(shí)間范圍內(nèi)對(duì)每個(gè)隊(duì)列消息執(zhí)行有意義的操作,否則隊(duì)列消息將被視為超時(shí),并在隊(duì)列中顯示備份,以便可以由其他工作者來(lái)處理此消息。每個(gè)消息都會(huì)作為用戶(hù)通知添加到表存儲(chǔ)中。關(guān)于工作者角色需要記住的重要一點(diǎn)是:一旦入口點(diǎn)方法完成,工作者角色也就結(jié)束了。這就是您需要在一個(gè)循環(huán)中保持邏輯運(yùn)行的原因。

          從客戶(hù)端的角度來(lái)說(shuō),我們需要能夠以 JSON 形式返回消息,以便 jQuery 可以異步輪詢(xún)并顯示新的用戶(hù)通知。為此,我們會(huì)將一些代碼添加到消息控制器中,以便可以訪問(wèn)這些通知(請(qǐng)參見(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 選項(xiàng),您無(wú)法將 JSON 數(shù)據(jù)返回到 jQuery 或其他客戶(hù)端。在 ASP.NET MVC 1 中,不需要此選項(xiàng)。現(xiàn)在,我們可以編寫(xiě) JavaScript,它每 15 秒將調(diào)用一次 GetMessages 方法并將以 toast 形式消息顯示通知(請(qǐng)參見(jiàn)圖 7)。



        關(guān)鍵詞: Windows Storage Azure 云存儲(chǔ)

        評(píng)論


        相關(guān)推薦

        技術(shù)專(zhuān)區(qū)

        關(guān)閉
        主站蜘蛛池模板: 肥东县| 五指山市| 泸州市| 英吉沙县| 江达县| 惠来县| 河北区| 易门县| 滨州市| 四川省| 武威市| 开原市| 钟山县| 达州市| 江油市| 丹巴县| 清新县| 洛隆县| 仪陇县| 定日县| 民乐县| 华坪县| 新干县| 大石桥市| 娱乐| 元谋县| 陆丰市| 鹿泉市| 康定县| 神池县| 高密市| 前郭尔| 成武县| 尼木县| 嘉鱼县| 荔波县| 禄劝| 固镇县| 襄城县| 江门市| 瑞丽市|