private async Task DeserializeMessage(CloudQueueMessage message) { Exception exception = null; BrokerQueueItem brokerQueueItem = null; if (message.AsBytes != null && message.AsBytes.Length > 0) { // Deserialize message to BrokerQueueItem try { brokerQueueItem = (BrokerQueueItem)this.formatter.Deserialize( await AzureStorageTool.GetMsgBody(this.blobContainer, message.AsBytes)); brokerQueueItem.PersistAsyncToken.AsyncToken = brokerQueueItem.Message.Headers.MessageId.ToString(); BrokerTracing.TraceVerbose( "[AzureQueueRequestFetcher] .DeserializeMessage: deserialize header={0} property={1}", brokerQueueItem.Message.Headers.MessageId, brokerQueueItem.Message.Properties); } catch (Exception e) { BrokerTracing.TraceError( "[AzureQueueRequestFetcher] .DeserializeMessage: deserialize message failed, Exception:{0}", e.ToString()); exception = e; } } if (brokerQueueItem != null && !this.requestDic.TryAdd( brokerQueueItem.PersistAsyncToken.AsyncToken.ToString(), true)) { Interlocked.Increment(ref this.pendingFetchCount); try { await this.requestQueue.DeleteMessageAsync(message); } catch (Exception e) { BrokerTracing.TraceError( "[AzureQueueRequestFetcher] .DeserializeMessage: delete duplicate message in request queue failed, Exception:{0}", e.ToString()); } } else { var copyMessage = new CloudQueueMessage(message.AsBytes); await this.pendingQueue.AddMessageAsync(copyMessage); try { await this.requestQueue.DeleteMessageAsync(message); } catch (Exception e) { BrokerTracing.TraceError( "[AzureQueueRequestFetcher] .DeserializeMessage: delete message in request queue failed, Exception:{0}", e.ToString()); } this.HandleMessageResult(new MessageResult(brokerQueueItem, exception)); } }
private async Task PeekMessageAsync() { if (this.isDisposedField) { this.RevertFetchCount(); return; } while (true) { Exception exception = null; if (this.pendingFetchCount < 1) { break; } while (this.pendingFetchCount > 0) { byte[] messageBody = null; try { if (responseList == null || responseList.Count <= index) { BrokerTracing.TraceVerbose( "[AzureQueueResponseFetch] .PeekMessageAsync: lastIndex={0}, ackIndex={1}", lastIndex, ackIndex); responseList = await AzureStorageTool.GetBatchEntityAsync( this.responseTable, this.lastIndex, this.ackIndex); index = 0; BrokerTracing.TraceVerbose( "[AzureQueueResponseFetch] .PeekMessageAsync: get batch entity count={0}", responseList.Count); } if (responseList.Count > index) { messageBody = responseList[index].Message; if (long.TryParse(responseList[index].RowKey, out var tempIndex) && tempIndex > this.lastIndex) { this.lastIndex = tempIndex; } index++; } } catch (Exception e) { BrokerTracing.TraceError( "[AzureQueueResponseFetch] .PeekMessageAsync: peek batch messages failed, Exception:{0}", e.ToString()); exception = e; } if (messageBody == null && exception == null) { BrokerTracing.TraceWarning("[AzureQueueResponseFetch] .PeekMessage: null message and exception, lastIndex = {0}, ack = {1}, Count = {2}, index = {3}.", this.lastIndex, this.ackIndex, responseList.Count, index); } else { BrokerQueueItem brokerQueueItem = null; if (messageBody != null && messageBody.Length > 0) { // Deserialize message to BrokerQueueItem try { brokerQueueItem = (BrokerQueueItem)this.formatter.Deserialize( await AzureStorageTool.GetMsgBody(this.blobContainer, messageBody)); brokerQueueItem.PersistAsyncToken.AsyncToken = brokerQueueItem.Message.Headers.RelatesTo.ToString(); BrokerTracing.TraceVerbose( "[AzureQueueResponseFetch] .PeekMessage: deserialize header={0} property={1}", brokerQueueItem.Message.Headers.RelatesTo, brokerQueueItem.Message.Properties); } catch (Exception e) { BrokerTracing.TraceError( "[AzureQueueResponseFetch] .PeekMessage: deserialize message failed, Exception:{0}", e.ToString()); exception = e; } } this.HandleMessageResult(new MessageResult(brokerQueueItem, exception)); Interlocked.Decrement(ref this.pendingFetchCount); } } this.CheckAndGetMoreMessages(); } }