简体   繁体   English

Spring AMQP:希望将消息放入队列并立即发送ACK

[英]Spring AMQP: would like to put message to queue and send ACK immediately

I wrote Java application, that sends messages to RabbitMQ. 我编写了Java应用程序,它向RabbitMQ发送消息。 Then Flume picks messages up from RabbitMQ queue. 然后Flume从RabbitMQ队列中选择消息。 I'm interested that nobody pulls messages from the queue, except flume. 我很感兴趣,除了水槽之外,没有人从队列中提取消息。

My application uses Spring AMQP Java plugin. 我的应用程序使用Spring AMQP Java插件。

The problem: 问题:

With the code below, message comes to RabbitMQ queue and stays 'Unknowledges' for ever. 使用下面的代码,消息来到RabbitMQ队列并永远保持'Unknowledges'。 As I understand, RabbitMQ is waiting for ACK from MessageListener, but MessageListener will never ACK. 据我所知,RabbitMQ正在等待来自MessageListener的ACK,但MessageListener永远不会发出ACK。 Does anybody have idea how to fix it? 有没有人知道如何解决它?

The code: 代码:

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());
     }


}

If you don't want to have to ACK each message then you can set the AcknowledgeMode on the SimpleMessageListenerContainer by doing 如果您不想确认每条消息,那么您可以通过执行设置SimpleMessageListenerContainer上的AcknowledgeMode

container.setAcknowledgeMode(AcknowledgeMode.NONE);

take a look at the API reference for more info. 有关详细信息,请参阅API参考

Update: Should be AcknowledgeMode.NONE 更新:应该是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). 设置为AcknowledgeMode.NONE以告诉代理不要期待任何确认,并且它将假定所有消息一发送就被确认(这是本机Rabbit代理术语中的“autoack”)。 If AcknowledgeMode.NONE then the channel cannot be transactional (so the container will fail on start up if that flag is accidentally set). 如果AcknowledgeMode.NONE则通道不能是事务性的(因此如果意外设置了该标志,容器将在启动时失败)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring AMQP-将消息返回到队列的开头 - Spring AMQP - return the message back to the beginning of the queue 如果使用事务处理,使用Spring Integration从队列中读取的消息为ACK, - When a message read from a queue with Spring Integration is ACK in case of Transacted 如何异步发送消息以排队它们而不等待使用java中的rabbitmq在spring amqp中回复每条消息? - How to send messages asynchronously to queue them up without waiting for reply of each message in spring amqp using rabbitmq in java? 为什么从已填充的队列调度Spring AMQP Message会有延迟? - Why is there a delay in Spring AMQP Message dispatching from a filled Queue? Spring-AMQP是否基于JVM重新排队消息计数? - Is Spring-AMQP re-queue message count JVM based? 当消息放入 MQ(消息队列)时,生产者是否等待 MQ 管理器的 ACK? - When a message is put in a MQ (Message Queue) does the producer wait for ACK from the MQ Manager? Spring集成-读取JDBC,发送AMQP消息和COMMIT - Spring integration - read JDBC, send AMQP message and COMMIT 发送消息到任意虚拟主机/与RabbitMQ / Spring AMQP交换 - Send message to arbitrary vhost / exchange with RabbitMQ / Spring AMQP Spring AMQP优先级消息 - Spring AMQP Priority Message Spring AMQP:具有机器名称的队列 - Spring AMQP: Queue with machine name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM