java – Spring AMQP:想把消息放到队列中并立即发送ACK

我编写了Java应用程序,它向RabbitMQ发送消息.
然后Flume从RabbitMQ队列中选择消息.
我很感兴趣,除了水槽之外,没有人从队列中提取消息.

我的应用程序使用Spring AMQP Java插件.

问题:

使用下面的代码,消息来到RabbitMQ队列并永远保持’Unknowledges’.
据我所知,RabbitMQ正在等待来自MessageListener的ACK,但MessageListener永远不会发出ACK.
有没有人知道如何解决它?

代码:

public class MyAmqpConfiguration {

    @Autowired
    ConnectionFactory connectionFactory;

    @Bean
    public SimpleMessageListenerContainer messageListenerContainer() {

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueues(activityLogsQueue());
    container.setMessageListener(MyMessageListener());
            container.setConcurrentConsumers(3);

    return container;
    }

        @Bean(name="myTemplate")
        public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(MyMessageConverter());
        return template;
        }
}


public class MyMessageListener implements MessageListener {


   public MyMessageListener(MessageConverter converter, MyMessageHandler<MyObject> messageHandler) {
      this.converter = converter;
      this.messageHandler = messageHandler;
    }

   @Override
   public void onMessage(Message message) {
     this.messageHandler.doThings();
   }

}

public class MyMessageHandler  {

     @Autowired
     @Qualifier("myTemplate")
     RabbitTemplate template;

     @Override
     public void handleMessage(MyObject thing) {
         template.convertAndSend(exchange, routingKey, thing);
     }

}


public class MyMessageConverter extends JsonMessageConverter {

    @Override
     protected Message createMessage(Object object, MessageProperties messageProperties) { 
        //do things
     }

     @Override
     public Object fromMessage(Message message) throws MessageConversionException {
         throw new UnsupportedOperationException("fromMessage is not supported in "+this.getClass().getName());
     }


}

解决方法:

如果您不想确认每条消息,那么您可以通过执行设置SimpleMessageListenerContainer上的AcknowledgeMode

container.setAcknowledgeMode(AcknowledgeMode.NONE);

看看API reference了解更多信息.

更新:应该是AcknowledgeMode.NONE

Set to AcknowledgeMode.NONE to tell the broker not to expect any acknowledgements, and it will assume all messages are acknowledged as soon as they are sent (this is “autoack” in native Rabbit broker terms). If AcknowledgeMode.NONE then the channel cannot be transactional (so the container will fail on start up if that flag is accidentally set).

上一篇:windows10 环境下的amqp安装步骤(图文)


下一篇:python – Django芹菜任务没有完成,并一直保持待定