Search in sources :

Example 21 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 22 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)

Example 23 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.

the class SslContextFactoryTest method doTestSetSslContextFactory.

private void doTestSetSslContextFactory(Supplier<ConnectionFactory> supplier) throws Exception {
    ConnectionFactory connectionFactory = supplier.get();
    SslContextFactory sslContextFactory = sslContextFactory();
    connectionFactory.setSslContextFactory(sslContextFactory);
    Connection connection = connectionFactory.newConnection("connection01");
    TestUtils.close(connection);
    try {
        connectionFactory.newConnection("connection02");
        fail("The SSL context of this client should not trust the server");
    } catch (SSLHandshakeException e) {
    // OK
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) SslContextFactory(com.rabbitmq.client.SslContextFactory) Connection(com.rabbitmq.client.Connection) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 24 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 25 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.

the class StrictExceptionHandlerTest method tooLongClosingMessage.

@Test
public void tooLongClosingMessage() throws Exception {
    ConnectionFactory cf = TestUtils.connectionFactory();
    final CountDownLatch latch = new CountDownLatch(1);
    cf.setExceptionHandler(new StrictExceptionHandler() {

        @Override
        public void handleConsumerException(Channel channel, Throwable exception, Consumer consumer, String consumerTag, String methodName) {
            try {
                super.handleConsumerException(channel, exception, consumer, consumerTag, methodName);
            } catch (IllegalArgumentException e) {
                fail("No exception should caught");
            }
            latch.countDown();
        }
    });
    try (Connection c = cf.newConnection()) {
        Channel channel = c.createChannel();
        String queue = channel.queueDeclare().getQueue();
        channel.basicConsume(queue, new VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongClassName(channel));
        channel.basicPublish("", queue, null, new byte[0]);
        assertThat(latch.await(5, TimeUnit.SECONDS), is(true));
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Consumer(com.rabbitmq.client.Consumer) Channel(com.rabbitmq.client.Channel) StrictExceptionHandler(com.rabbitmq.client.impl.StrictExceptionHandler) Connection(com.rabbitmq.client.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ConnectionFactory (com.rabbitmq.client.ConnectionFactory)64 Connection (com.rabbitmq.client.Connection)28 Test (org.junit.Test)26 IOException (java.io.IOException)17 Channel (com.rabbitmq.client.Channel)13 AMQConnection (com.rabbitmq.client.impl.AMQConnection)6 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)4 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)3 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)3 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)3 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 TimeoutException (java.util.concurrent.TimeoutException)3 AMQP (com.rabbitmq.client.AMQP)2 Address (com.rabbitmq.client.Address)2 Consumer (com.rabbitmq.client.Consumer)2 DnsRecordIpAddressResolver (com.rabbitmq.client.DnsRecordIpAddressResolver)2 MetricsCollector (com.rabbitmq.client.MetricsCollector)2 ShutdownListener (com.rabbitmq.client.ShutdownListener)2 SslContextFactory (com.rabbitmq.client.SslContextFactory)2