public Task AddAsync(byte[] message, CancellationToken cancellationToken = default) { _batch.Add(exchange: _context.ResolvedAttribute.Exchange, routingKey: _context.ResolvedAttribute.QueueName, mandatory: false, properties: _context.ResolvedAttribute.Properties, body: message); _logger.LogDebug($"Adding message to batch for publishing..."); return(Task.CompletedTask); }
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 void Append(IBasicPublishBatch batch, ulong publishTag) { batch.Add(_exchange, _routingKey, false, _properties, _body); _body = null; PublishTag = publishTag; }
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); }
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); }
public static void Add(this IBasicPublishBatch batch, string exchange, string routingKey, bool mandatory, IBasicProperties properties, ReadOnlyMemory <byte> body) { if (batch is BasicPublishBatch batchInternal) { batchInternal.Add(exchange, routingKey, mandatory, properties, body); return; } #pragma warning disable 618 batch.Add(exchange, routingKey, mandatory, properties, body.ToArray()); #pragma warning restore 618 }
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); }
/// <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; })); } }
internal static bool Add(this IBasicPublishBatch batch, IModel model, IPublisherMessage publisherMessage) { if (publisherMessage.CancellationToken.IsCancellationRequested) { return(false); } publisherMessage.Routing.Verify(model); publisherMessage.MarkAsPublished(); var basicProperties = model.CreateBasicProperties(); basicProperties.AddMessageProperties(publisherMessage); batch.Add(publisherMessage.Routing.ExchangeName, publisherMessage.Routing.RoutingKey, false, basicProperties, publisherMessage.Serialize()); return(true); }