Beispiel #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);
        }
Beispiel #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(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 = NSQueueRawClient.CreateRawMessageBody(message, e.CorrelationId, replyTo);

            var response = true;

            foreach (var queue in e.ResponseQueues)
            {
                try
                {
                    var nsqProducer = _rQueue.GetOrAdd(queue.Route, qRoute =>
                    {
                        Core.Log.LibVerbose("New Producer from RawQueueServer");
                        return(new Producer(qRoute));
                    });

                    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 nsqProducer.PublishAsync(replyTo, body).ConfigureAwait(false);
                        }
                        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);
                            await nsqProducer.PublishAsync(queue.Name + "_" + replyTo, 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 nsqProducer.PublishAsync(queue.Name, body).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    response = false;
                    Core.Log.Write(ex);
                }
            }
            return(response ? message.Count : -1);
        }
Beispiel #3
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);
            }
        }
Beispiel #4
0
        private async Task ProcessingTaskAsync(Message message)
        {
            try
            {
                var(correlationId, name) = KafkaQueueRawClient.GetFromRawMessageHeader(message.Key);
                var body = message.Value;

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

                if (ResponseServer)
                {
                    var evArgs =
                        new RawResponseReceivedEventArgs(_name, body, correlationId, body.Length)
                    {
                        Metadata =
                        {
                            ["ReplyTo"] = name
                        }
                    };
                    await OnResponseReceivedAsync(evArgs).ConfigureAwait(false);
                }
                else
                {
                    var evArgs =
                        new RawRequestReceivedEventArgs(_name, Connection, body, correlationId, body.Length)
                    {
                        Metadata =
                        {
                            ["ReplyTo"] = name
                        }
                    };
                    await OnRequestReceivedAsync(evArgs).ConfigureAwait(false);
                }
                Counters.IncrementTotalMessagesProccesed();
            }
            catch (Exception ex)
            {
                Counters.IncrementTotalExceptions();
                Core.Log.Write(ex);
            }
        }
        private async Task ProcessingTaskAsync(NATSQMessage message)
        {
            try
            {
                Core.Log.LibVerbose("Received {0} bytes from the Queue '{1}/{2}'", message.Body.Count, Connection.Route, Connection.Name);
                Counters.IncrementTotalReceivingBytes(message.Body.Count);

                if (ResponseServer)
                {
                    var evArgs =
                        new RawResponseReceivedEventArgs(_name, message.Body, message.CorrelationId, message.Body.Count)
                    {
                        Metadata =
                        {
                            ["ReplyTo"] = message.Name
                        }
                    };
                    await OnResponseReceivedAsync(evArgs).ConfigureAwait(false);
                }
                else
                {
                    var evArgs =
                        new RawRequestReceivedEventArgs(_name, Connection, message.Body, message.CorrelationId, message.Body.Count)
                    {
                        Metadata =
                        {
                            ["ReplyTo"] = message.Name
                        }
                    };
                    await OnRequestReceivedAsync(evArgs).ConfigureAwait(false);
                }
                Counters.IncrementTotalMessagesProccesed();
            }
            catch (Exception ex)
            {
                Counters.IncrementTotalExceptions();
                Core.Log.Write(ex);
                lock (_lock)
                    _exceptionSleep = true;
            }
        }
        /// <inheritdoc />
        /// <summary>
        /// On Send message data
        /// </summary>
        /// <param name="message">Response message instance</param>
        /// <param name="e">RawRequest received 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 crId    = e.CorrelationId.ToString();
            var replyTo = e.Metadata["ReplyTo"];

            var response = true;

            foreach (var queue in queues)
            {
                try
                {
                    var rabbitQueue = _rQueue.GetOrAdd(queue.Route, q =>
                    {
                        var rq = new RabbitMQueue(queue);
                        rq.EnsureConnection();
                        return(rq);
                    });
                    if (!rabbitQueue.EnsureConnection())
                    {
                        continue;
                    }
                    rabbitQueue.EnsureExchange();
                    var props = rabbitQueue.Channel.CreateBasicProperties();
                    props.CorrelationId = crId;
                    props.Priority      = _priority;
                    props.Expiration    = _expiration;
                    props.AppId         = Core.ApplicationName;
                    props.ContentType   = SenderSerializer.MimeTypes[0];
                    props.DeliveryMode  = _deliveryMode;
                    props.Type          = _label;
                    if (!string.IsNullOrEmpty(replyTo))
                    {
                        if (string.IsNullOrEmpty(queue.Name))
                        {
                            Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}' with CorrelationId={2}", message.Count, rabbitQueue.Route + "/" + replyTo, crId);
                            rabbitQueue.Channel.BasicPublish(rabbitQueue.ExchangeName ?? string.Empty, replyTo, props, (byte[])message);
                        }
                        else if (queue.Name.StartsWith(replyTo, StringComparison.Ordinal))
                        {
                            Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}' with CorrelationId={2}", message.Count, rabbitQueue.Route + "/" + queue.Name + "_" + replyTo, crId);
                            rabbitQueue.Channel.BasicPublish(rabbitQueue.ExchangeName ?? string.Empty, queue.Name + "_" + replyTo, props, (byte[])message);
                        }
                    }
                    else
                    {
                        Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}' with CorrelationId={2}", message.Count, rabbitQueue.Route + "/" + queue.Name, crId);
                        rabbitQueue.Channel.BasicPublish(rabbitQueue.ExchangeName ?? string.Empty, queue.Name, props, (byte[])message);
                    }
                }
                catch (Exception ex)
                {
                    response = false;
                    Core.Log.Write(ex);
                }
            }
            return(response ? Task.FromResult(message.Count) : TaskHelper.CompleteValueMinus1);
        }
        /// <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);
        }
 private async Task ProcessingTaskAsync(RabbitMessage message)
 {
     if (message.Body == null)
     {
         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);
         lock (_lock)
             _exceptionSleep = true;
     }
 }
Beispiel #9
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 header = KafkaQueueRawClient.CreateRawMessageHeader(e.CorrelationId, replyTo);
            var body   = message.AsArray();

            var response = true;

            foreach (var queue in e.ResponseQueues)
            {
                try
                {
                    var producer = _rQueue.GetOrAdd(queue.Route, qRoute =>
                    {
                        Core.Log.LibVerbose("New Producer from QueueClient");
                        if (string.IsNullOrEmpty(qRoute))
                        {
                            throw new UriFormatException($"The route for the connection to {qRoute} is null.");
                        }
                        var options = new KafkaOptions(new Uri(qRoute));
                        var router  = new BrokerRouter(options);
                        return(Extensions.InvokeWithRetry(() => new Producer(router), 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);
                            await producer.SendMessageAsync(replyTo, new[] { new Message {
                                                                                 Key = header, Value = body
                                                                             } }).ConfigureAwait(false);
                        }
                        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);
                            await producer.SendMessageAsync(queue.Name + "_" + replyTo, new[] { new Message {
                                                                                                    Key = header, Value = 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 producer.SendMessageAsync(queue.Name, new[] { new Message {
                                                                                Key = header, Value = body
                                                                            } }).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    response = false;
                    Core.Log.Write(ex);
                }
            }
            return(response ? message.Count : -1);
        }