Search in sources :

Example 1 with CredentialsProvider

use of com.rabbitmq.client.impl.CredentialsProvider 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 2 with CredentialsProvider

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

the class ConnectionRecovery method connectionRecoveryRequestsCredentialsAgain.

// See https://github.com/rabbitmq/rabbitmq-java-client/pull/350 .
// We want to request fresh creds when recovering.
@Test
public void connectionRecoveryRequestsCredentialsAgain() throws Exception {
    ConnectionFactory cf = buildConnectionFactoryWithRecoveryEnabled(false);
    final String username = cf.getUsername();
    final String password = cf.getPassword();
    final AtomicInteger usernameRequested = new AtomicInteger(0);
    final AtomicInteger passwordRequested = new AtomicInteger(0);
    cf.setCredentialsProvider(new CredentialsProvider() {

        @Override
        public String getUsername() {
            usernameRequested.incrementAndGet();
            return username;
        }

        @Override
        public String getPassword() {
            passwordRequested.incrementAndGet();
            return password;
        }
    });
    RecoverableConnection c = (RecoverableConnection) cf.newConnection();
    try {
        assertTrue(c.isOpen());
        assertThat(usernameRequested.get(), is(1));
        assertThat(passwordRequested.get(), is(1));
        closeAndWaitForRecovery(c);
        assertTrue(c.isOpen());
        // username is requested in AMQConnection#toString, so it can be accessed at any time
        assertThat(usernameRequested.get(), greaterThanOrEqualTo(2));
        assertThat(passwordRequested.get(), is(2));
    } finally {
        c.abort();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CredentialsProvider(com.rabbitmq.client.impl.CredentialsProvider) Test(org.junit.Test)

Aggregations

CredentialsProvider (com.rabbitmq.client.impl.CredentialsProvider)2 Test (org.junit.Test)2 Connection (com.rabbitmq.client.Connection)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 MetricsCollector (com.rabbitmq.client.MetricsCollector)1 AMQConnection (com.rabbitmq.client.impl.AMQConnection)1 ConnectionParams (com.rabbitmq.client.impl.ConnectionParams)1 FrameHandler (com.rabbitmq.client.impl.FrameHandler)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1