Search in sources :

Example 1 with StandardMetricsCollector

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

Example 2 with StandardMetricsCollector

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);
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) StandardMetricsCollector(com.rabbitmq.client.impl.StandardMetricsCollector) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) AutorecoveringConnection(com.rabbitmq.client.impl.recovery.AutorecoveringConnection) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with StandardMetricsCollector

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);
    }
}
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 4 with StandardMetricsCollector

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);
    }
}
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) Test(org.junit.Test)

Example 5 with StandardMetricsCollector

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