spring集成rabbitmq的confirm方法中,ack始终为false

问题描述 投票:0回答:0

春季 5.17

rabbitmq 2.18

junit 4.12

问题

我在测试可靠传递消息的

confirm
时,我是用
topic
模式来测试的。当我向交换机发送消息时,我会回拨
confirm(CorrelationData correlationData, boolean ack, String cause)
。即使
ack
发送成功,
ack
始终为false,并且有这条消息,消费者也可以消费这条消息。

代码

制作人

   <rabbitmq:queue id="spring_topic_queue01" name="spring_topic_queue01" auto-declare="true"/>
    <rabbitmq:queue id="spring_topic_queue02" name="spring_topic_queue02" auto-declare="true"/>
    <rabbitmq:topic-exchange name="spring_topic_exchange">
        <rabbitmq:bindings>
            <rabbitmq:binding pattern="*.java.#" queue="spring_topic_queue01"/>
            <rabbitmq:binding pattern="*.*.spring" queue="spring_topic_queue02"/>
            <rabbitmq:binding pattern="sql.#" queue="spring_topic_queue02"/>
        </rabbitmq:bindings>
    </rabbitmq:topic-exchange>

消费者

    <bean id="topicQueueListener01" class="com.zyl.TopicQueueListener01"/>
    <bean id="topicQueueListener02" class="com.zyl.TopicQueueListener02"/>
    
    <rabbitmq:listener-container connection-factory="connectionFactory">
    
        <rabbitmq:listener ref="topicQueueListener01" queue-names="spring_topic_queue01"/>
        <rabbitmq:listener ref="topicQueueListener02" queue-names="spring_topic_queue02"/>

    </rabbitmq:listener-container>

听众

public class TopicQueueListener01 implements MessageListener {
    @Override
    public void onMessage(Message message) {
        System.out.println("Consumer1:"+new String(message.getBody()));
    }
}

public class TopicQueueListener02 implements MessageListener {
    @Override
    public void onMessage(Message message) {
        System.out.println("Consumer2:"+new String(message.getBody()));
    }
}

测试

    @Test
    public void topicTest() throws InterruptedException {
  
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (ack)
                System.out.println("ok!\n"+cause);
            else
                System.out.println("fail!\n"+cause);
        });

        rabbitTemplate.convertAndSend("spring_topic_exchange",
                "sql.ssm.out", "Hello RabbitMQ!");

        Thread.sleep(10);
    }

输出结果

不写

Thread.sleep(10);
fail! clean channel shutdown; protocol method: #method\<channel.close\>(reply-code=200, reply-text=OK,class-id=0, method-id=0)

添加

Thread.sleep(10)
;结果是
ok! null

什么原因,一般情况下应该不用写

 Thread.sleep(10);
ack
也应该是
true

spring rabbitmq
© www.soinside.com 2019 - 2024. All rights reserved.