Search in sources :

Example 1 with AutorecoveringConnection

use of com.rabbitmq.client.impl.recovery.AutorecoveringConnection in project rabbitmq-java-client by rabbitmq.

the class ChannelLimitNegotiation method openingTooManyChannels.

@Test
public void openingTooManyChannels() throws Exception {
    int n = 48;
    try {
        Host.rabbitmqctl("eval 'application:set_env(rabbit, channel_max, " + n + ").'");
        ConnectionFactory cf = TestUtils.connectionFactory();
        Connection conn = cf.newConnection();
        assertEquals(n, conn.getChannelMax());
        for (int i = 1; i <= n; i++) {
            assertNotNull(conn.createChannel(i));
        }
        // ChannelManager guards against channel.open being sent
        assertNull(conn.createChannel(n + 1));
        // Construct a channel directly
        final ChannelN ch = new ChannelN(((AutorecoveringConnection) conn).getDelegate(), n + 1, new ConsumerWorkService(Executors.newSingleThreadExecutor(), Executors.defaultThreadFactory(), ConnectionFactory.DEFAULT_SHUTDOWN_TIMEOUT));
        conn.addShutdownListener(new ShutdownListener() {

            public void shutdownCompleted(ShutdownSignalException cause) {
                // make sure channel.open continuation is released
                ch.processShutdownSignal(cause, true, true);
            }
        });
        ch.open();
        fail("expected channel.open to cause a connection exception");
    } catch (IOException e) {
        checkShutdownSignal(530, e);
    } finally {
        Host.rabbitmqctl("eval 'application:set_env(rabbit, channel_max, 0).'");
    }
}
Also used : ShutdownListener(com.rabbitmq.client.ShutdownListener) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) ChannelN(com.rabbitmq.client.impl.ChannelN) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Connection(com.rabbitmq.client.Connection) AutorecoveringConnection(com.rabbitmq.client.impl.recovery.AutorecoveringConnection) ConsumerWorkService(com.rabbitmq.client.impl.ConsumerWorkService) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with AutorecoveringConnection

use of com.rabbitmq.client.impl.recovery.AutorecoveringConnection 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 AutorecoveringConnection

use of com.rabbitmq.client.impl.recovery.AutorecoveringConnection 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)

Example 4 with AutorecoveringConnection

use of com.rabbitmq.client.impl.recovery.AutorecoveringConnection in project rabbitmq-java-client by rabbitmq.

the class ConnectionFactory method newConnection.

/**
 * Create a new broker connection with a client-provided name, picking the first available address from
 * the list provided by the {@link AddressResolver}.
 *
 * If <a href="http://www.rabbitmq.com/api-guide.html#recovery">automatic connection recovery</a>
 * is enabled, the connection returned by this method will be {@link Recoverable}. Future
 * reconnection attempts will pick a random accessible address provided by the {@link AddressResolver}.
 *
 * @param executor thread execution service for consumers on the connection
 * @param addressResolver discovery service to list potential addresses (hostname/port pairs) to connect to
 * @param clientProvidedName application-specific connection name, will be displayed
 *                           in the management UI if RabbitMQ server supports it.
 *                           This value doesn't have to be unique and cannot be used
 *                           as a connection identifier e.g. in HTTP API requests.
 *                           This value is supposed to be human-readable.
 * @return an interface to the connection
 * @throws java.io.IOException if it encounters a problem
 * @see <a href="http://www.rabbitmq.com/api-guide.html#recovery">Automatic Recovery</a>
 */
public Connection newConnection(ExecutorService executor, AddressResolver addressResolver, String clientProvidedName) throws IOException, TimeoutException {
    if (this.metricsCollector == null) {
        this.metricsCollector = new NoOpMetricsCollector();
    }
    // make sure we respect the provided thread factory
    FrameHandlerFactory fhFactory = createFrameHandlerFactory();
    ConnectionParams params = params(executor);
    // set client-provided via a client property
    if (clientProvidedName != null) {
        Map<String, Object> properties = new HashMap<String, Object>(params.getClientProperties());
        properties.put("connection_name", clientProvidedName);
        params.setClientProperties(properties);
    }
    if (isAutomaticRecoveryEnabled()) {
        // see com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory#newConnection
        AutorecoveringConnection conn = new AutorecoveringConnection(params, fhFactory, addressResolver, metricsCollector);
        conn.init();
        return conn;
    } else {
        List<Address> addrs = addressResolver.getAddresses();
        Exception lastException = null;
        for (Address addr : addrs) {
            try {
                FrameHandler handler = fhFactory.create(addr, clientProvidedName);
                AMQConnection conn = createConnection(params, handler, metricsCollector);
                conn.start();
                this.metricsCollector.newConnection(conn);
                return conn;
            } catch (IOException e) {
                lastException = e;
            } catch (TimeoutException te) {
                lastException = te;
            }
        }
        if (lastException != null) {
            if (lastException instanceof IOException) {
                throw (IOException) lastException;
            } else if (lastException instanceof TimeoutException) {
                throw (TimeoutException) lastException;
            }
        }
        throw new IOException("failed to connect");
    }
}
Also used : SocketChannelFrameHandlerFactory(com.rabbitmq.client.impl.nio.SocketChannelFrameHandlerFactory) FrameHandlerFactory(com.rabbitmq.client.impl.FrameHandlerFactory) SocketFrameHandlerFactory(com.rabbitmq.client.impl.SocketFrameHandlerFactory) FrameHandler(com.rabbitmq.client.impl.FrameHandler) HashMap(java.util.HashMap) AMQConnection(com.rabbitmq.client.impl.AMQConnection) IOException(java.io.IOException) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AutorecoveringConnection(com.rabbitmq.client.impl.recovery.AutorecoveringConnection) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)4 Connection (com.rabbitmq.client.Connection)3 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 AMQConnection (com.rabbitmq.client.impl.AMQConnection)2 StandardMetricsCollector (com.rabbitmq.client.impl.StandardMetricsCollector)2 TimeoutException (java.util.concurrent.TimeoutException)2 Channel (com.rabbitmq.client.Channel)1 ShutdownListener (com.rabbitmq.client.ShutdownListener)1 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)1 ChannelN (com.rabbitmq.client.impl.ChannelN)1 ConnectionParams (com.rabbitmq.client.impl.ConnectionParams)1 ConsumerWorkService (com.rabbitmq.client.impl.ConsumerWorkService)1 FrameHandler (com.rabbitmq.client.impl.FrameHandler)1 FrameHandlerFactory (com.rabbitmq.client.impl.FrameHandlerFactory)1 SocketFrameHandlerFactory (com.rabbitmq.client.impl.SocketFrameHandlerFactory)1 SocketChannelFrameHandlerFactory (com.rabbitmq.client.impl.nio.SocketChannelFrameHandlerFactory)1 URISyntaxException (java.net.URISyntaxException)1 KeyManagementException (java.security.KeyManagementException)1