Search in sources :

Example 1 with RefreshProtectedCredentialsProvider

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

the class RefreshCredentialsTest method connectionAndRefreshCredentials.

@Test
public void connectionAndRefreshCredentials() throws Exception {
    ConnectionFactory cf = TestUtils.connectionFactory();
    CountDownLatch latch = new CountDownLatch(5);
    RefreshProtectedCredentialsProvider<TestToken> provider = new RefreshProtectedCredentialsProvider<TestToken>() {

        @Override
        protected TestToken retrieveToken() {
            latch.countDown();
            return new TestToken("guest", 2, Instant.now());
        }

        @Override
        protected String usernameFromToken(TestToken token) {
            return "guest";
        }

        @Override
        protected String passwordFromToken(TestToken token) {
            return token.secret;
        }

        @Override
        protected Duration timeBeforeExpiration(TestToken token) {
            return token.getTimeBeforeExpiration();
        }
    };
    cf.setCredentialsProvider(provider);
    refreshService = new DefaultCredentialsRefreshService.DefaultCredentialsRefreshServiceBuilder().refreshDelayStrategy(DefaultCredentialsRefreshService.fixedDelayBeforeExpirationRefreshDelayStrategy(Duration.ofSeconds(1))).approachingExpirationStrategy(expiration -> false).build();
    cf.setCredentialsRefreshService(refreshService);
    try (Connection c = cf.newConnection()) {
        Channel ch = c.createChannel();
        String queue = ch.queueDeclare().getQueue();
        TestUtils.sendAndConsumeMessage("", queue, queue, c);
        assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) RefreshProtectedCredentialsProvider(com.rabbitmq.client.impl.RefreshProtectedCredentialsProvider) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Channel (com.rabbitmq.client.Channel)1 Connection (com.rabbitmq.client.Connection)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 RefreshProtectedCredentialsProvider (com.rabbitmq.client.impl.RefreshProtectedCredentialsProvider)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1