Пример #1
0
        internal async Task <AmqpIotOutcome> SendMessagesAsync(IEnumerable <Message> messages, TimeSpan timeout)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, nameof(SendMessagesAsync));
            }

            // List to hold messages in AMQP friendly format
            var messageList = new List <Data>();

            foreach (Message message in messages)
            {
                using AmqpMessage amqpMessage = AmqpIotMessageConverter.MessageToAmqpMessage(message);
                var data = new Data
                {
                    Value = AmqpIotMessageConverter.ReadStream(amqpMessage.ToStream()),
                };
                messageList.Add(data);
            }

            Outcome outcome;

            using (AmqpMessage amqpMessage = AmqpMessage.Create(messageList))
            {
                amqpMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat;
                outcome = await SendAmqpMessageAsync(amqpMessage, timeout).ConfigureAwait(false);
            }

            var amqpIotOutcome = new AmqpIotOutcome(outcome);

            amqpIotOutcome.ThrowIfNotAccepted();

            if (Logging.IsEnabled)
            {
                Logging.Exit(this, nameof(SendMessagesAsync));
            }

            return(amqpIotOutcome);
        }
        internal async Task <AmqpIotOutcome> SendMessagesAsync(IEnumerable <Message> messages, CancellationToken cancellationToken)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, nameof(SendMessagesAsync));
            }

            cancellationToken.ThrowIfCancellationRequested();

            // List to hold messages in AMQP friendly format
            var messageList = new List <Data>(messages.Count());

            foreach (Message message in messages)
            {
                using AmqpMessage amqpMessage = AmqpIotMessageConverter.MessageToAmqpMessage(message);
                var data = new Data
                {
                    Value = AmqpIotMessageConverter.ReadStream(amqpMessage.ToStream()),
                };
                messageList.Add(data);
            }

            using var batchMessage     = AmqpMessage.Create(messageList);
            batchMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat;
            Outcome outcome = await SendAmqpMessageAsync(batchMessage, cancellationToken).ConfigureAwait(false);

            var amqpIotOutcome = new AmqpIotOutcome(outcome);

            amqpIotOutcome.ThrowIfNotAccepted();

            if (Logging.IsEnabled)
            {
                Logging.Exit(this, nameof(SendMessagesAsync));
            }

            return(amqpIotOutcome);
        }