Search in sources :

Example 11 with DefaultConsumer

use of com.rabbitmq.client.DefaultConsumer 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 12 with DefaultConsumer

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

the class Metrics method metrics.

@Test
public void metrics() throws IOException, TimeoutException {
    StandardMetricsCollector metrics = new StandardMetricsCollector();
    connectionFactory.setMetricsCollector(metrics);
    Connection connection1 = null;
    Connection connection2 = null;
    try {
        connection1 = connectionFactory.newConnection();
        assertThat(metrics.getConnections().getCount()).isEqualTo(1L);
        connection1.createChannel();
        connection1.createChannel();
        Channel channel = connection1.createChannel();
        assertThat(metrics.getChannels().getCount()).isEqualTo(3L);
        sendMessage(channel);
        assertThat(metrics.getPublishedMessages().getCount()).isEqualTo(1L);
        sendMessage(channel);
        assertThat(metrics.getPublishedMessages().getCount()).isEqualTo(2L);
        channel.basicGet(QUEUE, true);
        assertThat(metrics.getConsumedMessages().getCount()).isEqualTo(1L);
        channel.basicGet(QUEUE, true);
        assertThat(metrics.getConsumedMessages().getCount()).isEqualTo(2L);
        channel.basicGet(QUEUE, true);
        assertThat(metrics.getConsumedMessages().getCount()).isEqualTo(2L);
        connection2 = connectionFactory.newConnection();
        assertThat(metrics.getConnections().getCount()).isEqualTo(2L);
        connection2.createChannel();
        channel = connection2.createChannel();
        assertThat(metrics.getChannels().getCount()).isEqualTo(3L + 2L);
        sendMessage(channel);
        sendMessage(channel);
        assertThat(metrics.getPublishedMessages().getCount()).isEqualTo(2L + 2L);
        channel.basicGet(QUEUE, true);
        assertThat(metrics.getConsumedMessages().getCount()).isEqualTo(2L + 1L);
        channel.basicConsume(QUEUE, true, new DefaultConsumer(channel));
        waitAtMost(timeout(), () -> metrics.getConsumedMessages().getCount() == 2L + 1L + 1L);
        safeClose(connection1);
        waitAtMost(timeout(), () -> metrics.getConnections().getCount() == 1L);
        waitAtMost(timeout(), () -> metrics.getChannels().getCount() == 2L);
        safeClose(connection2);
        waitAtMost(timeout(), () -> metrics.getConnections().getCount() == 0L);
        waitAtMost(timeout(), () -> metrics.getChannels().getCount() == 0L);
        assertThat(metrics.getAcknowledgedMessages().getCount()).isEqualTo(0L);
        assertThat(metrics.getRejectedMessages().getCount()).isEqualTo(0L);
    } finally {
        safeClose(connection1);
        safeClose(connection2);
    }
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) StandardMetricsCollector(com.rabbitmq.client.impl.StandardMetricsCollector) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) AutorecoveringConnection(com.rabbitmq.client.impl.recovery.AutorecoveringConnection) Test(org.junit.Test)

Example 13 with DefaultConsumer

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

the class TopologyRecoveryRetry method topologyRecoveryConsumerFailure.

