V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Vimax
V2EX  ›  Java

RabbitMQ 开启 mandatory 为 True 后 addReturnListener 监听器无法进入

  •  1
     
  •   Vimax · 2019-02-22 15:43:59 +08:00 · 1503 次点击
    这是一个创建于 1883 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是 RabbitMQ 实战指南里面的一个 mandatory 参数介绍,详见 pg56,我开启 mandatory 参数为 true 后无法进入返回确认监听器。参考了搜索引擎的其他 demo,发现依旧无效。

    没有将交换器和队列绑定,没有设置路由 key,生产者应该没有被正确路由到合适的队列。为什么会无法进入监听器的?

    环境

    • Archlinux
    • IDEA

    依赖

      <dependency>
                <groupId>com.rabbitmq</groupId>
                <artifactId>amqp-client</artifactId>
                <version>3.0.1</version>
            </dependency>
    

    代码

     public static void main(String[] args) throws IOException {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("x.x.x.x");
            factory.setPort(5672);
            factory.setUsername("root");
            factory.setPassword("root");
            // 创建连接
            Connection connection = factory.newConnection();
            // 创建信道
            Channel channel = connection.createChannel();
            // 发送一条持久化的消息
            String message = "Hello world6666";
            channel.basicPublish("normalExchange", "", true, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
            System.out.println("----------------");
            channel.addReturnListener(new ReturnListener() {
                // 无法进入确认监听器
                @Override
                public void handleReturn(int i, String s, String s1, String s2, AMQP.BasicProperties basicProperties, byte[] bytes) throws IOException {
                    String string = new String(bytes);
                    System.out.println(string);
                }
            });
            // 关闭资源
            channel.close();
            connection.close();
        }
    

    抓包:

    图一
    mq001.png 图二

    mq002.png

    2 条回复    2019-03-28 16:34:41 +08:00
    chuanzhangACE
        1
    chuanzhangACE  
       2019-03-28 16:34:06 +08:00
    不知道你解决了没,最近在开 rabbitmq,写测试时也出现了这种问题,我解决的方式时,保证在 retrun message 之前 channel 不关闭,在发送消息之前绑定监听器。
    chuanzhangACE
        2
    chuanzhangACE  
       2019-03-28 16:34:41 +08:00
    @chuanzhangACE 看, 是
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   917 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 20:48 · PVG 04:48 · LAX 13:48 · JFK 16:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.