Receive single message from the queue in RabbitMQ using java:
I'm new to RabbitMQ and was wondering of a good approach to this problem I'm mulling over. I want to create a service that subscribes to a queue and Receive only one message and receive the next one after processing.
DeliverCallback deliverCallback = new DeliverCallback() {
public void handle(String s, Delivery delivery) throws IOException {
System.out.println("Tag: "+delivery.getEnvelope().getDeliveryTag());
String message = new String(delivery.getBody(), "UTF-8");
}
};
channel.basicConsume(QUEUE_NAME, false, deliverCallback, new CancelCallback() {
public void handle(String consumerTag) throws IOException {}
});
RabbitMQ supports polling a single message from a Queue at a time, but the official document doesnt encourage this approach.
Poll the message by disabling auto acknowledgement, finally send an ack after processing.
For more details please refer to the official document section 'Retrieving Individual Messages ("Pull API")'
https://www.rabbitmq.com/api-guide.html#getting