Search in sources :

Example 71 with ConnectionFactory

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

the class TopologyRecoveryRetry method newConnectionFactory.

@Override
protected ConnectionFactory newConnectionFactory() {
    ConnectionFactory connectionFactory = TestUtils.connectionFactory();
    connectionFactory.setTopologyRecoveryRetryHandler(RETRY_ON_QUEUE_NOT_FOUND_RETRY_HANDLER.backoffPolicy(attempt -> backoffConsumer.accept(attempt)).build());
    connectionFactory.setNetworkRecoveryInterval(1000);
    return connectionFactory;
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory)

Example 72 with ConnectionFactory

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

the class ExceptionHandling method nullExceptionHandler.

@Test
public void nullExceptionHandler() {
    ConnectionFactory cf = TestUtils.connectionFactory();
    try {
        cf.setExceptionHandler(null);
        fail("expected setExceptionHandler to throw");
    } catch (IllegalArgumentException iae) {
    // expected
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Test(org.junit.Test)

Example 73 with ConnectionFactory

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

the class ExceptionHandling method testConsumerHandleConsumerException.

protected void testConsumerHandleConsumerException(ExceptionHandler eh, CountDownLatch latch, boolean expectChannelClose) throws InterruptedException, TimeoutException, IOException {
    ConnectionFactory cf = newConnectionFactory(eh);
    assertEquals(cf.getExceptionHandler(), eh);
    Connection conn = cf.newConnection();
    assertEquals(conn.getExceptionHandler(), eh);
    Channel ch = conn.createChannel();
    String q = ch.queueDeclare().getQueue();
    ch.basicConsume(q, new DefaultConsumer(ch) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            throw new RuntimeException("exception expected here, don't freak out");
        }
    });
    ch.basicPublish("", q, null, "".getBytes());
    wait(latch);
    assertEquals(!expectChannelClose, ch.isOpen());
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) AMQP(com.rabbitmq.client.AMQP) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope)

Example 74 with ConnectionFactory

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

the class BlockedConnection method connection.

private Connection connection(final CountDownLatch latch) throws IOException, TimeoutException {
    ConnectionFactory factory = TestUtils.connectionFactory();
    Connection connection = factory.newConnection();
    connection.addBlockedListener(new BlockedListener() {

        public void handleBlocked(String reason) throws IOException {
            try {
                unblock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public void handleUnblocked() throws IOException {
            latch.countDown();
        }
    });
    return connection;
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) BlockedListener(com.rabbitmq.client.BlockedListener) IOException(java.io.IOException)

Example 75 with ConnectionFactory

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

the class ChannelLimitNegotiation method openingTooManyChannels.

@Test
public void openingTooManyChannels() throws Exception {
    int n = 48;
    Connection conn = null;
    try {
        Host.rabbitmqctl("eval 'application:set_env(rabbit, channel_max, " + n + ").'");
        ConnectionFactory cf = TestUtils.connectionFactory();
        conn = cf.newConnection();
        assertEquals(n, conn.getChannelMax());
        for (int i = 1; i <= n; i++) {
            assertNotNull(conn.createChannel(i));
        }
        // ChannelManager guards against channel.open being sent
        assertNull(conn.createChannel(n + 1));
        // Construct a channel directly
        final ChannelN ch = new ChannelN(((AutorecoveringConnection) conn).getDelegate(), n + 1, new ConsumerWorkService(Executors.newSingleThreadExecutor(), Executors.defaultThreadFactory(), ConnectionFactory.DEFAULT_SHUTDOWN_TIMEOUT));
        conn.addShutdownListener(new ShutdownListener() {

            public void shutdownCompleted(ShutdownSignalException cause) {
                // make sure channel.open continuation is released
                ch.processShutdownSignal(cause, true, true);
            }
        });
        ch.open();
        fail("expected channel.open to cause a connection exception");
    } catch (IOException e) {
        checkShutdownSignal(530, e);
    } finally {
        TestUtils.abort(conn);
        Host.rabbitmqctl("eval 'application:set_env(rabbit, channel_max, 0).'");
    }
}
Also used : ShutdownListener(com.rabbitmq.client.ShutdownListener) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) ChannelN(com.rabbitmq.client.impl.ChannelN) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Connection(com.rabbitmq.client.Connection) AutorecoveringConnection(com.rabbitmq.client.impl.recovery.AutorecoveringConnection) ConsumerWorkService(com.rabbitmq.client.impl.ConsumerWorkService) IOException(java.io.IOException) Test(org.junit.Test)

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