Henri
Henri

Reputation: 1761

RabbitMQ auto_delete options is not working

I'm following the tutorial #2 on RabbitMQ (https://www.rabbitmq.com/tutorials/tutorial-two-python.html), but I want to make a slight difference.

I want my Queue auto_delete once it's 100% consumed, but it's not. Where am I wrong?

My Producer Code:

var amqp = require('amqplib/callback_api');


const RABBIT_MQ_HOST = 'amqp://localhost'

amqp.connect(RABBIT_MQ_HOST, function(error0, connection){
    if(error0){
        throw error0;
    }
    connection.createChannel(function(error1, channel){
        if(error1){
            throw error1;
        }

        const queue = 'task_queue';
        const msg = process.argv.slice(2).join(' ') || "Hello World!";

        channel.assertQueue(queue, {
            durable: true,
            auto_delete: true,
        });

        channel.sendToQueue(queue, Buffer.from(msg), {
            persistent: true,
        });
        console.log('[x] Sent %s', msg);
    });
    setTimeout(function() {
        connection.close();
        process.exit(0)
        }, 500);
});

My Consumer Code:

var amqp = require('amqplib/callback_api');

const RABBIT_MQ_HOST = 'amqp://localhost'

amqp.connect(RABBIT_MQ_HOST, function(error, connection) {
    connection.createChannel(function(error, channel) {
        var queue = 'task_queue';

        channel.assertQueue(queue, {
            durable: true,
            auto_delete: true,
        });
        channel.prefetch(1);
        console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", queue);
        channel.consume(queue, function(msg) {
            var secs = msg.content.toString().split('.').length - 1;

            console.log(" [x] Received %s", msg.content.toString());
            setTimeout(function() {
                console.log(" [x] Done");
                channel.ack(msg);
            }, secs * 1000);
        }, {
            noAck: false
        });
    });
});

After launching the Producer code, then the Consumer code, at the end my Queue is empty, but not deleted.

What am I doing wrong ?

Thanks

Upvotes: 0

Views: 1205

Answers (1)

Gabriele Santomaggio
Gabriele Santomaggio

Reputation: 22750

The auto-delete queue is deleted when its last consumer is cancelled and not when the queue is empty.

See the documentation

An auto-delete queue will be deleted when its last consumer is cancelled (e.g. using the basic.cancel in AMQP 0-9-1) or gone (closed channel or connection, or lost TCP connection with the server).

Upvotes: 2

Related Questions