Search in sources :

Example 6 with StandardMetricsCollector

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);
    }
}
Also used : StandardMetricsCollector(com.rabbitmq.client.impl.StandardMetricsCollector) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) AutorecoveringConnection(com.rabbitmq.client.impl.recovery.AutorecoveringConnection) GetResponse(com.rabbitmq.client.GetResponse) Test(org.junit.Test)

Example 7 with StandardMetricsCollector

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();
    }
}
Also used : StandardMetricsCollector(com.rabbitmq.client.impl.StandardMetricsCollector) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) AutorecoveringConnection(com.rabbitmq.client.impl.recovery.AutorecoveringConnection) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Aggregations

Connection (com.rabbitmq.client.Connection)7 StandardMetricsCollector (com.rabbitmq.client.impl.StandardMetricsCollector)7 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)7 Test (org.junit.Test)7 Channel (com.rabbitmq.client.Channel)6 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)2 GetResponse (com.rabbitmq.client.GetResponse)2 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1