Search in sources :

Example 11 with Message

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);
    }
}
Also used : CloseableChannelWrapper(de.gessnerfl.rabbitmq.queue.management.connection.CloseableChannelWrapper) Message(de.gessnerfl.rabbitmq.queue.management.model.Message) Channel(com.rabbitmq.client.Channel) IOException(java.io.IOException) GetResponse(com.rabbitmq.client.GetResponse)

Aggregations

Message (de.gessnerfl.rabbitmq.queue.management.model.Message)11 Test (org.junit.Test)10 GetResponse (com.rabbitmq.client.GetResponse)6 AMQP (com.rabbitmq.client.AMQP)5 Envelope (com.rabbitmq.client.Envelope)5 LongString (com.rabbitmq.client.LongString)5 BasicProperties (de.gessnerfl.rabbitmq.queue.management.model.BasicProperties)4 AbstractControllerIntegrationTest (de.gessnerfl.rabbitmq.queue.management.controller.AbstractControllerIntegrationTest)2 Channel (com.rabbitmq.client.Channel)1 CloseableChannelWrapper (de.gessnerfl.rabbitmq.queue.management.connection.CloseableChannelWrapper)1 IOException (java.io.IOException)1