Search in sources :

Example 1 with MetricsCollector

use of com.rabbitmq.client.MetricsCollector in project rabbitmq-java-client by rabbitmq.

the class ConnectionFactoryTest method tryNextAddressIfTimeoutExceptionNoAutoRecovery.

// see https://github.com/rabbitmq/rabbitmq-java-client/issues/262
@Test
public void tryNextAddressIfTimeoutExceptionNoAutoRecovery() throws IOException, TimeoutException {
    final AMQConnection connectionThatThrowsTimeout = mock(AMQConnection.class);
    final AMQConnection connectionThatSucceeds = mock(AMQConnection.class);
    final Queue<AMQConnection> connections = new ArrayBlockingQueue<AMQConnection>(10);
    connections.add(connectionThatThrowsTimeout);
    connections.add(connectionThatSucceeds);
    ConnectionFactory connectionFactory = new ConnectionFactory() {

        @Override
        protected AMQConnection createConnection(ConnectionParams params, FrameHandler frameHandler, MetricsCollector metricsCollector) {
            return connections.poll();
        }

        @Override
        protected synchronized FrameHandlerFactory createFrameHandlerFactory() throws IOException {
            return mock(FrameHandlerFactory.class);
        }
    };
    connectionFactory.setAutomaticRecoveryEnabled(false);
    doThrow(TimeoutException.class).when(connectionThatThrowsTimeout).start();
    doNothing().when(connectionThatSucceeds).start();
    Connection returnedConnection = connectionFactory.newConnection(new Address[] { new Address("host1"), new Address("host2") });
    assertSame(connectionThatSucceeds, returnedConnection);
}
Also used : MetricsCollector(com.rabbitmq.client.MetricsCollector) FrameHandler(com.rabbitmq.client.impl.FrameHandler) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Address(com.rabbitmq.client.Address) AMQConnection(com.rabbitmq.client.impl.AMQConnection) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Connection(com.rabbitmq.client.Connection) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) Test(org.junit.Test)

Example 2 with MetricsCollector

use of com.rabbitmq.client.MetricsCollector in project rabbitmq-java-client by rabbitmq.

the class ConnectionFactoryTest method customizeCredentialsProvider.

// see https://github.com/rabbitmq/rabbitmq-java-client/pull/350
@Test
public void customizeCredentialsProvider() throws Exception {
    final CredentialsProvider provider = mock(CredentialsProvider.class);
    final AMQConnection connection = mock(AMQConnection.class);
    final AtomicBoolean createCalled = new AtomicBoolean(false);
    ConnectionFactory connectionFactory = new ConnectionFactory() {

        @Override
        protected AMQConnection createConnection(ConnectionParams params, FrameHandler frameHandler, MetricsCollector metricsCollector) {
            assertSame(provider, params.getCredentialsProvider());
            createCalled.set(true);
            return connection;
        }
    };
    connectionFactory.setCredentialsProvider(provider);
    connectionFactory.setAutomaticRecoveryEnabled(false);
    doNothing().when(connection).start();
    Connection returnedConnection = connectionFactory.newConnection();
    assertSame(returnedConnection, connection);
    assertTrue(createCalled.get());
}
Also used : MetricsCollector(com.rabbitmq.client.MetricsCollector) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FrameHandler(com.rabbitmq.client.impl.FrameHandler) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) AMQConnection(com.rabbitmq.client.impl.AMQConnection) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Connection(com.rabbitmq.client.Connection) CredentialsProvider(com.rabbitmq.client.impl.CredentialsProvider) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) Test(org.junit.Test)

Example 3 with MetricsCollector

use of com.rabbitmq.client.MetricsCollector in project rabbitmq-java-client by rabbitmq.

the class RecoveryAwareAMQConnectionFactoryTest method tryNextAddressIfTimeoutException.

// see https://github.com/rabbitmq/rabbitmq-java-client/issues/262
@Test
public void tryNextAddressIfTimeoutException() throws IOException, TimeoutException {
    final RecoveryAwareAMQConnection connectionThatThrowsTimeout = mock(RecoveryAwareAMQConnection.class);
    final RecoveryAwareAMQConnection connectionThatSucceeds = mock(RecoveryAwareAMQConnection.class);
    final Queue<RecoveryAwareAMQConnection> connections = new ArrayBlockingQueue<RecoveryAwareAMQConnection>(10);
    connections.add(connectionThatThrowsTimeout);
    connections.add(connectionThatSucceeds);
    AddressResolver addressResolver = () -> Arrays.asList(new Address("host1"), new Address("host2"));
    RecoveryAwareAMQConnectionFactory connectionFactory = new RecoveryAwareAMQConnectionFactory(new ConnectionParams(), mock(FrameHandlerFactory.class), addressResolver) {

        @Override
        protected RecoveryAwareAMQConnection createConnection(ConnectionParams params, FrameHandler handler, MetricsCollector metricsCollector) {
            return connections.poll();
        }
    };
    doThrow(TimeoutException.class).when(connectionThatThrowsTimeout).start();
    doNothing().when(connectionThatSucceeds).start();
    Connection returnedConnection = connectionFactory.newConnection();
    assertSame(connectionThatSucceeds, returnedConnection);
}
Also used : RecoveryAwareAMQConnectionFactory(com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory) FrameHandlerFactory(com.rabbitmq.client.impl.FrameHandlerFactory) MetricsCollector(com.rabbitmq.client.MetricsCollector) AddressResolver(com.rabbitmq.client.AddressResolver) FrameHandler(com.rabbitmq.client.impl.FrameHandler) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Address(com.rabbitmq.client.Address) RecoveryAwareAMQConnection(com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnection) RecoveryAwareAMQConnection(com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnection) Connection(com.rabbitmq.client.Connection) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) Test(org.junit.Test)

Aggregations

Connection (com.rabbitmq.client.Connection)3 MetricsCollector (com.rabbitmq.client.MetricsCollector)3 ConnectionParams (com.rabbitmq.client.impl.ConnectionParams)3 FrameHandler (com.rabbitmq.client.impl.FrameHandler)3 Test (org.junit.Test)3 Address (com.rabbitmq.client.Address)2 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)2 AMQConnection (com.rabbitmq.client.impl.AMQConnection)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 AddressResolver (com.rabbitmq.client.AddressResolver)1 CredentialsProvider (com.rabbitmq.client.impl.CredentialsProvider)1 FrameHandlerFactory (com.rabbitmq.client.impl.FrameHandlerFactory)1 RecoveryAwareAMQConnection (com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnection)1 RecoveryAwareAMQConnectionFactory (com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1