use of com.rabbitmq.client.impl.StandardMetricsCollector in project rabbitmq-java-client by rabbitmq.
the class Metrics method metricsAck.
@Test
public void metricsAck() throws IOException, TimeoutException {
StandardMetricsCollector metrics = new StandardMetricsCollector();
connectionFactory.setMetricsCollector(metrics);
Connection connection = null;
try {
connection = connectionFactory.newConnection();
Channel channel1 = connection.createChannel();
Channel channel2 = connection.createChannel();
sendMessage(channel1);
GetResponse getResponse = channel1.basicGet(QUEUE, false);
channel1.basicAck(getResponse.getEnvelope().getDeliveryTag(), false);
assertThat(metrics.getConsumedMessages().getCount(), is(1L));
assertThat(metrics.getAcknowledgedMessages().getCount(), is(1L));
// basicGet / basicAck
sendMessage(channel1);
sendMessage(channel2);
sendMessage(channel1);
sendMessage(channel2);
sendMessage(channel1);
sendMessage(channel2);
GetResponse response1 = channel1.basicGet(QUEUE, false);
GetResponse response2 = channel2.basicGet(QUEUE, false);
GetResponse response3 = channel1.basicGet(QUEUE, false);
GetResponse response4 = channel2.basicGet(QUEUE, false);
GetResponse response5 = channel1.basicGet(QUEUE, false);
GetResponse response6 = channel2.basicGet(QUEUE, false);
assertThat(metrics.getConsumedMessages().getCount(), is(1L + 6L));
assertThat(metrics.getAcknowledgedMessages().getCount(), is(1L));
channel1.basicAck(response5.getEnvelope().getDeliveryTag(), false);
assertThat(metrics.getAcknowledgedMessages().getCount(), is(1L + 1L));
channel1.basicAck(response3.getEnvelope().getDeliveryTag(), true);
assertThat(metrics.getAcknowledgedMessages().getCount(), is(1L + 1L + 2L));
channel2.basicAck(response2.getEnvelope().getDeliveryTag(), true);
assertThat(metrics.getAcknowledgedMessages().getCount(), is(1L + (1L + 2L) + 1L));
channel2.basicAck(response6.getEnvelope().getDeliveryTag(), true);
assertThat(metrics.getAcknowledgedMessages().getCount(), is(1L + (1L + 2L) + 1L + 2L));
long alreadySentMessages = 1 + (1 + 2) + 1 + 2;
// basicConsume / basicAck
channel1.basicConsume(QUEUE, false, new MultipleAckConsumer(channel1, false));
channel1.basicConsume(QUEUE, false, new MultipleAckConsumer(channel1, true));
channel2.basicConsume(QUEUE, false, new MultipleAckConsumer(channel2, false));
channel2.basicConsume(QUEUE, false, new MultipleAckConsumer(channel2, true));
int nbMessages = 10;
for (int i = 0; i < nbMessages; i++) {
sendMessage(i % 2 == 0 ? channel1 : channel2);
}
waitAtMost(timeout()).until(() -> metrics.getConsumedMessages().getCount(), equalTo(alreadySentMessages + nbMessages));
waitAtMost(timeout()).until(() -> metrics.getAcknowledgedMessages().getCount(), equalTo(alreadySentMessages + nbMessages));
} finally {
safeClose(connection);
}
}
use of com.rabbitmq.client.impl.StandardMetricsCollector in project rabbitmq-java-client by rabbitmq.
the class Metrics method multiThreadedMetricsStandardConnection.
@Test
public void multiThreadedMetricsStandardConnection() throws InterruptedException, TimeoutException, IOException {
StandardMetricsCollector metrics = new StandardMetricsCollector();
connectionFactory.setMetricsCollector(metrics);
int nbConnections = 3;
int nbChannelsPerConnection = 5;
int nbChannels = nbConnections * nbChannelsPerConnection;
long nbOfMessages = 100;
// channel are not thread-safe
int nbTasks = nbChannels;
Random random = new Random();
// create connections
Connection[] connections = new Connection[nbConnections];
ExecutorService executorService = Executors.newFixedThreadPool(nbTasks);
try {
Channel[] channels = new Channel[nbChannels];
for (int i = 0; i < nbConnections; i++) {
connections[i] = connectionFactory.newConnection();
for (int j = 0; j < nbChannelsPerConnection; j++) {
Channel channel = connections[i].createChannel();
channel.basicQos(1);
channels[i * nbChannelsPerConnection + j] = channel;
}
}
// consume messages without ack
for (int i = 0; i < nbOfMessages; i++) {
sendMessage(channels[random.nextInt(nbChannels)]);
}
List<Callable<Void>> tasks = new ArrayList<Callable<Void>>();
for (int i = 0; i < nbTasks; i++) {
Channel channelForConsuming = channels[random.nextInt(nbChannels)];
tasks.add(random.nextInt(10) % 2 == 0 ? new BasicGetTask(channelForConsuming, true) : new BasicConsumeTask(channelForConsuming, true));
}
executorService.invokeAll(tasks);
assertThat(metrics.getPublishedMessages().getCount(), is(nbOfMessages));
waitAtMost(timeout()).until(() -> metrics.getConsumedMessages().getCount(), equalTo(nbOfMessages));
assertThat(metrics.getAcknowledgedMessages().getCount(), is(0L));
// to remove the listeners
for (int i = 0; i < nbChannels; i++) {
channels[i].close();
Channel channel = connections[random.nextInt(nbConnections)].createChannel();
channel.basicQos(1);
channels[i] = channel;
}
// consume messages with ack
for (int i = 0; i < nbOfMessages; i++) {
sendMessage(channels[random.nextInt(nbChannels)]);
}
executorService.shutdownNow();
executorService = Executors.newFixedThreadPool(nbTasks);
tasks = new ArrayList<Callable<Void>>();
for (int i = 0; i < nbTasks; i++) {
Channel channelForConsuming = channels[i];
tasks.add(random.nextBoolean() ? new BasicGetTask(channelForConsuming, false) : new BasicConsumeTask(channelForConsuming, false));
}
executorService.invokeAll(tasks);
assertThat(metrics.getPublishedMessages().getCount(), is(2 * nbOfMessages));
waitAtMost(timeout()).until(() -> metrics.getConsumedMessages().getCount(), equalTo(2 * nbOfMessages));
waitAtMost(timeout()).until(() -> metrics.getAcknowledgedMessages().getCount(), equalTo(nbOfMessages));
// to remove the listeners
for (int i = 0; i < nbChannels; i++) {
channels[i].close();
Channel channel = connections[random.nextInt(nbConnections)].createChannel();
channel.basicQos(1);
channels[i] = channel;
}
// consume messages and reject them
for (int i = 0; i < nbOfMessages; i++) {
sendMessage(channels[random.nextInt(nbChannels)]);
}
executorService.shutdownNow();
executorService = Executors.newFixedThreadPool(nbTasks);
tasks = new ArrayList<Callable<Void>>();
for (int i = 0; i < nbTasks; i++) {
Channel channelForConsuming = channels[i];
tasks.add(random.nextBoolean() ? new BasicGetRejectTask(channelForConsuming) : new BasicConsumeRejectTask(channelForConsuming));
}
executorService.invokeAll(tasks);
assertThat(metrics.getPublishedMessages().getCount(), is(3 * nbOfMessages));
waitAtMost(timeout()).until(() -> metrics.getConsumedMessages().getCount(), equalTo(3 * nbOfMessages));
waitAtMost(timeout()).until(() -> metrics.getAcknowledgedMessages().getCount(), equalTo(nbOfMessages));
waitAtMost(timeout()).until(() -> metrics.getRejectedMessages().getCount(), equalTo(nbOfMessages));
} finally {
for (Connection connection : connections) {
safeClose(connection);
}
executorService.shutdownNow();
}
}
Aggregations