2016年9月8日 星期四

[研究] 背景工作角色 (WorkerRole) 和 服務匯流排佇列的背景工作角色 (WorkerRoleWithSBQueue) 比較

[研究] 背景工作角色 (WorkerRole) 和 服務匯流排佇列的背景工作角色 (WorkerRoleWithSBQueue) 比較

Visual Studio 2015 with Update 3 + Windows Azure SDK 2.9.5 + .NET 4.5.2

Double Click 可看 100% 圖




左邊:WorkerRole1 的 WorkerRole.cs
右邊:WorkerRoleWithSBQueue1 的 WorkerRole.cs

D:\CodeTemp\AzureWorkRoleCompare\WorkerRole1\WorkerRole.csD:\CodeTemp\AzureWorkRoleCompare\WorkerRoleWithSBQueue1\WorkerRole.cs
using System;using System;
using System.Collections.Generic;using System.Collections.Generic;
using System.Diagnostics;using System.Diagnostics;
using System.Linq;using System.Linq;
using System.Net;using System.Net;
using System.Threading;using System.Threading;
using System.Threading.Tasks; using Microsoft.ServiceBus; 
 using Microsoft.ServiceBus.Messaging 
using Microsoft.WindowsAzure;using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics  
using Microsoft.WindowsAzure.ServiceRuntime;using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.Storage  
  
