新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 基于云存儲實現用Windows Azure Storage增強應用程序的引擎

        基于云存儲實現用Windows Azure Storage增強應用程序的引擎

        作者: 時間:2010-04-17 來源:網絡 收藏

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

        因為我們的機制已經確定,所以我們需要一個工作者角色作為;以便在我們的電子商務站點的后臺處理消息。為此,我們定義了一個從 Microsoft.ServiceHosting.ServiceRuntime.RoleEntryPoint 類繼承的類,并將其與云服務項目中的工作者角色關聯(請參見圖 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; 
            } 
          } 
        }

        讓我們看一下工作者角色代碼。在初始化和設置所需的隊列和表之后,此代碼將進入一個循環。每 10 秒鐘,它就會處理一次隊列中的消息。每次我們通過處理循環時都將獲取隊列中的消息,直到最終返回 null,這表示隊列為空。

          您從隊列中看到的消息永遠不會超過 20 個,如果不信,您可以反復嘗試來驗證一下。對隊列進行的任何處理都有時間限制,必須在該時間范圍內對每個隊列消息執行有意義的操作,否則隊列消息將被視為超時,并在隊列中顯示備份,以便可以由其他工作者來處理此消息。每個消息都會作為用戶通知添加到表中。關于工作者角色需要記住的重要一點是:一旦入口點方法完成,工作者角色也就結束了。這就是您需要在一個循環中保持邏輯運行的原因。

          從客戶端的角度來說,我們需要能夠以 JSON 形式返回消息,以便 jQuery 可以異步輪詢并顯示新的用戶通知。為此,我們會將一些代碼添加到消息控制器中,以便可以訪問這些通知(請參見圖 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(我們用于撰寫本文的環境)中,如果沒有 JsonRequestBehavior.AllowGet 選項,您無法將 JSON 數據返回到 jQuery 或其他客戶端。在 ASP.NET MVC 1 中,不需要此選項。現在,我們可以編寫 JavaScript,它每 15 秒將調用一次 GetMessages 方法并將以 toast 形式消息顯示通知(請參見圖 7)。



        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 阿拉尔市| 浦东新区| 汉寿县| 德兴市| 屏南县| 宣化县| 嵩明县| 鄂托克前旗| 玉屏| 南和县| 南康市| 紫阳县| 满城县| 博湖县| 山西省| 葵青区| 泽库县| 白银市| 兴化市| 武川县| 玉田县| 陆河县| 南康市| 大城县| 丹巴县| 临汾市| 新田县| 出国| 叙永县| 海兴县| 进贤县| 荣昌县| 大名县| 北宁市| 开平市| 全州县| 平远县| 岑溪市| 嘉禾县| 聊城市| 临颍县|