Search in sources :

Example 16 with ConnectionFactory

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

the class CloseInMainLoop method specialConnectionFactory.

private ConnectionFactory specialConnectionFactory() {
    ConnectionFactory f = TestUtils.connectionFactory();
    f.setExceptionHandler(new DefaultExceptionHandler() {

        @Override
        public void handleConsumerException(Channel channel, Throwable exception, Consumer consumer, String consumerTag, String methodName) {
            try {
                // TODO: change this to call 4-parameter close and make 6-parm one private
                ((AMQConnection) channel.getConnection()).close(AMQP.INTERNAL_ERROR, "Internal error in Consumer " + consumerTag, false, exception, -1, false);
            } catch (Throwable e) {
            // Man, this clearly isn't our day.
            // TODO: Log the nested failure
            } finally {
                closeLatch.countDown();
            }
        }
    });
    return f;
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Consumer(com.rabbitmq.client.Consumer) Channel(com.rabbitmq.client.Channel) DefaultExceptionHandler(com.rabbitmq.client.impl.DefaultExceptionHandler)

Example 17 with ConnectionFactory

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

the class ConnectionFactoryTest method tryNextAddressIfTimeoutExceptionNoAutoRecovery.

// see https://github.com/rabbitmq/rabbitmq-java-client/issues/262
@Test
public void tryNextAddressIfTimeoutExceptionNoAutoRecovery() throws IOException, TimeoutException {
    final AMQConnection connectionThatThrowsTimeout = mock(AMQConnection.class);
    final AMQConnection connectionThatSucceeds = mock(AMQConnection.class);
    final Queue<AMQConnection> connections = new ArrayBlockingQueue<AMQConnection>(10);
    connections.add(connectionThatThrowsTimeout);
    connections.add(connectionThatSucceeds);
    ConnectionFactory connectionFactory = new ConnectionFactory() {

        @Override
        protected AMQConnection createConnection(ConnectionParams params, FrameHandler frameHandler, MetricsCollector metricsCollector) {
            return connections.poll();
        }

        @Override
        protected synchronized FrameHandlerFactory createFrameHandlerFactory() throws IOException {
            return mock(FrameHandlerFactory.class);
        }
    };
    connectionFactory.setAutomaticRecoveryEnabled(false);
    doThrow(TimeoutException.class).when(connectionThatThrowsTimeout).start();
    doNothing().when(connectionThatSucceeds).start();
    Connection returnedConnection = connectionFactory.newConnection(new Address[] { new Address("host1"), new Address("host2") });
    assertSame(connectionThatSucceeds, returnedConnection);
}
Also used : MetricsCollector(com.rabbitmq.client.MetricsCollector) FrameHandler(com.rabbitmq.client.impl.FrameHandler) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Address(com.rabbitmq.client.Address) AMQConnection(com.rabbitmq.client.impl.AMQConnection) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Connection(com.rabbitmq.client.Connection) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) Test(org.junit.Test)

Example 18 with ConnectionFactory

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

the class ConnectionFactoryTest method customizeCredentialsProvider.

// see https://github.com/rabbitmq/rabbitmq-java-client/pull/350
@Test
public void customizeCredentialsProvider() throws Exception {
    final CredentialsProvider provider = mock(CredentialsProvider.class);
    final AMQConnection connection = mock(AMQConnection.class);
    final AtomicBoolean createCalled = new AtomicBoolean(false);
    ConnectionFactory connectionFactory = new ConnectionFactory() {

        @Override
        protected AMQConnection createConnection(ConnectionParams params, FrameHandler frameHandler, MetricsCollector metricsCollector) {
            assertSame(provider, params.getCredentialsProvider());
            createCalled.set(true);
            return connection;
        }
    };
    connectionFactory.setCredentialsProvider(provider);
    connectionFactory.setAutomaticRecoveryEnabled(false);
    doNothing().when(connection).start();
    Connection returnedConnection = connectionFactory.newConnection();
    assertSame(returnedConnection, connection);
    assertTrue(createCalled.get());
}
Also used : MetricsCollector(com.rabbitmq.client.MetricsCollector) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FrameHandler(com.rabbitmq.client.impl.FrameHandler) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) AMQConnection(com.rabbitmq.client.impl.AMQConnection) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Connection(com.rabbitmq.client.Connection) CredentialsProvider(com.rabbitmq.client.impl.CredentialsProvider) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) Test(org.junit.Test)

Example 19 with ConnectionFactory

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

the class DnsRecordIpAddressResolverTests method loopbackInterfaceResolution.

@Test
public void loopbackInterfaceResolution() throws IOException, TimeoutException {
    DnsRecordIpAddressResolver addressResolver = new DnsRecordIpAddressResolver("127.0.0.1");
    ConnectionFactory connectionFactory = newConnectionFactory();
    Connection connection = connectionFactory.newConnection(addressResolver);
    try {
        connection.createChannel();
    } finally {
        connection.abort();
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DnsRecordIpAddressResolver(com.rabbitmq.client.DnsRecordIpAddressResolver) Connection(com.rabbitmq.client.Connection) Test(org.junit.Test)

Example 20 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)

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