public void ConsumeOneByOne() { var factory = new ConnectionFactory() { HostName = "localhost" }; using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(channel); var finalDatabaseRepository = new ReactiveBatchRepository(new SQLiteDatabase(nameof(ConsumeOneByOne))); consumer.Received += (model, ea) => { var body = ea.Body; var message = Encoding.UTF8.GetString(body.ToArray()); finalDatabaseRepository.InsertData(int.Parse(message)).Wait(); channel.BasicAck(ea.DeliveryTag, false); }; channel.BasicConsume(queue: "hello", autoAck: false, consumer: consumer); Thread.Sleep(TimeSpan.FromSeconds(10)); }
public void ConsumePreFetchWithFinalRepository() { var factory = new ConnectionFactory() { HostName = "localhost" }; using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null); channel.BasicQos(0, 10000, false); var consumer = new EventingBasicConsumer(channel); var receiver = Observable.FromEventPattern <EventHandler <BasicDeliverEventArgs>, BasicDeliverEventArgs>( h => consumer.Received += h, h => consumer.Received -= h); var finalDatabaseRepository = new ReactiveBatchRepository(new SQLiteDatabase(nameof(ConsumePreFetchWithFinalRepository))); using var eventHandlingFlow = receiver .Select(@event => Observable.FromAsync(async() => { var item = int.Parse(Encoding.UTF8.GetString(@event.EventArgs.Body.ToArray())); await finalDatabaseRepository.InsertData(item).ConfigureAwait(false); channel.BasicAck(@event.EventArgs.DeliveryTag, false); })) .Merge() .Subscribe(); channel.BasicConsume(queue: "hello", autoAck: false, consumer: consumer); Thread.Sleep(TimeSpan.FromSeconds(10)); }