WEB开发网
开发学院服务器云计算 云存储:使用 Windows Azure Storage 增强应用程序... 阅读

云存储:使用 Windows Azure Storage 增强应用程序的引擎

 2010-03-26 00:00:00 来源:WEB开发网   
核心提示: 因为我们的存储机制已经确定,所以我们需要一个工作者角色作为引擎;以便在我们的电子商务站点的后台处理消息,云存储:使用 Windows Azure Storage 增强应用程序的引擎(8),为此,我们定义了一个从 Microsoft.ServiceHosting.ServiceRuntime.Ro

因为我们的存储机制已经确定,所以我们需要一个工作者角色作为引擎;以便在我们的电子商务站点的后台处理消息。为此,我们定义了一个从 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.FromMessage<ToastQueueMessage>(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; 
    } 
  } 
}

上一页  3 4 5 6 7 8 9 10  下一页

Tags:存储 使用 Windows

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接