检查rabbitmq中是否存在指定名称的Exchange [英] Check if the Exchange with a specified name exist in rabbitmq

查看:19
本文介绍了检查rabbitmq中是否存在指定名称的Exchange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,其中有一个应用程序正在生成不同类型的有趣事件(不是命令).生产者应用程序不关心事件由谁以及如何处理.

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.

我正在实现一个消费者,他将监听一些已发布的事件并适当地处理它们.消费者应用程序想要检查发布者应用程序交换是否存在.那么,问题是如何利用spring提供的rabbit/AMQP库来检查是否存在特定名称的交换?

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.

推荐答案

使用被动声明和 RabbitTemplate;类似...

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;

这篇关于检查rabbitmq中是否存在指定名称的Exchange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