Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the Exchange with a specified name exist in rabbitmq

I have a scenario where there is an application which is generating different types of interesting events (not commands). The producer application does not care about by whom and how the events get processed.

I am implementing a consumer who will listen to few of the published events and process them appropriately. The consumer application wants to check if the publisher application exchange exists or not. So, the question is how to check if exchange with specific name exists or not by making use of spring provided rabbit/AMQP libraries?

I guess, this could be handled indirectly by trying to bind a queue to a non-existing exchange resulting in an exception. I am looking for better way to handle this situation.

like image 921
Anand Patel Avatar asked Aug 08 '14 10:08

Anand Patel


1 Answers

Use passive declaration and a RabbitTemplate; something like...

final String exchange = "foo";
boolean exists rabbitTemplate.execute(new ChannelCallback<DeclareOk>() {
        @Override
        public DeclareOk doInRabbit(Channel channel) throws Exception {
            try {
                return channel.exchangeDeclarePassive(exchange);
            }
            catch (Exception e) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Exchange '" + exchange + "' does not exist");
                }
                return null;
            }
        }
    }) != null;
like image 114
Gary Russell Avatar answered Oct 13 '22 00:10

Gary Russell