Search in sources :

Example 26 with DefaultConsumer

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

the class TopologyRecoveryFiltering method topologyRecoveryFilteringConsumers.

@Test
public void topologyRecoveryFilteringConsumers() throws Exception {
    Channel ch = c.createChannel();
    ch.exchangeDeclare("topology.recovery.exchange", "direct");
    ch.queueDeclare("topology.recovery.queue.1", false, false, false, null);
    ch.queueDeclare("topology.recovery.queue.2", false, false, false, null);
    ch.queueBind("topology.recovery.queue.1", "topology.recovery.exchange", "recovered.consumer");
    ch.queueBind("topology.recovery.queue.2", "topology.recovery.exchange", "filtered.consumer");
    final AtomicInteger recoveredConsumerMessageCount = new AtomicInteger(0);
    ch.basicConsume("topology.recovery.queue.1", true, "recovered.consumer", new DefaultConsumer(ch) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            recoveredConsumerMessageCount.incrementAndGet();
        }
    });
    ch.basicPublish("topology.recovery.exchange", "recovered.consumer", null, "".getBytes());
    waitAtMost(Duration.ofSeconds(5), () -> recoveredConsumerMessageCount.get() == 1);
    final AtomicInteger filteredConsumerMessageCount = new AtomicInteger(0);
    final CountDownLatch filteredConsumerLatch = new CountDownLatch(2);
    ch.basicConsume("topology.recovery.queue.2", true, "filtered.consumer", new DefaultConsumer(ch) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            filteredConsumerMessageCount.incrementAndGet();
            filteredConsumerLatch.countDown();
        }
    });
    ch.basicPublish("topology.recovery.exchange", "filtered.consumer", null, "".getBytes());
    waitAtMost(Duration.ofSeconds(5), () -> filteredConsumerMessageCount.get() == 1);
    closeAndWaitForRecovery((RecoverableConnection) c);
    int initialCount = recoveredConsumerMessageCount.get();
    ch.basicPublish("topology.recovery.exchange", "recovered.consumer", null, "".getBytes());
    waitAtMost(Duration.ofSeconds(5), () -> recoveredConsumerMessageCount.get() == initialCount + 1);
    ch.basicPublish("topology.recovery.exchange", "filtered.consumer", null, "".getBytes());
    assertFalse("Consumer shouldn't recover, no extra messages should have been received", filteredConsumerLatch.await(5, TimeUnit.SECONDS));
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AMQP(com.rabbitmq.client.AMQP) Channel(com.rabbitmq.client.Channel) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 27 with DefaultConsumer

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

the class QueueLifecycle method queueAutoDelete.

// Note that this assumes that auto-deletion is synchronous with
// basic.cancel,
// which is not actually in the spec. (If it isn't, there's a race here).
@Test
public void queueAutoDelete() throws IOException {
    String name = "tempqueue";
    channel.queueDeclare(name, false, false, true, null);
    // now it's there
    verifyQueue(name, false, false, true, null);
    Consumer consumer = new DefaultConsumer(channel);
    String consumerTag = channel.basicConsume(name, consumer);
    channel.basicCancel(consumerTag);
    // now it's not .. we hope
    try {
        verifyQueueExists(name);
    } catch (IOException ioe) {
        checkShutdownSignal(AMQP.NOT_FOUND, ioe);
        return;
    }
    fail("Queue should have been auto-deleted after we removed its only consumer");
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Consumer(com.rabbitmq.client.Consumer) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) IOException(java.io.IOException) Test(org.junit.Test)

Example 28 with DefaultConsumer

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

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

the class TopologyRecoveryRetry method topologyRecoveryRetry.

@Test
public void topologyRecoveryRetry() throws Exception {
    int nbQueues = 200;
    String prefix = "topology-recovery-retry-" + System.currentTimeMillis();
    for (int i = 0; i < nbQueues; i++) {
        String queue = prefix + i;
        channel.queueDeclare(queue, false, false, true, new HashMap<>());
        channel.queueBind(queue, "amq.direct", queue);
        channel.queueBind(queue, "amq.direct", queue + "2");
        channel.basicConsume(queue, true, new DefaultConsumer(channel));
    }
    closeAllConnectionsAndWaitForRecovery(this.connection);
    assertTrue(channel.isOpen());
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Test(org.junit.Test)

Example 30 with DefaultConsumer

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

the class PriorityQueues method prioritiesOfEnqueuedMessages.

private List<Integer> prioritiesOfEnqueuedMessages(String q, int n) throws InterruptedException, IOException {
    final List<Integer> xs = new ArrayList<Integer>();
    final CountDownLatch latch = new CountDownLatch(n);
    channel.basicConsume(q, true, new DefaultConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            xs.add(properties.getPriority());
            latch.countDown();
        }
    });
    latch.await(5, TimeUnit.SECONDS);
    return xs;
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) AMQP(com.rabbitmq.client.AMQP) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Envelope(com.rabbitmq.client.Envelope)

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