1
0
mirror of synced 2024-11-05 11:09:17 +00:00
Tapeti/Tapeti.Cmd/Commands/ExportCommand.cs
Mark van Renswoude 34da354fc2 Start of Tapeti.Cmd [ci skip]
Support for basic queue to/from disk operations
2019-10-16 13:54:43 +02:00

60 lines
1.7 KiB
C#

using RabbitMQ.Client;
using Tapeti.Cmd.Serialization;
namespace Tapeti.Cmd.Commands
{
public class ExportCommand
{
public ConnectionFactory ConnectionFactory { get; set; }
public IMessageSerializer MessageSerializer { get; set; }
public string QueueName { get; set; }
public bool RemoveMessages { get; set; }
public int? MaxCount { get; set; }
public int Execute()
{
using (var connection = ConnectionFactory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
return GetMessages(channel);
}
}
}
private int GetMessages(IModel channel)
{
var messageCount = 0;
while (!MaxCount.HasValue || messageCount < MaxCount.Value)
{
var result = channel.BasicGet(QueueName, false);
if (result == null)
// No more messages on the queue
break;
messageCount++;
MessageSerializer.Serialize(new Message
{
DeliveryTag = result.DeliveryTag,
Redelivered = result.Redelivered,
Exchange = result.Exchange,
RoutingKey = result.RoutingKey,
Queue = QueueName,
Properties = result.BasicProperties,
Body = result.Body
});
if (RemoveMessages)
channel.BasicAck(result.DeliveryTag, false);
}
return messageCount;
}
}
}