RabbitMQ消息确认模式:
- AcknowledgeMode.NONE:不确认
- AcknowledgeMode.AUTO:自动确认
- AcknowledgeMode.MANUAL:手动确认
当设置为手动确认时AcknowledgeMode.MANUAL
RabbitMQ消息监听器接收到消息之后,需进行确认
@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如下配置,可以设置一个队列多个消费者监听消息:
@Bean public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() { SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); factory.setConnectionFactory(connectionFactory()); factory.setConcurrentConsumers(3);//对于当前消息队列,设置三个消费者并发监听消费消息 factory.setMaxConcurrentConsumers(10); // factory.setMessageConverter(producerJackson2MessageConverter()); factory.setAcknowledgeMode(AcknowledgeMode.NONE);//设置确认模式自动确认 return factory; }
参考资料:
RabbitMQ的消息确认机制
rabbitmq 配置多个消费者(转载)