use of de.gessnerfl.rabbitmq.queue.management.model.Message in project rabbitmq-queue-management by gessnerfl.
the class QueueListOperation method getMessagesFromQueue.
public List<Message> getMessagesFromQueue(String brokerName, String queueName, int maxNumberOfMessages) {
try (CloseableChannelWrapper wrapper = connector.connectAsClosable(brokerName)) {
List<Message> messages = new ArrayList<>();
Channel channel = wrapper.getChannel();
channel.basicQos(DEFAULT_FETCH_COUNT);
int fetched = 0;
boolean messagesAvailable = true;
Long lastDeliveryTag = null;
while (fetched < maxNumberOfMessages && messagesAvailable) {
GetResponse response = channel.basicGet(queueName, false);
if (response != null) {
messages.add(createMessage(response));
lastDeliveryTag = response.getEnvelope().getDeliveryTag();
fetched++;
messagesAvailable = response.getMessageCount() > 0;
} else {
messagesAvailable = false;
}
}
if (lastDeliveryTag != null) {
channel.basicNack(lastDeliveryTag, true, true);
}
return messages;
} catch (IOException e) {
throw new MessageFetchFailedException(e);
}
}
Aggregations