示例#1
0
 public void HandleMessage(NsqSharp.IMessage message)
 {
     Core.Log.LibVerbose("Message received");
     try
     {
         (var body, var correlationId) = NSQueueClient.GetFromMessageBody(message.Body);
         var rMsg = new NSQMessage
         {
             CorrelationId = correlationId,
             Body          = body
         };
         Task.Run(() => _listener.EnqueueMessageToProcessAsync(_listener.ProcessingTaskAsync, rMsg));
         Try.Do(message.Finish, false);
     }
     catch (Exception ex)
     {
         Core.Log.Write(ex);
     }
 }
示例#2
0
        private async Task ProcessingTaskAsync(byte[] data)
        {
            if (data == null)
            {
                return;
            }
            var oldContext = Core.ContextGroupName;

            try
            {
                (var body, var correlationId) = NSQueueClient.GetFromMessageBody(data);
                Counters.IncrementTotalReceivingBytes(body.Count);
                Core.Log.LibVerbose("Received {0} bytes from the Queue '{1}/{2}'", body.Count, Connection.Route, Connection.Name);
                var messageBody = ReceiverSerializer.Deserialize(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, Connection, request, body.Count, SenderSerializer, Config.RequestOptions.ServerReceiverOptions.CancellationBeforeClientResponseTimeoutInSec);
                    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, body.Count);
                    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;
            }
        }