use of com.rabbitmq.client.ConnectionFactory 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();
}
}
use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.
the class SharedThreadPoolTest method willShutDownExecutor.
@Test
public void willShutDownExecutor() throws IOException, TimeoutException {
ExecutorService executor1 = null;
ExecutorService executor2 = null;
AMQConnection conn1 = null;
AMQConnection conn2 = null;
AMQConnection conn3 = null;
AMQConnection conn4 = null;
try {
ConnectionFactory cf = TestUtils.connectionFactory();
cf.setAutomaticRecoveryEnabled(false);
executor1 = Executors.newFixedThreadPool(8);
cf.setSharedExecutor(executor1);
conn1 = (AMQConnection) cf.newConnection();
assertFalse(conn1.willShutDownConsumerExecutor());
executor2 = Executors.newSingleThreadExecutor();
conn2 = (AMQConnection) cf.newConnection(executor2);
assertFalse(conn2.willShutDownConsumerExecutor());
conn3 = (AMQConnection) cf.newConnection((ExecutorService) null);
assertTrue(conn3.willShutDownConsumerExecutor());
cf.setSharedExecutor(null);
conn4 = (AMQConnection) cf.newConnection();
assertTrue(conn4.willShutDownConsumerExecutor());
} finally {
close(conn1);
close(conn2);
close(conn3);
close(conn4);
close(executor1);
close(executor2);
}
}
use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.
the class NoAutoRecoveryWhenTcpWindowIsFullTest method setUp.
@Before
public void setUp() throws Exception {
// we need several threads to publish, dispatch deliveries, handle RPC responses, etc.
executorService = Executors.newFixedThreadPool(10);
final ConnectionFactory factory = TestUtils.connectionFactory();
factory.setSocketConfigurator(new DefaultSocketConfigurator() {
/* default value on Linux */
int DEFAULT_RECEIVE_BUFFER_SIZE = 43690;
@Override
public void configure(Socket socket) throws IOException {
super.configure(socket);
socket.setReceiveBufferSize(DEFAULT_RECEIVE_BUFFER_SIZE);
}
});
factory.setAutomaticRecoveryEnabled(true);
factory.setTopologyRecoveryEnabled(true);
// we try to set the lower values for closing timeouts, etc.
// this makes the test execute faster.
factory.setRequestedHeartbeat(5);
factory.setSharedExecutor(executorService);
// we need the shutdown executor: channel shutting down depends on the work pool,
// which is full. Channel shutting down will time out with the shutdown executor.
factory.setShutdownExecutor(executorService);
factory.setNetworkRecoveryInterval(2000);
if (TestUtils.USE_NIO) {
factory.setWorkPoolTimeout(10 * 1000);
factory.setNioParams(new NioParams().setWriteQueueCapacity(10 * 1000 * 1000).setNbIoThreads(4));
}
producingConnection = (AutorecoveringConnection) factory.newConnection("Producer Connection");
producingChannel = (AutorecoveringChannel) producingConnection.createChannel();
consumingConnection = (AutorecoveringConnection) factory.newConnection("Consuming Connection");
consumingChannel = (AutorecoveringChannel) consumingConnection.createChannel();
consumerOkLatch = new CountDownLatch(2);
}
use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.
the class SslContextFactoryTest method setSslContextFactory.
@Test
public void setSslContextFactory() throws Exception {
doTestSetSslContextFactory(() -> {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.useBlockingIo();
connectionFactory.setAutomaticRecoveryEnabled(true);
return connectionFactory;
});
doTestSetSslContextFactory(() -> {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.useNio();
connectionFactory.setAutomaticRecoveryEnabled(true);
return connectionFactory;
});
doTestSetSslContextFactory(() -> {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.useBlockingIo();
connectionFactory.setAutomaticRecoveryEnabled(false);
return connectionFactory;
});
doTestSetSslContextFactory(() -> {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.useNio();
connectionFactory.setAutomaticRecoveryEnabled(false);
return connectionFactory;
});
}
use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.
the class SslContextFactoryTest method doTestSocketFactoryTakesPrecedenceOverSslContextFactoryWithBlockingIo.
private void doTestSocketFactoryTakesPrecedenceOverSslContextFactoryWithBlockingIo(Supplier<ConnectionFactory> supplier) throws Exception {
ConnectionFactory connectionFactory = supplier.get();
connectionFactory.useBlockingIo();
SslContextFactory sslContextFactory = sslContextFactory();
connectionFactory.setSslContextFactory(sslContextFactory);
SSLContext contextAcceptAll = sslContextFactory.create("connection01");
connectionFactory.setSocketFactory(contextAcceptAll.getSocketFactory());
Connection connection = connectionFactory.newConnection("connection01");
TestUtils.close(connection);
connection = connectionFactory.newConnection("connection02");
TestUtils.close(connection);
}
Aggregations