use of com.rabbitmq.client.GetResponse in project rabbitmq-queue-management by gessnerfl.
the class MessageMapperTest method mockResponse.
private GetResponse mockResponse(Envelope envelope, AMQP.BasicProperties basicProperties) {
GetResponse response = mock(GetResponse.class);
when(response.getEnvelope()).thenReturn(envelope);
when(response.getProps()).thenReturn(basicProperties);
when(response.getBody()).thenReturn(DEFAULT_PAYLOAD);
return response;
}
use of com.rabbitmq.client.GetResponse in project rabbitmq-queue-management by gessnerfl.
the class MessageMapperTest method shouldMapBasicMessageFields.
@Test
public void shouldMapBasicMessageFields() {
String checksum = "checksum";
Envelope envelope = mock(Envelope.class);
AMQP.BasicProperties basicProperties = mock(AMQP.BasicProperties.class);
GetResponse getResponse = mockResponse(envelope, basicProperties);
when(messageChecksum.createFor(basicProperties, DEFAULT_PAYLOAD)).thenReturn(checksum);
Message result = sut.map(getResponse);
assertEquals(DEFAULT_PAYLOAD, result.getBody());
assertEquals(checksum, result.getChecksum());
assertSame(envelope, result.getEnvelope());
assertNotNull(result.getProperties());
}
use of com.rabbitmq.client.GetResponse in project rabbitmq-queue-management by gessnerfl.
the class MessageMapperTest method shouldMapRabbitMqSpecificTypesInAmqpBasicPropertiesHeadersDeepInLists.
@Test
public void shouldMapRabbitMqSpecificTypesInAmqpBasicPropertiesHeadersDeepInLists() {
String headerKey = "headerKey";
String listValue = "listValue";
List<String> list = Arrays.asList(listValue);
Map<String, Object> headers = new HashMap<>();
headers.put(headerKey, list);
AMQP.BasicProperties basicProperties = mock(AMQP.BasicProperties.class);
when(basicProperties.getHeaders()).thenReturn(headers);
Envelope envelope = mock(Envelope.class);
GetResponse getResponse = mockResponse(envelope, basicProperties);
Message result = sut.map(getResponse);
BasicProperties basicPropertiesOfResult = result.getProperties();
assertNotNull(basicPropertiesOfResult);
assertThat(basicPropertiesOfResult.getHeaders().keySet(), Matchers.hasSize(1));
assertThat(basicPropertiesOfResult.getHeaders(), Matchers.hasKey(headerKey));
assertThat(basicPropertiesOfResult.getHeaders().get(headerKey), Matchers.instanceOf(List.class));
assertThat(((List<String>) basicPropertiesOfResult.getHeaders().get(headerKey)), Matchers.hasSize(1));
assertThat(((List<String>) basicPropertiesOfResult.getHeaders().get(headerKey)), Matchers.contains(listValue));
}
use of com.rabbitmq.client.GetResponse 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);
}
}
use of com.rabbitmq.client.GetResponse 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);
}
}
Aggregations