RabbitMQ message reception confirmation RabbitMQ message confirmation mechanism rabbitmq configure multiple consumers (reproduced)

RabbitMQ message confirmation mode:

  • AcknowledgeMode.NONE: do not confirm
  • AcknowledgeMode.AUTO: automatic confirmation
  • AcknowledgeMode.MANUAL: manual confirmation

When set to manual confirmation AcknowledgeMode.MANUAL

RabbitMQ message listener needs to confirm after receiving the message

    @RabbitListener(queues ="${ocos.rabbit.queue}",containerFactory = "rabbitListenerContainerFactory")
    public void onMessage(Message message, Channel channel) {
               try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            try {
                e.printStackTrace();
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

Rabbitmq is configured as follows, you can set up a queue for multiple consumers to listen for messages:

@Bean
     public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory () { 
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory (); 
        factory.setConnectionFactory (connectionFactory ()); 
        factory.setConcurrentConsumers ( 3 ); // For the current message queue, set up three consumers to concurrently listen and consume the message 
        factory. setMaxConcurrentConsumers ( 10 );
        // factory.setMessageConverter (producerJackson2MessageConverter ()); 
        factory.setAcknowledgeMode (AcknowledgeMode.NONE); // Set confirmation mode to automatically confirm 
        return factory; 
    }

Reference materials:

RabbitMQ message confirmation mechanism

rabbitmq configure multiple consumers (reproduced)

 

Guess you like

Origin www.cnblogs.com/dancser/p/12700067.html
Recommended