Example #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(ResponseMessage message, RequestReceivedEventArgs e)
        {
            if (e.ResponseQueues?.Any() != true)
            {
                return(TaskHelper.CompleteValueMinus1);
            }

            var senderOptions = Config.ResponseOptions.ServerSenderOptions;

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

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

            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 QueueServer");
                        return(_factory.CreateConnection(queue.Route));
                    }, null, 1));
                    Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}' with CorrelationId={2}", data.Count, queue.Route + "/" + queue.Name, message.CorrelationId);
                    var producer = producerPool.New();
                    producer.Publish(queue.Name, body);
                    producerPool.Store(producer);
                }
                catch (Exception ex)
                {
                    response = false;
                    Core.Log.Write(ex);
                }
            }
            return(response ? Task.FromResult(data.Count) : TaskHelper.CompleteValueMinus1);
        }
Example #2
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(ResponseMessage message, RequestReceivedEventArgs e)
        {
            if (e.ResponseQueues?.Any() != true)
            {
                return(TaskHelper.CompleteValueMinus1);
            }

            var senderOptions = Config.ResponseOptions.ServerSenderOptions;

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

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

            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());
                    });
                    Core.Log.LibVerbose("Sending {0} bytes to the Queue '{1}/{2}' with CorrelationId={3}", data.Count, queue.Route, queue.Name, message.CorrelationId);
                    producer.Publish(queue.Name, body);
                }
                catch (Exception ex)
                {
                    response = false;
                    Core.Log.Write(ex);
                }
            }
            return(response ? Task.FromResult(data.Count) : TaskHelper.CompleteValueMinus1);
        }