示例#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
        /// <inheritdoc />
        /// <summary>
        /// On Send message data
        /// </summary>
        /// <param name="message">Response message instance</param>
        /// <param name="e">Event Args</param>
        protected override async Task <int> OnSendAsync(ResponseMessage message, RequestReceivedEventArgs e)
        {
            if (e.ResponseQueues?.Any() != true)
            {
                return(-1);
            }

            var senderOptions = Config.ResponseOptions.ServerSenderOptions;

            if (senderOptions == null)
            {
                throw new ArgumentNullException("ServerSenderOptions");
            }

            var data = SenderSerializer.Serialize(message);
            var body = NSQueueClient.CreateMessageBody(data, message.CorrelationId);

            var response = true;

            foreach (var queue in e.ResponseQueues)
            {
                try
                {
                    var nsqProducerPool = _rQueue.GetOrAdd(queue.Route, q => new ObjectPool <Producer>(pool =>
                    {
                        Core.Log.LibVerbose("New Producer from QueueServer");
                        return(new Producer(q));
                    }, null, 1));
                    Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}' with CorrelationId={2}", data.Count, queue.Route + "/" + queue.Name, message.CorrelationId);
                    var nsqProducer = nsqProducerPool.New();
                    await nsqProducer.PublishAsync(queue.Name, body).ConfigureAwait(false);

                    nsqProducerPool.Store(nsqProducer);
                }
                catch (Exception ex)
                {
                    response = false;
                    Core.Log.Write(ex);
                }
            }
            return(response ? data.Count : -1);
        }
示例#3
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;
            }
        }