Esempio n. 1
0
        /// <inheritdoc />
        /// <summary>
        /// On Send message data
        /// </summary>
        /// <param name="message">Response message instance</param>
        /// <param name="e">Event Args</param>
        protected override Task <int> OnSendAsync(MultiArray <byte> message, RawRequestReceivedEventArgs e)
        {
            var queues  = e.ResponseQueues;
            var replyTo = e.Metadata["ReplyTo"] ?? string.Empty;

            queues.Add(new MQConnection
            {
                Route      = e.Sender.Route,
                Parameters = e.Sender.Parameters
            });

            var senderOptions = Config.ResponseOptions.ServerSenderOptions;

            if (senderOptions is null)
            {
                throw new NullReferenceException("ServerSenderOptions is null.");
            }

            var body = NATSQueueRawClient.CreateRawMessageBody(message, e.CorrelationId, replyTo);

            var response = true;

            foreach (var queue in e.ResponseQueues)
            {
                try
                {
                    var producer = _rQueue.GetOrAdd(queue.Route, qRoute =>
                    {
                        Core.Log.LibVerbose("New Producer from QueueServer");
                        return(Extensions.InvokeWithRetry(() => _factory.CreateConnection(qRoute), 5000, int.MaxValue).WaitAsync());
                    });

                    if (!string.IsNullOrEmpty(replyTo))
                    {
                        if (string.IsNullOrEmpty(queue.Name))
                        {
                            Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}/{2}' with CorrelationId={3}", body.Length, queue.Route, replyTo, e.CorrelationId);
                            producer.Publish(replyTo, body);
                        }
                        else if (queue.Name.StartsWith(replyTo, StringComparison.Ordinal))
                        {
                            Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}/{2}' with CorrelationId={3}", body.Length, queue.Route, queue.Name + "_" + replyTo, e.CorrelationId);
                            producer.Publish(queue.Name + "_" + replyTo, body);
                        }
                    }
                    else
                    {
                        Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}/{2}' with CorrelationId={3}", body.Length, queue.Route, queue.Name, e.CorrelationId);
                        producer.Publish(queue.Name, body);
                    }
                }
                catch (Exception ex)
                {
                    response = false;
                    Core.Log.Write(ex);
                }
            }
            return(response ? Task.FromResult(message.Count) : TaskHelper.CompleteValueMinus1);
        }
Esempio n. 2
0
        private async Task ProcessingTaskAsync(byte[] data)
        {
            try
            {
                (var body, var correlationId, var name) = NATSQueueRawClient.GetFromRawMessageBody(data);

                Core.Log.LibVerbose("Received {0} bytes from the Queue '{1}/{2}'", body.Count, Connection.Route, Connection.Name);
                Counters.IncrementTotalReceivingBytes(body.Count);

                if (ResponseServer)
                {
                    var evArgs =
                        new RawResponseReceivedEventArgs(_name, body, correlationId, body.Count)
                    {
                        Metadata =
                        {
                            ["ReplyTo"] = name
                        }
                    };
                    await OnResponseReceivedAsync(evArgs).ConfigureAwait(false);
                }
                else
                {
                    var evArgs =
                        new RawRequestReceivedEventArgs(_name, Connection, body, correlationId, body.Count)
                    {
                        Metadata =
                        {
                            ["ReplyTo"] = name
                        }
                    };
                    await OnRequestReceivedAsync(evArgs).ConfigureAwait(false);
                }
                Counters.IncrementTotalMessagesProccesed();
            }
            catch (Exception ex)
            {
                Counters.IncrementTotalExceptions();
                Core.Log.Write(ex);
                Interlocked.Exchange(ref _exceptionSleep, 1);
            }
        }
        private void MessageHandler(object sender, MsgHandlerEventArgs e)
        {
            Core.Log.LibVerbose("Message received");

            try
            {
                (var body, var correlationId, var name) = NATSQueueRawClient.GetFromRawMessageBody(e.Message.Data);
                var rMsg = new NATSQMessage
                {
                    CorrelationId = correlationId,
                    Body          = body,
                    Name          = name
                };
                Task.Run(() => EnqueueMessageToProcessAsync(ProcessingTaskAsync, rMsg));
            }
            catch (Exception ex)
            {
                Core.Log.Write(ex);
            }
        }
Esempio n. 4
0
        /// <inheritdoc />
        /// <summary>
        /// On Send message data
        /// </summary>
        /// <param name="message">Response message instance</param>
        /// <param name="e">Event Args</param>
        protected override Task <int> OnSendAsync(SubArray <byte> message, RawRequestReceivedEventArgs e)
        {
            var queues = e.ResponseQueues;

            queues.Add(new MQConnection
            {
                Route      = e.Sender.Route,
                Parameters = e.Sender.Parameters
            });

            var senderOptions = Config.ResponseOptions.ServerSenderOptions;

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

            var body    = NATSQueueRawClient.CreateRawMessageBody(message, e.CorrelationId, e.Metadata["ReplyTo"]);
            var replyTo = e.Metadata["ReplyTo"];

            var response = true;

            foreach (var queue in e.ResponseQueues)
            {
                try
                {
                    var producerPool = _rQueue.GetOrAdd(queue.Route, q => new ObjectPool <IConnection>(pool =>
                    {
                        Core.Log.LibVerbose("New Producer from RawQueueServer");
                        return(_factory.CreateConnection(queue.Route));
                    }, null, 1));
                    var producer = producerPool.New();

                    if (!string.IsNullOrEmpty(replyTo))
                    {
                        if (string.IsNullOrEmpty(queue.Name))
                        {
                            Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}' with CorrelationId={2}", body.Length, queue.Route + "/" + replyTo, e.CorrelationId);
                            producer.Publish(replyTo, body);
                        }
                        else if (queue.Name.StartsWith(replyTo, StringComparison.Ordinal))
                        {
                            Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}' with CorrelationId={2}", body.Length, queue.Route + "/" + queue.Name + "_" + replyTo, e.CorrelationId);
                            producer.Publish(queue.Name + "_" + replyTo, body);
                        }
                    }
                    else
                    {
                        Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}' with CorrelationId={2}", body.Length, queue.Route + "/" + queue.Name, e.CorrelationId);
                        producer.Publish(queue.Name, body);
                    }

                    producerPool.Store(producer);
                }
                catch (Exception ex)
                {
                    response = false;
                    Core.Log.Write(ex);
                }
            }
            return(response ? Task.FromResult(message.Count) : TaskHelper.CompleteValueMinus1);
        }