/// <summary> /// Cleanup current object. /// </summary> private void Cleanup() { if (this.messageSender != null) { try { this.messageSender.Close(); } catch (Exception e) { BrokerTracing.TraceError(SoaHelper.CreateTraceMessage("Proxy", "Cleanup", string.Format("Closing message retriever failed, {0}", e))); } this.messageSender = null; } foreach (var messageRetriever in this.messageRetrievers) { if (messageRetriever != null) { try { messageRetriever.Close(); } catch (Exception e) { BrokerTracing.TraceError(SoaHelper.CreateTraceMessage("Proxy", "Cleanup", string.Format("Closing message retrier failed, {0}", e))); } // messageRetriever = null; } } if (this.messageProcessorTask != null) { if (this.taskCancellation != null) { try { using (this.taskCancellation) { this.taskCancellation.Cancel(); // no need to wait here for the task could be waiting for the semaphore // this.messageProcessorTask.Wait(WaitTimeForTaskInMillisecond, this.taskCancellation.Token); } } catch (Exception e) { BrokerTracing.TraceError(SoaHelper.CreateTraceMessage("Proxy", "Cleanup", string.Format("Cancelling messageProcessorTask failed, {0}", e))); } } try { // the Task cannot be disposed when it is not in a cmpletion state // this.messageProcessorTask.Dispose(); } catch (Exception e) { BrokerTracing.TraceError(SoaHelper.CreateTraceMessage("Proxy", "Cleanup", string.Format("Disposing message processor task failed, {0}", e))); } this.messageProcessorTask = null; } if (this.semaphoreForRequest != null) { try { this.semaphoreForRequest.Dispose(); } catch (Exception e) { BrokerTracing.TraceError(SoaHelper.CreateTraceMessage("Proxy", "Cleanup", string.Format("Disposing semaphoreForRequest failed, {0}", e))); } this.semaphoreForRequest = null; } if (this.semaphoreForWorker != null) { try { this.semaphoreForWorker.Dispose(); } catch (Exception e) { BrokerTracing.TraceError(SoaHelper.CreateTraceMessage("Proxy", "Cleanup", string.Format("Disposing semaphoreForWorker failed, {0}", e))); } this.semaphoreForWorker = null; } foreach (var requestStorageClient in this.requestStorageClients) { if (requestStorageClient != null) { try { requestStorageClient.Close(); } catch (Exception e) { BrokerTracing.TraceError(SoaHelper.CreateTraceMessage("Proxy", "Cleanup", string.Format("Disposing requestStorageClient failed, {0}", e))); } // requestStorageClient = null; } } if (this.responseStorageClient != null) { try { this.responseStorageClient.Close(); } catch (Exception e) { BrokerTracing.TraceError(SoaHelper.CreateTraceMessage("Proxy", "Cleanup", string.Format("Disposing responseStorageClient failed, {0}", e))); } this.responseStorageClient = null; } this.requestMessageQueue?.Dispose(); }
/// <summary> /// Initializes a new instance of the AzureQueueManager /// </summary> /// <param name="clusterName">the cluster name</param> /// <param name="clusterHash">the cluster id hash</param> /// <param name="sessionId">the session id</param> /// <param name="azureStorageConnectionString">the Azure storage connection string</param> public AzureQueueProxy(string clusterName, int clusterHash, string sessionId, string azureStorageConnectionString) { // this works for broker ThreadPool.SetMinThreads(MinThreadsOfThreadpool, MinThreadsOfThreadpool); // improve http performance for Azure storage queue traffic ServicePointManager.DefaultConnectionLimit = 1000; ServicePointManager.Expect100Continue = false; ServicePointManager.UseNagleAlgorithm = false; this.clusterName = clusterName; this.SessionId = sessionId; this.clusterHash = clusterHash; this.azureStorageConnectionString = azureStorageConnectionString; // build the request and response queue // var requestStorageName = SoaHelper.GetRequestStorageName(clusterHash, SessionId); var requestBlobContainerName = SoaHelper.GetRequestStorageName(clusterHash, sessionId); var requestQueueNames = Enumerable.Range(0, MessageRetrieveConcurrencyLevel).Select(i => SoaHelper.GetRequestStorageName(clusterHash, sessionId) + $"-{i}").ToArray(); // this.responseStorageNamePrefix = SoaHelper.GetResponseStorageName(clusterId, SessionId); // exponential retry this.CreateStorageClient(DefaultRetryPolicy); this.requestQueues = requestQueueNames.Select(n => this.queueClient.GetQueueReference(n)).ToArray(); foreach (var requestQueue in this.requestQueues) { CreateQueueWithRetry(requestQueue); } if (sessionId == SessionStartInfo.StandaloneSessionId) { // Clear the queue in standalone session foreach (var requestQueue in this.requestQueues) { requestQueue.Clear(); } } this.requestBlobContainer = this.blobClient.GetContainerReference(requestBlobContainerName); CreateContainerWithRetry(this.requestBlobContainer); // generate the SAS token for the queue and blob container SharedAccessQueuePolicy queuePolicy = new SharedAccessQueuePolicy() { Permissions = SharedAccessQueuePermissions.Add, SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7) }; this.requestQueueUris = this.requestQueues.Select(q => string.Join(string.Empty, q.Uri, q.GetSharedAccessSignature(queuePolicy))).ToArray(); SharedAccessBlobPolicy blobPolicy = new SharedAccessBlobPolicy() { Permissions = SharedAccessBlobPermissions.Write, SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7) }; this.requestBlobUri = string.Join(string.Empty, this.requestBlobContainer.Uri, this.requestBlobContainer.GetSharedAccessSignature(blobPolicy)); this.requestStorageClients = this.requestQueues.Select(q => new AzureStorageClient(q, this.requestBlobContainer)).ToArray(); // this.responseStorageClient = new AzureStorageClient(this.responseQueue, this.responseBlobContainer); // initialize sender and retriever this.messageSender = new MessageSender(this.responseMessageClients, SenderConcurrency); this.messageRetrievers = this.requestStorageClients.Select(c => new MessageRetriever(c.Queue, RetrieverConcurrency, DefaultVisibleTimeout, this.HandleMessages, null)).ToArray(); this.receiveRequest = this.ReceiveRequest; }