Exemplo 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 async 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 = RedisQueueRawClient.CreateRawMessageBody(message, e.CorrelationId, replyTo);

            var response = true;

            foreach (var queue in e.ResponseQueues)
            {
                try
                {
                    var producer   = _rQueue.GetOrAdd(queue.Route, _ => new RedisMQConnection(queue));
                    var subscriber = await producer.GetSubscriberAsync().ConfigureAwait(false);

                    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);
                            await subscriber.PublishAsync(replyTo, body).ConfigureAwait(false);
                        }
                        else if (queue.Name.StartsWith(replyTo, StringComparison.Ordinal))
                        {
                            var nName = queue.Name + "_" + replyTo;
                            Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}/{2}' with CorrelationId={3}", body.Length, queue.Route, nName, e.CorrelationId);
                            await subscriber.PublishAsync(nName, body).ConfigureAwait(false);
                        }
                    }
                    else
                    {
                        Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}/{2}' with CorrelationId={3}", body.Length, queue.Route, queue.Name, e.CorrelationId);
                        await subscriber.PublishAsync(queue.Name, body).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    response = false;
                    Core.Log.Write(ex);
                }
            }
            return(response ? message.Count : -1);
        }
Exemplo n.º 2
0
        private async Task ProcessingTaskAsync(RedisValue data)
        {
            try
            {
                (var body, var correlationId, var name) = RedisQueueRawClient.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);
            }
        }