@Test
public void topologyRecoveryConsumerFailure() throws Exception {
    final String queue = "topology-recovery-retry-consumer-failure" + System.currentTimeMillis();
    channel.queueDeclare(queue, false, false, true, new HashMap<>());
    channel.queueBind(queue, "amq.topic", "topic1");
    channel.queueBind(queue, "amq.topic", "topic2");
    final CountDownLatch messagesReceivedLatch = new CountDownLatch(2);
    channel.basicConsume(queue, true, new DefaultConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) {
            messagesReceivedLatch.countDown();
        }
    });
    final CountDownLatch recoveryLatch = new CountDownLatch(1);
    ((AutorecoveringConnection) connection).addRecoveryListener(new RecoveryListener() {

        @Override
        public void handleRecoveryStarted(Recoverable recoverable) {
        // no-op
        }

        @Override
        public void handleRecovery(Recoverable recoverable) {
            recoveryLatch.countDown();
        }
    });
    // we want recovery to fail when recovering the consumer
    // give the recorded consumer a bad queue name so it fails
    final RecordedConsumer consumer = ((AutorecoveringConnection) connection).getRecordedConsumers().values().iterator().next();
    consumer.setQueue(UUID.randomUUID().toString());
    // use the backoffConsumer to know that it has failed
    // then delete the real queue & fix the recorded consumer
    // it should fail once more because queue is gone, and then succeed
    final CountDownLatch backoffLatch = new CountDownLatch(1);
    backoffConsumer = attempt -> {
        if (attempt == 1) {
            consumer.setQueue(queue);
            try {
                Host.rabbitmqctl("delete_queue " + queue);
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        backoffLatch.countDown();
    };
    // close connection
    Host.closeAllConnections();
    // assert backoff was called
    assertTrue(backoffLatch.await(90, TimeUnit.SECONDS));
    // wait for full recovery
    assertTrue(recoveryLatch.await(90, TimeUnit.SECONDS));
    // publish messages to verify both bindings & consumer were recovered
    basicPublishVolatile("test1".getBytes(), "amq.topic", "topic1");
    basicPublishVolatile("test2".getBytes(), "amq.topic", "topic2");
    assertTrue(messagesReceivedLatch.await(10, TimeUnit.SECONDS));
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) CountDownLatch(java.util.concurrent.CountDownLatch) Envelope(com.rabbitmq.client.Envelope) RecordedConsumer(com.rabbitmq.client.impl.recovery.RecordedConsumer) IOException(java.io.IOException) RecoveryListener(com.rabbitmq.client.RecoveryListener) AutorecoveringConnection(com.rabbitmq.client.impl.recovery.AutorecoveringConnection) Recoverable(com.rabbitmq.client.Recoverable) Test(org.junit.Test)

Example 14 with DefaultConsumer

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

the class Bug19219Test method it.

@Test
public void it() throws IOException, InterruptedException {
    final Consumer c = new DefaultConsumer(channel);
    // each.
    for (int i = 0; i < Q_COUNT; i++) {
        String qName = channel.queueDeclare().getQueue();
        channel.queueBind(qName, "amq.fanout", "");
        channel.basicConsume(qName, false, c);
    }
    // 2. send lots of messages in background, to keep the server,
    // and especially the queues, busy
    final Runnable r = new Runnable() {

        public void run() {
            try {
                startPublisher();
            } catch (IOException e) {
            } catch (InterruptedException e) {
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
        }
    };
    for (int i = 0; i < PUB_THREAD_COUNT; i++) {
        final Thread t = new Thread(r);
        t.start();
        // wait for thread to finish initialisation
        init.acquire();
    }
    // tell all threads to resume
    resume.countDown();
    // wait for threads to get into full swing
    Thread.sleep(CLOSE_DELAY);
    // 3. close channel. This will result in the server notifying
    // all the queues in parallel, which in turn will requeue all
    // the messages. The combined workload may result in some
    // notifications timing out.
    boolean success = false;
    try {
        channel.abort();
        success = true;
    } catch (ShutdownSignalException e) {
    } finally {
        // We deliberately do not perform a clean shutdown of all
        // the connections. This test is pushing the server really
        // hard, so we chose the quickest way to end things.
        channel = null;
        connection = null;
        assertTrue(success);
    }
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Consumer(com.rabbitmq.client.Consumer) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 15 with DefaultConsumer

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

the class QueueLifecycle method exclusiveNotAutoDelete.

@Test
public void exclusiveNotAutoDelete() throws IOException {
    String name = "exclusivequeue";
    channel.queueDeclare(name, false, true, false, null);
    // now it's there
    verifyQueue(name, false, true, false, null);
    Consumer consumer = new DefaultConsumer(channel);
    String consumerTag = channel.basicConsume(name, consumer);
    channel.basicCancel(consumerTag);
    // and still there, because exclusive no longer implies autodelete
    verifyQueueExists(name);
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Consumer(com.rabbitmq.client.Consumer) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Test(org.junit.Test)

Aggregations

DefaultConsumer (com.rabbitmq.client.DefaultConsumer)32 IOException (java.io.IOException)21 Envelope (com.rabbitmq.client.Envelope)20 Channel (com.rabbitmq.client.Channel)15 Test (org.junit.Test)15 AMQP (com.rabbitmq.client.AMQP)11 TimeoutException (java.util.concurrent.TimeoutException)10 Connection (com.rabbitmq.client.Connection)9 Consumer (com.rabbitmq.client.Consumer)9 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)8 CountDownLatch (java.util.concurrent.CountDownLatch)4 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)3 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)3 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 UUID (java.util.UUID)3 Path (javax.ws.rs.Path)3 AlreadyClosedException (com.rabbitmq.client.AlreadyClosedException)2 Recoverable (com.rabbitmq.client.Recoverable)2 RecoveryListener (com.rabbitmq.client.RecoveryListener)2