Ejemplo n.º 1
0
        private async Task ProcessingTaskAsync(RabbitMessage message)
        {
            if (message.Body is null)
            {
                RabbitMessage.Return(message);
                return;
            }
            var oldContext = Core.ContextGroupName;

            try
            {
                Counters.IncrementTotalReceivingBytes(message.Body.Length);
                Core.Log.LibVerbose("Received {0} bytes from the Queue '{1}/{2}'", message.Body.Length, _receiver.Route, _receiver.Name);
                var messageBody = ReceiverSerializer.Deserialize(message.Body, _messageType);
                switch (messageBody)
                {
                case RequestMessage request when request.Header != null:
                    if (!string.IsNullOrEmpty(request.Header.ContextGroupName))
                    {
                        Core.ContextGroupName = request.Header.ContextGroupName;
                    }
                    request.Header.ApplicationReceivedTime = Core.Now;
                    Counters.IncrementReceivingTime(request.Header.TotalTime);
                    if (request.Header.ClientName != Config.Name)
                    {
                        Core.Log.Warning("The Message Client Name '{0}' is different from the Server Name '{1}'", request.Header.ClientName, Config.Name);
                    }
                    var evArgs = new RequestReceivedEventArgs(_name, _receiver, request, message.Body.Length, SenderSerializer, Config.RequestOptions.ServerReceiverOptions.CancellationBeforeClientResponseTimeoutInSec);
                    evArgs.Metadata["ReplyTo"]   = message.Properties.ReplyTo;
                    evArgs.Metadata["MessageId"] = message.Properties.MessageId;
                    if (request.Header.ResponseQueue != null)
                    {
                        evArgs.ResponseQueues.Add(request.Header.ResponseQueue);
                    }
                    await OnRequestReceivedAsync(evArgs).ConfigureAwait(false);

                    break;

                case ResponseMessage response when response.Header != null:
                    if (!string.IsNullOrEmpty(response.Header.Request.Header.ContextGroupName))
                    {
                        Core.ContextGroupName = response.Header.Request.Header.ContextGroupName;
                    }
                    response.Header.Response.ApplicationReceivedTime = Core.Now;
                    Counters.IncrementReceivingTime(response.Header.Response.TotalTime);
                    var evArgs2 = new ResponseReceivedEventArgs(_name, response, message.Body.Length);
                    evArgs2.Metadata["ReplyTo"]   = message.Properties.ReplyTo;
                    evArgs2.Metadata["MessageId"] = message.Properties.MessageId;
                    await OnResponseReceivedAsync(evArgs2).ConfigureAwait(false);

                    break;
                }
                Counters.IncrementTotalMessagesProccesed();
            }
            catch (Exception ex)
            {
                Counters.IncrementTotalExceptions();
                Core.Log.Write(ex);
                Interlocked.Exchange(ref _exceptionSleep, 1);
            }
            finally
            {
                Core.ContextGroupName = oldContext;
                RabbitMessage.Return(message);
            }
        }
Ejemplo n.º 2
0
 private async Task ProcessingTaskAsync(RabbitMessage message)
 {
     if (message.Body is null)
     {
         RabbitMessage.Return(message);
         return;
     }
     try
     {
         Core.Log.LibVerbose("Received {0} bytes from the Queue '{1}/{2}'", message.Body.Length, _receiver.Route, _receiver.Name);
         Counters.IncrementTotalReceivingBytes(message.Body.Length);
         if (ResponseServer)
         {
             var evArgs =
                 new RawResponseReceivedEventArgs(_name, message.Body, message.CorrelationId, message.Body.Length)
             {
                 Metadata =
                 {
                     ["AppId"]           = message.Properties.AppId,
                     ["ContentEncoding"] = message.Properties.ContentEncoding,
                     ["ContentType"]     = message.Properties.ContentType,
                     ["DeliveryMode"]    = message.Properties.DeliveryMode.ToString(),
                     ["Expiration"]      = message.Properties.Expiration,
                     ["Priority"]        = message.Properties.Priority.ToString(),
                     ["Timestamp"]       = message.Properties.Timestamp.ToString(),
                     ["Type"]            = message.Properties.Type,
                     ["UserId"]          = message.Properties.UserId,
                     ["ReplyTo"]         = message.Properties.ReplyTo,
                     ["MessageId"]       = message.Properties.MessageId
                 }
             };
             await OnResponseReceivedAsync(evArgs).ConfigureAwait(false);
         }
         else
         {
             var evArgs =
                 new RawRequestReceivedEventArgs(_name, _receiver, message.Body, message.CorrelationId, message.Body.Length)
             {
                 Metadata =
                 {
                     ["AppId"]           = message.Properties.AppId,
                     ["ContentEncoding"] = message.Properties.ContentEncoding,
                     ["ContentType"]     = message.Properties.ContentType,
                     ["DeliveryMode"]    = message.Properties.DeliveryMode.ToString(),
                     ["Expiration"]      = message.Properties.Expiration,
                     ["Priority"]        = message.Properties.Priority.ToString(),
                     ["Timestamp"]       = message.Properties.Timestamp.ToString(),
                     ["Type"]            = message.Properties.Type,
                     ["UserId"]          = message.Properties.UserId,
                     ["ReplyTo"]         = message.Properties.ReplyTo,
                     ["MessageId"]       = message.Properties.MessageId
                 }
             };
             await OnRequestReceivedAsync(evArgs).ConfigureAwait(false);
         }
         Counters.IncrementTotalMessagesProccesed();
     }
     catch (Exception ex)
     {
         Counters.IncrementTotalExceptions();
         Core.Log.Write(ex);
         Interlocked.Exchange(ref _exceptionSleep, 1);
     }
     finally
     {
         RabbitMessage.Return(message);
     }
 }