Search in sources :

Example 26 with Envelope

use of com.rabbitmq.client.Envelope 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 27 with Envelope

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

the class CloseInMainLoop method closeWithFaultyConsumer.

// The thrown runtime exception should get intercepted by the
// consumer exception handler, and result in a clean shut down.
@Test
public void closeWithFaultyConsumer() throws Exception {
    SpecialConnection connection = new SpecialConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare("x", "direct");
    channel.queueDeclare("q", false, false, false, null);
    channel.queueBind("q", "x", "k");
    channel.basicConsume("q", true, new DefaultConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
            throw new RuntimeException("I am a bad consumer");
        }
    });
    channel.basicPublish("x", "k", null, new byte[10]);
    assertTrue(closeLatch.await(1000, TimeUnit.MILLISECONDS));
    assertTrue(connection.hadValidShutdown());
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) AMQP(com.rabbitmq.client.AMQP) Channel(com.rabbitmq.client.Channel) Envelope(com.rabbitmq.client.Envelope) Test(org.junit.Test)

Example 28 with Envelope

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

the class ChannelAsyncCompletableFutureTest method async.

@Test
public void async() throws Exception {
    channel.confirmSelect();
    CountDownLatch latch = new CountDownLatch(1);
    AMQP.Queue.Declare queueDeclare = new AMQImpl.Queue.Declare.Builder().queue(queue).durable(true).exclusive(false).autoDelete(false).arguments(null).build();
    channel.asyncCompletableRpc(queueDeclare).thenComposeAsync(action -> {
        try {
            return channel.asyncCompletableRpc(new AMQImpl.Exchange.Declare.Builder().exchange(exchange).type("fanout").durable(false).autoDelete(false).arguments(null).build());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }, executor).thenComposeAsync(action -> {
        try {
            return channel.asyncCompletableRpc(new AMQImpl.Queue.Bind.Builder().queue(queue).exchange(exchange).routingKey("").arguments(null).build());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }, executor).thenAcceptAsync(action -> {
        try {
            channel.basicPublish("", queue, null, "dummy".getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }, executor).thenAcceptAsync((whatever) -> {
        try {
            channel.basicConsume(queue, true, new DefaultConsumer(channel) {

                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    latch.countDown();
                }
            });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }, executor);
    channel.waitForConfirmsOrDie(1000);
    assertTrue(latch.await(2, TimeUnit.SECONDS));
}
Also used : Envelope(com.rabbitmq.client.Envelope) java.util.concurrent(java.util.concurrent) After(org.junit.After) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) UUID(java.util.UUID) AMQImpl(com.rabbitmq.client.impl.AMQImpl) AMQP(com.rabbitmq.client.AMQP) Before(org.junit.Before) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope) AMQP(com.rabbitmq.client.AMQP) Test(org.junit.Test)

Example 29 with Envelope

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

the class QosTests method singleChannelAndQueueFairness.

@Test
public void singleChannelAndQueueFairness() throws IOException {
    // check that when we have multiple consumers on the same
    // channel & queue, and a prefetch limit set, that all
    // consumers get a fair share of the messages.
    channel.basicQos(1, true);
    String q = channel.queueDeclare().getQueue();
    channel.queueBind(q, "amq.fanout", "");
    final Map<String, Integer> counts = Collections.synchronizedMap(new HashMap<String, Integer>());
    QueueingConsumer c = new QueueingConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            counts.put(consumerTag, counts.get(consumerTag) + 1);
            super.handleDelivery(consumerTag, envelope, properties, body);
        }
    };
    channel.basicConsume(q, false, "c1", c);
    channel.basicConsume(q, false, "c2", c);
    int count = 10;
    counts.put("c1", 0);
    counts.put("c2", 0);
    fill(count);
    try {
        for (int i = 0; i < count; i++) {
            Delivery d = c.nextDelivery();
            channel.basicAck(d.getEnvelope().getDeliveryTag(), false);
        }
    } catch (InterruptedException ie) {
        fail("interrupted");
    }
    // we only check that the server isn't grossly unfair; perfect
    // fairness is too much to ask for (even though RabbitMQ atm
    // does actually provide it in this case)
    assertTrue(counts.get("c1").intValue() > 0);
    assertTrue(counts.get("c2").intValue() > 0);
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Delivery(com.rabbitmq.client.QueueingConsumer.Delivery) Envelope(com.rabbitmq.client.Envelope) Test(org.junit.Test)

Example 30 with Envelope

use of com.rabbitmq.client.Envelope 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

Envelope (com.rabbitmq.client.Envelope)31 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)20 AMQP (com.rabbitmq.client.AMQP)18 IOException (java.io.IOException)17 Channel (com.rabbitmq.client.Channel)16 Test (org.junit.Test)12 Connection (com.rabbitmq.client.Connection)11 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)10 TimeoutException (java.util.concurrent.TimeoutException)9 GetResponse (com.rabbitmq.client.GetResponse)8 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)6 LongString (com.rabbitmq.client.LongString)5 Consumer (com.rabbitmq.client.Consumer)4 BasicProperties (de.gessnerfl.rabbitmq.queue.management.model.BasicProperties)4 Message (de.gessnerfl.rabbitmq.queue.management.model.Message)4 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 UUID (java.util.UUID)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Path (javax.ws.rs.Path)3