public async Task <SendQueueResult> SendToQueueBatch(QueueName queueName, List <ReadOnlyMemory <byte> > dataList)
        {
            try
            {
                var connection = await GetConnection();

                using (var rabbitMQChannel = connection.CreateModel())
                {
                    var queueResult = rabbitMQChannel.QueueDeclare(queue: queueName.ToString(), durable: true, exclusive: false, autoDelete: false, arguments: null);

                    IBasicPublishBatch publishBatch = rabbitMQChannel.CreateBasicPublishBatch();

                    foreach (var data in dataList)
                    {
                        publishBatch.Add(exchange: "", routingKey: queueName.ToString(), mandatory: true, null, body: data);
                    }

                    publishBatch.Publish();
                }

                return(new SendQueueResult {
                    IsSuccess = true, ErrorMessage = ""
                });
            }
            catch (Exception ex)
            {
                return(new SendQueueResult {
                    IsSuccess = false, ErrorMessage = ex.Message
                });
            }
        }
        public (ITargetBlock <MessageItem> writer, IDataflowBlock finalBlock) GetWriter(ISource acknowledgeSource)
        {
            BatchBlock <MessageItem>    batcher = new BatchBlock <MessageItem>(_model.BatchSize);
            ActionBlock <MessageItem[]> writer  = new ActionBlock <MessageItem[]>(items =>
            {
                IBasicPublishBatch batch = _channel.CreateBasicPublishBatch();

                foreach (MessageItem item in items)
                {
                    string exchange   = _model.Exchange ?? item.Exchange;
                    string routingKey = _model.RoutingKey ?? item.RoutingKey;

                    BasicProperties basicProperties = new BasicProperties
                    {
                        Headers    = new Dictionary <string, object>(),
                        Persistent = item.Persistent
                    };

                    if (item.Created.HasValue)
                    {
                        basicProperties.Timestamp = new AmqpTimestamp(((DateTimeOffset)item.Created).ToUnixTimeSeconds());
                    }

                    if (item.Properties != null)
                    {
                        foreach ((string key, object value) in item.Properties)
                        {
                            basicProperties.Headers.Add(key, value);
                        }
                    }

                    batch.Add(exchange, routingKey, true, basicProperties, item.Data);
                }

                _logger.LogDebug("Writing {Count} messages to AMQP", items.Length);
                batch.Publish();

                acknowledgeSource.Acknowledge(items);
            });

            batcher.LinkTo(writer, new DataflowLinkOptions
            {
                PropagateCompletion = true
            });

            return(batcher, writer);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sends many messages to the broker.
        /// </summary>
        /// <param name="messages">The messages.</param>
        /// <returns></returns>
        public Task SendAsync(IEnumerable <OutboundMessage> messages)
        {
            // get message array
            OutboundMessage[] messageArr = messages.ToArray();

            if (messageArr.Length == 0)
            {
                return(Task.FromResult(true));
            }
            else if (messageArr.Length == 1)
            {
                return(SendAsync(messageArr[0]));
            }
            else
            {
                // create batch
                IBasicPublishBatch batch = _channel.CreateBasicPublishBatch();

                // add all the messages
                foreach (OutboundMessage message in messageArr)
                {
                    IBasicProperties properties = _channel.CreateBasicProperties();

                    if (message.ReplyTo != null)
                    {
                        properties.ReplyTo = message.ReplyTo;
                    }
                    if (message.Headers != null)
                    {
                        properties.Headers = message.Headers;
                    }
                    if (message.ReplyID != null)
                    {
                        properties.CorrelationId = message.ReplyID.ToString();
                    }

                    batch.Add(message.Exchange, message.RoutingKey, message.Mandatory, properties, message.Body);
                }

                return(_ctx.AskWork(delegate() {
                    batch.Publish();
                    return null;
                }));
            }
        }
        public void TestBasicPublishBatchSend()
        {
            Model.ConfirmSelect();
            Model.QueueDeclare(queue: "test-message-batch-a", durable: false);
            Model.QueueDeclare(queue: "test-message-batch-b", durable: false);
            IBasicPublishBatch batch = Model.CreateBasicPublishBatch();

            batch.Add("", "test-message-batch-a", false, null, new ReadOnlyMemory <byte>());
            batch.Add("", "test-message-batch-b", false, null, new ReadOnlyMemory <byte>());
            batch.Publish();
            Model.WaitForConfirmsOrDie(TimeSpan.FromSeconds(15));
            BasicGetResult resultA = Model.BasicGet("test-message-batch-a", true);

            Assert.NotNull(resultA);
            BasicGetResult resultB = Model.BasicGet("test-message-batch-b", true);

            Assert.NotNull(resultB);
        }
        public void TestBasicPublishBatchSendWithWrongSizeHint()
        {
            _model.ConfirmSelect();
            _model.QueueDeclare(queue: "test-message-batch-a", durable: false);
            _model.QueueDeclare(queue: "test-message-batch-b", durable: false);
            IBasicPublishBatch    batch        = _model.CreateBasicPublishBatch(1);
            ReadOnlyMemory <byte> bodyAsMemory = new byte [] {};

            batch.Add("", "test-message-batch-a", false, null, bodyAsMemory);
            batch.Add("", "test-message-batch-b", false, null, bodyAsMemory);
            batch.Publish();
            _model.WaitForConfirmsOrDie(TimeSpan.FromSeconds(15));
            BasicGetResult resultA = _model.BasicGet("test-message-batch-a", true);

            Assert.NotNull(resultA);
            BasicGetResult resultB = _model.BasicGet("test-message-batch-b", true);

            Assert.NotNull(resultB);
        }
Exemplo n.º 6
0
        public async Task TestBasicPublishBatchSend()
        {
            _model.ConfirmSelect();
            _model.QueueDeclare(queue: "test-message-batch-a", durable: false);
            _model.QueueDeclare(queue: "test-message-batch-b", durable: false);
            IBasicPublishBatch batch = _model.CreateBasicPublishBatch();

            batch.Add("", "test-message-batch-a", false, null, new ReadOnlyMemory <byte>());
            batch.Add("", "test-message-batch-b", false, null, new ReadOnlyMemory <byte>());
            batch.Publish();
            using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15));
            await _model.WaitForConfirmsOrDieAsync(cts.Token).ConfigureAwait(false);

            BasicGetResult resultA = _model.BasicGet("test-message-batch-a", true);

            Assert.NotNull(resultA);
            BasicGetResult resultB = _model.BasicGet("test-message-batch-b", true);

            Assert.NotNull(resultB);
        }
Exemplo n.º 7
0
 internal Task PublishAsync()
 {
     _batch.Publish();
     _logger.LogDebug($"Publishing messages to queue.");
     return(Task.CompletedTask);
 }