public (ITargetBlock <MessageItem> writer, IDataflowBlock finalBlock) GetWriter(ISource acknowledgeSource) { // Prepare file name format string nameFormat = $"m-{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}-"; int idx = 0; ActionBlock <MessageItem> block = new ActionBlock <MessageItem>(item => { string name = nameFormat + idx++; ZipArchiveEntry entry = _zip.CreateEntry(name + DataExtension, CompressionLevel.Optimal); using (Stream entryFs = entry.Open()) entryFs.Write(item.Data); entry = _zip.CreateEntry(name + MetaExtension, CompressionLevel.Optimal); using (Stream entryFs = entry.Open()) Serialization.Serialize(entryFs, item); acknowledgeSource.Acknowledge(new List <MessageItem> { item }); }); return(block, block); }
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); }