Search in sources :

Example 1 with RecoveryListener

use of com.rabbitmq.client.RecoveryListener 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 2 with RecoveryListener

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

the class TopologyRecoveryRetry method topologyRecoveryBindingFailure.

@Test
public void topologyRecoveryBindingFailure() throws Exception {
    final String queue = "topology-recovery-retry-binding-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 2nd binding
    // give the 2nd recorded binding a bad queue name so it fails
    final RecordedBinding binding2 = ((AutorecoveringConnection) connection).getRecordedBindings().get(1);
    binding2.destination(UUID.randomUUID().toString());
    // use the backoffConsumer to know that it has failed
    // then delete the real queue & fix the recorded binding
    // it should fail once more because queue is gone, and then succeed
    final CountDownLatch backoffLatch = new CountDownLatch(1);
    backoffConsumer = attempt -> {
        if (attempt == 1) {
            binding2.destination(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 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) RecordedBinding(com.rabbitmq.client.impl.recovery.RecordedBinding) CountDownLatch(java.util.concurrent.CountDownLatch) Envelope(com.rabbitmq.client.Envelope) 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 3 with RecoveryListener

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

the class NoAutoRecoveryWhenTcpWindowIsFullTest method failureAndRecovery.

@Test
public void failureAndRecovery() throws IOException, InterruptedException {
    final String queue = UUID.randomUUID().toString();
    final CountDownLatch recoveryLatch = new CountDownLatch(1);
    consumingConnection.addRecoveryListener(new RecoveryListener() {

        @Override
        public void handleRecovery(Recoverable recoverable) {
            recoveryLatch.countDown();
        }

        @Override
        public void handleRecoveryStarted(Recoverable recoverable) {
        }
    });
    declareQueue(producingChannel, queue);
    produceMessagesInBackground(producingChannel, queue);
    startConsumer(queue);
    assertThat(recoveryLatch.await(60, TimeUnit.SECONDS)).as("Connection should have been closed and should have recovered by now").isTrue();
    assertThat(consumerOkLatch.await(5, TimeUnit.SECONDS)).as("Consumer should have recovered by now").isTrue();
}
Also used : RecoveryListener(com.rabbitmq.client.RecoveryListener) CountDownLatch(java.util.concurrent.CountDownLatch) Recoverable(com.rabbitmq.client.Recoverable) Test(org.junit.Test)

Example 4 with RecoveryListener

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

the class Metrics method closeAndWaitForRecovery.

private void closeAndWaitForRecovery(AutorecoveringConnection connection) throws IOException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    connection.addRecoveryListener(new RecoveryListener() {

        public void handleRecovery(Recoverable recoverable) {
            latch.countDown();
        }

        @Override
        public void handleRecoveryStarted(Recoverable recoverable) {
        // no-op
        }
    });
    Host.closeConnection(connection);
    assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue();
}
Also used : RecoveryListener(com.rabbitmq.client.RecoveryListener) CountDownLatch(java.util.concurrent.CountDownLatch) Recoverable(com.rabbitmq.client.Recoverable)

Aggregations

Recoverable (com.rabbitmq.client.Recoverable)4 RecoveryListener (com.rabbitmq.client.RecoveryListener)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.junit.Test)3 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)2 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)2 Envelope (com.rabbitmq.client.Envelope)2 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)2 IOException (java.io.IOException)2 RecordedBinding (com.rabbitmq.client.impl.recovery.RecordedBinding)1 RecordedConsumer (com.rabbitmq.client.impl.recovery.RecordedConsumer)1