Search in sources :

Example 16 with QueueingConsumer

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

the class QosTests method noAckObeysLimit.

@Test
public void noAckObeysLimit() throws IOException {
    channel.basicQos(1, true);
    QueueingConsumer c1 = new QueueingConsumer(channel);
    declareBindConsume(channel, c1, false);
    fill(1);
    QueueingConsumer c2 = new QueueingConsumer(channel);
    declareBindConsume(channel, c2, true);
    fill(1);
    try {
        Delivery d = c2.nextDelivery(1000);
        assertNull(d);
    } catch (InterruptedException ie) {
        fail("interrupted");
    }
    List<Delivery> d = drain(c1, 1);
    // must ack before the next one appears
    ack(d, false);
    d = drain(c1, 1);
    ack(d, false);
    drain(c2, 1);
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Delivery(com.rabbitmq.client.QueueingConsumer.Delivery) Test(org.junit.Test)

Example 17 with QueueingConsumer

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

the class QosTests method messageLimitUnlimited.

@Test
public void messageLimitUnlimited() throws IOException {
    QueueingConsumer c = new QueueingConsumer(channel);
    configure(c, 0, 1, 2);
    drain(c, 2);
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Test(org.junit.Test)

Example 18 with QueueingConsumer

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

the class Nack method singleNack.

@Test
public void singleNack() throws Exception {
    String q = queueCreator.apply(channel);
    byte[] m1 = "1".getBytes();
    byte[] m2 = "2".getBytes();
    channel.confirmSelect();
    basicPublishVolatile(m1, q);
    basicPublishVolatile(m2, q);
    channel.waitForConfirmsOrDie(1000);
    long tag1 = checkDelivery(channel.basicGet(q, false), m1, false);
    long tag2 = checkDelivery(channel.basicGet(q, false), m2, false);
    QueueingConsumer c = new QueueingConsumer(secondaryChannel);
    String consumerTag = secondaryChannel.basicConsume(q, false, c);
    // requeue
    channel.basicNack(tag2, false, true);
    long tag3 = checkDelivery(c.nextDelivery(), m2, true);
    secondaryChannel.basicCancel(consumerTag);
    // no requeue
    secondaryChannel.basicNack(tag3, false, false);
    assertNull(channel.basicGet(q, false));
    channel.basicAck(tag1, false);
    channel.basicNack(tag3, false, true);
    expectError(AMQP.PRECONDITION_FAILED);
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Test(org.junit.Test)

Example 19 with QueueingConsumer

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

the class QueueingConsumerTests method nThreadShutdown.

@Test
public void nThreadShutdown() throws Exception {
    Channel channel = connection.createChannel();
    final QueueingConsumer c = new QueueingConsumer(channel);
    channel.queueDeclare(QUEUE, false, true, true, null);
    channel.basicConsume(QUEUE, c);
    final AtomicInteger count = new AtomicInteger(THREADS);
    final CountDownLatch latch = new CountDownLatch(THREADS);
    for (int i = 0; i < THREADS; i++) {
        new Thread() {

            @Override
            public void run() {
                try {
                    while (true) {
                        c.nextDelivery();
                    }
                } catch (ShutdownSignalException sig) {
                    count.decrementAndGet();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                } finally {
                    latch.countDown();
                }
            }
        }.start();
    }
    connection.close();
    // Far longer than this could reasonably take
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertEquals(0, count.get());
}
Also used : ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Channel(com.rabbitmq.client.Channel) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) IOException(java.io.IOException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) Test(org.junit.Test)

Example 20 with QueueingConsumer

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

the class BindingLifecycleBase method doAutoDelete.

protected void doAutoDelete(boolean durable, int queues) throws IOException, TimeoutException {
    String[] queueNames = null;
    Binding binding = Binding.randomBinding();
    channel.exchangeDeclare(binding.x, "direct", durable, true, null);
    channel.queueDeclare(binding.q, durable, false, true, null);
    channel.queueBind(binding.q, binding.x, binding.k);
    if (queues > 1) {
        int j = queues - 1;
        queueNames = new String[j];
        for (int i = 0; i < j; i++) {
            queueNames[i] = randomString();
            channel.queueDeclare(queueNames[i], durable, false, false, null);
            channel.queueBind(queueNames[i], binding.x, binding.k);
            channel.basicConsume(queueNames[i], true, new QueueingConsumer(channel));
        }
    }
    subscribeSendUnsubscribe(binding);
    if (durable) {
        restart();
    }
    if (queues > 1 && queueNames != null) {
        for (String s : queueNames) {
            channel.basicConsume(s, true, new QueueingConsumer(channel));
            Binding tmp = new Binding(s, binding.x, binding.k);
            sendUnroutable(tmp);
        }
    }
    channel.queueDeclare(binding.q, durable, true, true, null);
    // bind should fail
    try {
        channel.queueBind(binding.q, binding.x, binding.k);
        sendRoutable(binding);
    } catch (IOException e) {
        checkShutdownSignal(AMQP.NOT_FOUND, e);
        channel = null;
        return;
    }
    if (queues == 1) {
        deleteExchangeAndQueue(binding);
        fail("Queue bind should have failed");
    }
    // Do some cleanup
    if (queues > 1 && queueNames != null) {
        for (String q : queueNames) {
            channel.queueDelete(q);
        }
    }
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) IOException(java.io.IOException)

Aggregations

QueueingConsumer (com.rabbitmq.client.QueueingConsumer)55 Test (org.junit.Test)30 IOException (java.io.IOException)13 ConsumerCancelledException (com.rabbitmq.client.ConsumerCancelledException)12 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)12 Channel (com.rabbitmq.client.Channel)11 Connection (com.rabbitmq.client.Connection)9 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)9 Delivery (com.rabbitmq.client.QueueingConsumer.Delivery)9 AMQP (com.rabbitmq.client.AMQP)2 ArrayList (java.util.ArrayList)2 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)1 Queue (com.rabbitmq.client.AMQP.Queue)1 Envelope (com.rabbitmq.client.Envelope)1 GetResponse (com.rabbitmq.client.GetResponse)1 List (java.util.List)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RabbitMQMessage (org.apache.axis2.transport.rabbitmq.RabbitMQMessage)1