For a professional need, I used RabbitMQ to streamline tasks. The frameworks and libraries used are:
- RabbitMQ.Client 7.1.2
- .NET 8.0My task is basic: copy files from one location to another.So I implemented a consumer that receives and executes tasks from a specific queue.So far so good, the task arrives and executes, except that..:
- if I set the autoAck:true option, even if the task is not correctly executed, the message still disappears from the queue
- if I set the autoAck:false option, no message is correctly consumed.The desired result is: if a task is not executed, I must be able to send it back to the queue, and if so, the task disappears.Any help would be greatly appreciated.Thanks in advanceHere's the method that handles the arrival of messages:
public async Task TurnOnListener() { if(_config != null) { var factory = new ConnectionFactory() { Uri = new(_config.Uri), ClientProvidedName = _config.ClientProvidedName }; using (var connection = await factory.CreateConnectionAsync()) { using (var channel = await connection.CreateChannelAsync()) { await channel.ExchangeDeclareAsync(exchange: _config.ExchangeName, ExchangeType.Direct); await channel.QueueDeclareAsync(queue: _config.QueueName, durable: false, exclusive: false, autoDelete: false, arguments: null); await channel.QueueBindAsync(queue: _config.QueueName, exchange: _config.ExchangeName, routingKey: _config.RoutingKey, null); await channel.BasicQosAsync(0, 1, false); var consumer = new AsyncEventingBasicConsumer(channel); consumer.ReceivedAsync += async (model, ea) => { bool done = await HandleTask(channel, ea); if (done) { await channel.BasicAckAsync(deliveryTag: ea.DeliveryTag, multiple: false); } else { await channel.BasicNackAsync(deliveryTag: ea.DeliveryTag, false, true); } }; string consumerTag = await channel.BasicConsumeAsync(queue: _config.QueueName, autoAck: true, consumer: consumer); await channel.CloseAsync(); } await connection.CloseAsync(); } } else { //Console.WriteLine($""); } }