checking of exchange, queue existence with pika

2,935 views
Skip to first unread message

Ján Kianička

unread,
Feb 6, 2015, 9:48:01 AM2/6/15
to rabbitm...@googlegroups.com
Dear RabbitMQ (python pika) Users,
I wanted to implement following scenarion:
1. Check if the exchange exists.
2. Report about existence.
3. Create missing exchange.
4. Check if queue exists.
5. Report about existence and if it does not exists, create it.
For checking I have used exchange, queue declare method with passive=True. This method raise the channel exception with reply code 404 (not found).
But then when I have tried to use the channel again for declaring the queue with passive=False, there came error from pika:
/usr/lib/python2.6/site-packages/pika/callback.py:69: UserWarning: CallbackManager.add: Duplicate callback found for "1:Queue.DeclareOk"

I have tried to study the Callbacks module, tried to remove, process the method. But the only successful workaround was to close the channel, connection.
And to reopen it in case of exception.
Does anyone have idea if it is possible to "reset" the channel to the previous correct state without reconnect?
Thank you very much for any help.
Kind regards
Jan Kianicka

Here is the code I am using now:
----
            try:
                queueResult = channel.queue_declare(queue=queue_name,
                                            durable=True,
                                            passive=True,)
                print("- %s exists on vhost."%(queue_name)
            except:
                print("%s does not exists, will be created."%(queue_name)
                connection.close()
                connection = pika.BlockingConnection(parameters)
                channel = connection.channel(1)
                queueResult = channel.queue_declare(queue=queue_name,
                                            durable=True),
                                           
                print "Created queue: %s, %s"%(queue_name)
----

Simon MacMullen

unread,
Feb 6, 2015, 9:51:54 AM2/6/15
to Ján Kianička, rabbitm...@googlegroups.com
On 06/02/15 14:48, Ján Kianička wrote:
> Dear RabbitMQ (python pika) Users,
> I wanted to implement following scenarion:
> 1. Check if the exchange exists.
> 2. Report about existence.
> 3. Create missing exchange.
> 4. Check if queue exists.
> 5. Report about existence and if it does not exists, create it.

This is not the AMQPish way to do it - the intent is that you should use
exchange.declare / queue.declare to state what you need, and the server
will create it transparently if needed. 99% of the time you don't need
to explicitly check if something exists, especially if your intent is to
create it if it doesn't.

> For checking I have used exchange, queue declare method with
> passive=True. This method raise the channel exception with reply code
> 404 (not found).

Yes.

> But then when I have tried to use the channel again for declaring the
> queue with passive=False, there came error from pika:

Once you have received a channel exception, that channel is dead. You
need to create another one.

(But really, just declare your queues / exchanges and proceed).

Cheers, Simon

Simon MacMullen

unread,
Feb 6, 2015, 10:13:43 AM2/6/15
to Ján Kianička, rabbitm...@googlegroups.com
+rabbitmq-users

In that case you should recreate the channel after the exception (or do
the declare on a "disposable" channel you create for the purpose, might
be easier).

Alternatively, if I had to solve this problem I'd be tempted to write
something that polled the HTTP API periodically and checked whether
things were as they should be.

Cheers, Simon

On 06/02/15 15:00, Ján Kianička wrote:
> Dear Simon,
> Thank you very much for quick answer. I had it before like you said,
> but I have to obey the customer and I got explicit request to
> implement this sort of logging into the administrative python scripts.
> We have concept of "deployment resources" scripts which should report
> about the changes in RabbitMQ Server.
> Kind regards
> Jan
Reply all
Reply to author
Forward
0 new messages