namespace WorkerRole1 namespace WorkerRoleWithSBQueue1 
{{
   public class WorkerRole : RoleEntryPoint   public class WorkerRole : RoleEntryPoint
   {   {
       private readonly CancellationTokenSource cancellationTokenSource = newCancellationTokenSource();        // 佇列的名稱 
       private readonly ManualResetEvent runCompleteEvent = new ManualResetEvent(false);        const string QueueName = "ProcessingQueue"; 
  
        // QueueClient 可進行安全對話。已建議您進行快取,  
        // 而非在每次要求時重新建立  
        QueueClient Client 
        ManualResetEvent CompletedEvent = new ManualResetEvent(false) 
  
       public override void Run()       public override void Run()
       {       {
           Trace.TraceInformation("WorkerRole1 is running");            Trace.WriteLine("開始處理訊息"); 
  
           try            // 起始訊息幫浦,且已叫用每則已收到的訊息回呼,用戶端結果的呼叫會停止幫浦。 
           {            Client.OnMessage((receivedMessage) => 
               this.RunAsync(this.cancellationTokenSource.Token).Wait();                { 
           }                    try 
           finally                    { 
           {                        // 處理訊息 
               this.runCompleteEvent.Set();                        Trace.WriteLine("處理 Service Bus 訊息: " +receivedMessage.SequenceNumber.ToString()); 
           }                   }
                    catch  
                     
                        // 處理任何正在這裡處理特定例外狀況的訊息  
                     
                }) 
  
            CompletedEvent.WaitOne() 
       }       }
  
       public override bool OnStart()       public override bool OnStart()
       {       {
           // 設定同時連線的數目上限             // 設定並行連線數目上限  
           ServicePointManager.DefaultConnectionLimit = 12;           ServicePointManager.DefaultConnectionLimit = 12;
  
           // 如需有關處理組態變更的資訊,            // 若不存在,請建立佇列 
           // see the MSDN topic at https://go.microsoft.com/fwlink/?LinkId=166357.            string connectionString =CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString"); 
            var namespaceManager =NamespaceManager.CreateFromConnectionString(connectionString) 
           bool result = base.OnStart();            if (!namespaceManager.QueueExists(QueueName)) 
             
           Trace.TraceInformation("WorkerRole1 has been started");                namespaceManager.CreateQueue(QueueName); 
             
  
           return result;            // 起始到 Service Bus 佇列的連線 
            Client = QueueClient.CreateFromConnectionString(connectionString, QueueName) 
            return base.OnStart() 
       }       }
  
       public override void OnStop()       public override void OnStop()
       {       {
           Trace.TraceInformation("WorkerRole1 is stopping");            // 關閉到 Service Bus 佇列的連線 
            Client.Close() 
           this.cancellationTokenSource.Cancel();            CompletedEvent.Set(); 
           this.runCompleteEvent.WaitOne()  
  
           base.OnStop();           base.OnStop();
  
           Trace.TraceInformation("WorkerRole1 has stopped")  
         
  
       private async Task RunAsync(CancellationToken cancellationToken  
         
           // TODO: 依您自己的邏輯取代下列項目。   
           while (!cancellationToken.IsCancellationRequested  
             
               Trace.TraceInformation("Working")  
               await Task.Delay(1000)  
             
       }       }
   }   }
}  
  

左邊:WorkerRole1 的 app.config
右邊:WorkerRoleWithSBQueue1 的 app.config

D:\CodeTemp\AzureWorkRoleCompare\WorkerRole1\app.configD:\CodeTemp\AzureWorkRoleCompare\WorkerRoleWithSBQueue1\App.config
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?> 
<configuration> <configuration> 
 <system.diagnostics>  <system.diagnostics> 
   <trace>    <trace> 
     <listeners>      <listeners> 
       <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"         <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">  
         name="AzureDiagnostics">   
         <filter type="" />          <filter type="" /> 
       </add>        </add> 
     </listeners>      </listeners> 
   </trace>    </trace> 
 </system.diagnostics>  </system.diagnostics> 
  <system.serviceModel>  
    <extensions>  
      <!-- 在此延伸模組區段中,我們將介紹所有已知的服務匯流排延伸模組。使用者可以移除他們不需要的延伸模組。 -->  
      <behaviorExtensions>  
        <add name="connectionStatusBehavior"type="Microsoft.ServiceBus.Configuration.ConnectionStatusElement, Microsoft.ServiceBus, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />  
        <add name="transportClientEndpointBehavior"type="Microsoft.ServiceBus.Configuration.TransportClientEndpointBehaviorElement, Microsoft.ServiceBus, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />  
        <add name="serviceRegistrySettings"type="Microsoft.ServiceBus.Configuration.ServiceRegistrySettingsElement, Microsoft.ServiceBus, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />  
      </behaviorExtensions>  
      <bindingElementExtensions>  
        <add name="netMessagingTransport"type="Microsoft.ServiceBus.Messaging.Configuration.NetMessagingTransportExtensionElement, Microsoft.ServiceBus, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />  
      </bindingElementExtensions>  
      <bindingExtensions>  
        <add name="netMessagingBinding"type="Microsoft.ServiceBus.Messaging.Configuration.NetMessagingBindingCollectionElement, Microsoft.ServiceBus, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />  
      </bindingExtensions>  
    </extensions>  
  </system.serviceModel>  
 <runtime>  <runtime> 
   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly>      <dependentAssembly> 
       <assemblyIdentity name="Microsoft.Data.Services.Client"publicKeyToken="31bf3856ad364e35" culture="neutral" />        <assemblyIdentity name="Microsoft.Data.Services.Client" publicKeyToken="31bf3856ad364e35"culture="neutral" /> 
       <bindingRedirect oldVersion="0.0.0.0-5.7.0.0" newVersion="5.7.0.0" />        <bindingRedirect oldVersion="0.0.0.0-5.7.0.0" newVersion="5.7.0.0" /> 
     </dependentAssembly>      </dependentAssembly> 
     <dependentAssembly>      <dependentAssembly> 
       <assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35"culture="neutral" />        <assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35"culture="neutral" /> 
       <bindingRedirect oldVersion="0.0.0.0-5.7.0.0" newVersion="5.7.0.0" />        <bindingRedirect oldVersion="0.0.0.0-5.7.0.0" newVersion="5.7.0.0" /> 
     </dependentAssembly>      </dependentAssembly> 
     <dependentAssembly>      <dependentAssembly> 
       <assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35"culture="neutral" />        <assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35"culture="neutral" /> 
       <bindingRedirect oldVersion="0.0.0.0-5.7.0.0" newVersion="5.7.0.0" />        <bindingRedirect oldVersion="0.0.0.0-5.7.0.0" newVersion="5.7.0.0" /> 
     </dependentAssembly>      </dependentAssembly> 
     <dependentAssembly>      <dependentAssembly> 
       <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"culture="neutral" />        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"culture="neutral" /> 
       <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" /> 
     </dependentAssembly>      </dependentAssembly> 
   </assemblyBinding>    </assemblyBinding> 
 </runtime>  </runtime> 
  <appSettings>  
    <!-- Service Bus specific app setings for messaging connections -->  
    <add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://[your namespace].servicebus.windows.net;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[your secret]" />  
  </appSettings>  
</configuration> </configuration> 

(完)

沒有留言:

張貼留言