Spring-amqp - 队列中的最后一条消息在我关闭服务器之前一直未被确认

时间:2016-11-27 16:21:54

标签: rabbitmq spring-amqp

我是spring-amqp的新手。我试图手动确认消息而不是使用auto-ack。

我看到管理控制台中的最后一条消息正在被取消。

image for unacked message in managemnet console. 但队列是空的。

一旦我停止服务器,最后一条消息就会得到确认。我如何处理这个问题?如何在日志中打印,消息ID /信息已被确认。

这是我实施的代码。

RabbitConfig.java:

public class RabbitMQConfig {

final static String queueName = "spring-boot";

@Bean
Queue queue() {
    return new Queue(queueName, true,false,false,null);
}

@Bean
TopicExchange exchange() {
    return new TopicExchange("spring-boot-exchange");
}

@Bean
Binding binding(Queue queue, TopicExchange exchange) {
    return BindingBuilder.bind(queue).to(exchange).with(queueName);
}

@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                         MessageListenerAdapter listenerAdapter) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setQueueNames(queueName);
    container.setMessageListener(listenerAdapter);
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    return container;
}

@Bean
Consumer receiver() {
    return new Consumer();
}

@Bean
MessageListenerAdapter listenerAdapter(Consumer receiver) {
    return new MessageListenerAdapter(receiver, "receiveMessage");
}

Consumer.java

public class Consumer实现ChannelAwareMessageListener {

@RabbitListener(queues = "spring-boot")
public void receiveMessage(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag)
        throws IOException, InterruptedException {
    Thread.sleep(500);
    channel.basicAck(tag, true);
    System.out.println(tag + "received");
}

@Override
public void onMessage(Message arg0, Channel arg1) throws Exception {
    // TODO Auto-generated method stub

}

生产者端点:

@RestController 公共类HelloController {

private final RabbitTemplate rabbitTemplate;

public HelloController(RabbitTemplate rabbitTemplate) {
    this.rabbitTemplate = rabbitTemplate;

}

// Call this end point from the postman or the browser then check in the
// rabbitmq server
@GetMapping(path = "/hello")
public String sayHello() throws InterruptedException {
    // Producer operation
    for (int i = 0; i < 100; i++) {
        Thread.sleep(500);
        rabbitTemplate.convertAndSend(RabbitMQConfig.queueName, "Hello World");
    }
    return "hello";
}

@GetMapping(path = "/hellotwo")
public String sayHellotwo() throws InterruptedException {
    // Producer operation
    for (int i = 0; i < 50; i++) {
        Thread.sleep(500);
        rabbitTemplate.convertAndSend(RabbitMQConfig.queueName, "SEcond message");

    }
    return "hellotwo";
}

1 个答案:

答案 0 :(得分:0)

你有两个监听器容器; container bean和@RabbitListener的框架创建的bean。

如果不自行运行测试,我不完全确定发生了什么,但我怀疑问题是您尝试从简单的receiveMessage拨打MessageListenerAdapter

该适配器仅用于调用具有一个参数的方法(从Message转换而来)。此外,该适配器不知道如何映射@Header参数。我怀疑交付失败,因为你正在使用MANUAL acks,因为unack'd交付和默认qos(1),不再尝试交付该容器。

你不需要你的container豆;而是配置消息监听器容器工厂以设置ack模式。请参阅the documentation

如果你是spring-amqp的新手;为什么你认为你需要手动ack?默认模式(自动)意味着容器将为您设置ack / nack(NONE是传统的兔子自动确认)。在Spring中使用手册并不常见。