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();
}
}
Aggregations