1
votes

In case of non-generated names it's enough to call #'queue.declare' to get newly created queue or existing one with given name. However, when using auto-generated names (beginning with amq.gen- prefix) it's not as trivial. First of all, amq. is restricted prefix, so there is no way to call #'queue.declare'{queue=<<"amq.gen-xxx">>}.

I also tried to play with passive=true option and although I may pass restricted name, I get an exit error when queue does not exists. Following is error report:

** Handler sse_handler terminating in init/3
   for the reason exit:{{shutdown,
                        {server_initiated_close,404,
                            <<"NOT_FOUND - no queue 'amq.gen-wzPK0nIBPzr-dwtZ5Jy58V' in vhost '/'">>}},
                    {gen_server,call,
                        [<0.62.0>,
                         {call,
                             {'queue.declare',0,
                                 <<"amq.gen-wzPK0nIBPzr-dwtZ5Jy58V">>,
                                 true,false,false,false,false,[]},
                             none,<0.269.0>},
                         infinity]}}

Is there any way to solve this problem?

EDIT: Here is a short story behind this question. Disclaimer: I'm erlang newbie, so maybe there is better way to make it working :)

I have a gen_server based application holding SSE (server-side events) connections with web browsers. Each connection is bound to rabbitmq queue. SSE connection when broken, automatically tries to reconnect after given timeout - this is something that web browser supports out of the box. To reuse previously created queue I'm trying to check if queue of given name (taken from request cookie) already exists. It's all done in init callback.

1
The rabbitmqctl command can list queues. Is this help?halfelf
Thanks for answer, but I need to do checking dynamically in my application, not from the command line.Michal
I still don't understand why you need to name the queues with "amq." prefix. If you get the hint for the queue name however via cookie or what not, why don't you create your own naming convention? Then you can declare a queue whether or not it existed before.Tilman
@Tilman indeed, that solves my problem. I was using auto-generated queues only for their random names but I just figured out, I can generate random names by myself without restricted amq. prefixes. Thanks!Michal

1 Answers

0
votes

You can declare a queue with the prefix amq. if the queue already exists. You would get Declare-Ok if the queue exists or access-refused if not. (My question is why would you, though? ;)

Furthermore, you can use the passive option to check if it already exists. According to AMQP reference the server treats it as not-found error if the queue doesn't exist. In order to catch this in your Erlang client you could try something around the lines of this:

try 
    %% declare queue with passive=true
    queue_exists
catch exit:{{shutdown, {server_initiated_close,404,_},_,_} -> 
    queue_does_not_exist 
end