Search in sources :

Example 91 with ConnectionFactory

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

Example 92 with ConnectionFactory

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);
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) AMQConnection(com.rabbitmq.client.impl.AMQConnection) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 93 with ConnectionFactory

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);
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) NioParams(com.rabbitmq.client.impl.nio.NioParams) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultSocketConfigurator(com.rabbitmq.client.DefaultSocketConfigurator) Socket(java.net.Socket) Before(org.junit.Before)

Example 94 with ConnectionFactory

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;
    });
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Test(org.junit.Test)

Example 95 with 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);
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) SslContextFactory(com.rabbitmq.client.SslContextFactory) Connection(com.rabbitmq.client.Connection) SSLContext(javax.net.ssl.SSLContext)

Aggregations

ConnectionFactory (com.rabbitmq.client.ConnectionFactory)188 Connection (com.rabbitmq.client.Connection)87 Test (org.junit.Test)73 Channel (com.rabbitmq.client.Channel)63 IOException (java.io.IOException)53 TimeoutException (java.util.concurrent.TimeoutException)23 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)11 Envelope (com.rabbitmq.client.Envelope)11 SSLContext (javax.net.ssl.SSLContext)11 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)9 HashMap (java.util.HashMap)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 AMQP (com.rabbitmq.client.AMQP)6 Consumer (com.rabbitmq.client.Consumer)6 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)6 AMQConnection (com.rabbitmq.client.impl.AMQConnection)6 Address (com.rabbitmq.client.Address)5 NioParams (com.rabbitmq.client.impl.nio.NioParams)4 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)4 Path (javax.ws.rs.Path)3