Search in sources :

Example 1 with CloseableChannelWrapper

use of de.gessnerfl.rabbitmq.queue.management.connection.CloseableChannelWrapper in project rabbitmq-queue-management by gessnerfl.

the class RabbitMqTestEnvironment method setup.

public void setup() {
    try (CloseableChannelWrapper wrapper = connector.connectAsClosable(BROKER)) {
        Channel channel = wrapper.getChannel();
        declareExchanges(channel);
        declareQueues(channel);
    } catch (RabbitMqEnvironmentException e) {
        cleanup();
        throw e;
    }
}
Also used : CloseableChannelWrapper(de.gessnerfl.rabbitmq.queue.management.connection.CloseableChannelWrapper) Channel(com.rabbitmq.client.Channel)

Example 2 with CloseableChannelWrapper

use of de.gessnerfl.rabbitmq.queue.management.connection.CloseableChannelWrapper in project rabbitmq-queue-management by gessnerfl.

the class RabbitMqTestEnvironment method cleanup.

public void cleanup() {
    try (CloseableChannelWrapper wrapper = connector.connectAsClosable(BROKER)) {
        Channel channel = wrapper.getChannel();
        deleteQueues(channel);
        deleteExchanges(channel);
    }
}
Also used : CloseableChannelWrapper(de.gessnerfl.rabbitmq.queue.management.connection.CloseableChannelWrapper) Channel(com.rabbitmq.client.Channel)

Example 3 with CloseableChannelWrapper

use of de.gessnerfl.rabbitmq.queue.management.connection.CloseableChannelWrapper in project rabbitmq-queue-management by gessnerfl.

the class RabbitMqTestEnvironment method publishMessage.

public void publishMessage(String exchange, String routingKey) {
    try (CloseableChannelWrapper wrapper = connector.connectAsClosable(BROKER)) {
        Channel channel = wrapper.getChannel();
        publishMessage(channel, exchange, routingKey, 1);
    }
}
Also used : CloseableChannelWrapper(de.gessnerfl.rabbitmq.queue.management.connection.CloseableChannelWrapper) Channel(com.rabbitmq.client.Channel)

Example 4 with CloseableChannelWrapper

use of de.gessnerfl.rabbitmq.queue.management.connection.CloseableChannelWrapper 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)

Example 5 with CloseableChannelWrapper

use of de.gessnerfl.rabbitmq.queue.management.connection.CloseableChannelWrapper in project rabbitmq-queue-management by gessnerfl.

the class MessageOperationExecutor method consumeMessageApplyFunctionAndAckknowlegeOnSuccess.

public void consumeMessageApplyFunctionAndAckknowlegeOnSuccess(String brokerName, String queueName, String expectedChecksum, MessageOperationFunction fn) {
    try (CloseableChannelWrapper wrapper = connector.connectAsClosable(brokerName)) {
        Channel channel = wrapper.getChannel();
        GetResponse response = getFirstMessage(queueName, channel);
        String checksum = messageChecksum.createFor(response.getProps(), response.getBody());
        if (checksum.equals(expectedChecksum)) {
            fn.apply(channel, response);
            channel.basicAck(response.getEnvelope().getDeliveryTag(), false);
        } else {
            channel.basicNack(response.getEnvelope().getDeliveryTag(), false, true);
            throw new MessageOperationFailedException("Checksum does not match");
        }
    } catch (Exception e) {
        if (e instanceof MessageOperationFailedException) {
            throw (MessageOperationFailedException) e;
        }
        throw new MessageOperationFailedException(e);
    }
}
Also used : CloseableChannelWrapper(de.gessnerfl.rabbitmq.queue.management.connection.CloseableChannelWrapper) Channel(com.rabbitmq.client.Channel) GetResponse(com.rabbitmq.client.GetResponse) IOException(java.io.IOException)

Aggregations

Channel (com.rabbitmq.client.Channel)5 CloseableChannelWrapper (de.gessnerfl.rabbitmq.queue.management.connection.CloseableChannelWrapper)5 GetResponse (com.rabbitmq.client.GetResponse)2 IOException (java.io.IOException)2 Message (de.gessnerfl.rabbitmq.queue.management.model.Message)1