use of com.rabbitmq.client.impl.StandardMetricsCollector in project rabbitmq-java-client by rabbitmq.
the class Metrics method metrics.
@Test
public void metrics() throws IOException, TimeoutException {
StandardMetricsCollector metrics = new StandardMetricsCollector();
connectionFactory.setMetricsCollector(metrics);
Connection connection1 = null;
Connection connection2 = null;
try {
connection1 = connectionFactory.newConnection();
assertThat(metrics.getConnections().getCount(), is(1L));
connection1.createChannel();
connection1.createChannel();
Channel channel = connection1.createChannel();
assertThat(metrics.getChannels().getCount(), is(3L));
sendMessage(channel);
assertThat(metrics.getPublishedMessages().getCount(), is(1L));
sendMessage(channel);
assertThat(metrics.getPublishedMessages().getCount(), is(2L));
channel.basicGet(QUEUE, true);
assertThat(metrics.getConsumedMessages().getCount(), is(1L));
channel.basicGet(QUEUE, true);
assertThat(metrics.getConsumedMessages().getCount(), is(2L));
channel.basicGet(QUEUE, true);
assertThat(metrics.getConsumedMessages().getCount(), is(2L));
connection2 = connectionFactory.newConnection();
assertThat(metrics.getConnections().getCount(), is(2L));
connection2.createChannel();
channel = connection2.createChannel();
assertThat(metrics.getChannels().getCount(), is(3L + 2L));
sendMessage(channel);
sendMessage(channel);
assertThat(metrics.getPublishedMessages().getCount(), is(2L + 2L));
channel.basicGet(QUEUE, true);
assertThat(metrics.getConsumedMessages().getCount(), is(2L + 1L));
channel.basicConsume(QUEUE, true, new DefaultConsumer(channel));
waitAtMost(timeout()).until(() -> metrics.getConsumedMessages().getCount(), equalTo(2L + 1L + 1L));
safeClose(connection1);
waitAtMost(timeout()).until(() -> metrics.getConnections().getCount(), equalTo(1L));
waitAtMost(timeout()).until(() -> metrics.getChannels().getCount(), equalTo(2L));
safeClose(connection2);
waitAtMost(timeout()).until(() -> metrics.getConnections().getCount(), equalTo(0L));
waitAtMost(timeout()).until(() -> metrics.getChannels().getCount(), equalTo(0L));
assertThat(metrics.getAcknowledgedMessages().getCount(), is(0L));
assertThat(metrics.getRejectedMessages().getCount(), is(0L));
} finally {
safeClose(connection1);
safeClose(connection2);
}
}
use of com.rabbitmq.client.impl.StandardMetricsCollector in project rabbitmq-java-client by rabbitmq.
the class Metrics method checkAcksWithAutomaticRecovery.
@Test
public void checkAcksWithAutomaticRecovery() throws Exception {
ConnectionFactory connectionFactory = createConnectionFactory();
connectionFactory.setNetworkRecoveryInterval(2000);
connectionFactory.setAutomaticRecoveryEnabled(true);
StandardMetricsCollector metrics = new StandardMetricsCollector();
connectionFactory.setMetricsCollector(metrics);
Connection connection = null;
try {
connection = connectionFactory.newConnection();
Channel channel1 = connection.createChannel();
AtomicInteger ackedMessages = new AtomicInteger(0);
channel1.basicConsume(QUEUE, false, (consumerTag, message) -> {
try {
channel1.basicAck(message.getEnvelope().getDeliveryTag(), false);
ackedMessages.incrementAndGet();
} catch (Exception e) {
}
}, tag -> {
});
Channel channel2 = connection.createChannel();
channel2.confirmSelect();
int nbMessages = 10;
for (int i = 0; i < nbMessages; i++) {
sendMessage(channel2);
}
channel2.waitForConfirms(1000);
closeAndWaitForRecovery((AutorecoveringConnection) connection);
for (int i = 0; i < nbMessages; i++) {
sendMessage(channel2);
}
waitAtMost(timeout()).until(() -> ackedMessages.get(), equalTo(nbMessages * 2));
assertThat(metrics.getConsumedMessages().getCount(), is((long) (nbMessages * 2)));
assertThat(metrics.getAcknowledgedMessages().getCount(), is((long) (nbMessages * 2)));
} finally {
safeClose(connection);
}
}
use of com.rabbitmq.client.impl.StandardMetricsCollector in project rabbitmq-java-client by rabbitmq.
the class Metrics method metricsReject.
@Test
public void metricsReject() throws IOException, TimeoutException {
StandardMetricsCollector metrics = new StandardMetricsCollector();
connectionFactory.setMetricsCollector(metrics);
Connection connection = null;
try {
connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
sendMessage(channel);
sendMessage(channel);
sendMessage(channel);
GetResponse response1 = channel.basicGet(QUEUE, false);
GetResponse response2 = channel.basicGet(QUEUE, false);
GetResponse response3 = channel.basicGet(QUEUE, false);
channel.basicReject(response2.getEnvelope().getDeliveryTag(), false);
assertThat(metrics.getRejectedMessages().getCount(), is(1L));
channel.basicNack(response3.getEnvelope().getDeliveryTag(), true, false);
assertThat(metrics.getRejectedMessages().getCount(), is(1L + 2L));
} finally {
safeClose(connection);
}
}
use of com.rabbitmq.client.impl.StandardMetricsCollector in project rabbitmq-java-client by rabbitmq.
the class Metrics method errorInChannel.
@Test
public void errorInChannel() throws IOException, TimeoutException {
StandardMetricsCollector metrics = new StandardMetricsCollector();
connectionFactory.setMetricsCollector(metrics);
Connection connection = null;
try {
connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
assertThat(metrics.getConnections().getCount(), is(1L));
assertThat(metrics.getChannels().getCount(), is(1L));
channel.basicPublish("unlikelynameforanexchange", "", null, "msg".getBytes("UTF-8"));
waitAtMost(timeout()).until(() -> metrics.getChannels().getCount(), is(0L));
assertThat(metrics.getConnections().getCount(), is(1L));
} finally {
safeClose(connection);
}
}
use of com.rabbitmq.client.impl.StandardMetricsCollector in project rabbitmq-java-client by rabbitmq.
the class Metrics method checkListenersWithAutoRecoveryConnection.
@Test
public void checkListenersWithAutoRecoveryConnection() throws Exception {
ConnectionFactory connectionFactory = createConnectionFactory();
connectionFactory.setNetworkRecoveryInterval(2000);
connectionFactory.setAutomaticRecoveryEnabled(true);
StandardMetricsCollector metrics = new StandardMetricsCollector();
connectionFactory.setMetricsCollector(metrics);
Connection connection = null;
try {
connection = connectionFactory.newConnection();
Collection<?> shutdownHooks = getShutdownHooks(connection);
assertThat(shutdownHooks.size(), is(0));
connection.createChannel();
assertThat(metrics.getConnections().getCount(), is(1L));
assertThat(metrics.getChannels().getCount(), is(1L));
closeAndWaitForRecovery((AutorecoveringConnection) connection);
assertThat(metrics.getConnections().getCount(), is(1L));
assertThat(metrics.getChannels().getCount(), is(1L));
assertThat(shutdownHooks.size(), is(0));
} finally {
safeClose(connection);
}
}
Aggregations