V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Vimax
V2EX  ›  Java

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

  •  1
     
  •   Vimax · Feb 22, 2019 · 1995 views
    This topic created in 2635 days ago, the information mentioned may be changed or developed.

    这是 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 replies    2019-03-28 16:34:41 +08:00
    chuanzhangACE
        1
    chuanzhangACE  
       Mar 28, 2019
    不知道你解决了没,最近在开 rabbitmq,写测试时也出现了这种问题,我解决的方式时,保证在 retrun message 之前 channel 不关闭,在发送消息之前绑定监听器。
    chuanzhangACE
        2
    chuanzhangACE  
       Mar 28, 2019
    @chuanzhangACE 看, 是
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5682 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 07:59 · PVG 15:59 · LAX 00:59 · JFK 03:59
    ♥ Do have faith in what you're doing.