Search in sources :

Example 6 with Delivery

use of com.rabbitmq.client.QueueingConsumer.Delivery 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 7 with Delivery

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

the class QosTests method drain.

/**
 * receive n messages - check that we receive no fewer and cannot
 * receive more
 */
public static List<Delivery> drain(QueueingConsumer c, int n) throws IOException {
    List<Delivery> res = new LinkedList<Delivery>();
    try {
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            Delivery d = c.nextDelivery(1000);
            assertNotNull(d);
            res.add(d);
        }
        long finish = System.currentTimeMillis();
        Thread.sleep((n == 0 ? 0 : (finish - start) / n) + 10);
        assertNull(c.nextDelivery(0));
    } catch (InterruptedException ie) {
        fail("interrupted");
    }
    return res;
}
Also used : Delivery(com.rabbitmq.client.QueueingConsumer.Delivery) LinkedList(java.util.LinkedList)

Example 8 with Delivery

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

the class QosTests method limitDecrease.

@Test
public void limitDecrease() throws IOException {
    QueueingConsumer c = new QueueingConsumer(channel);
    List<Delivery> d = configure(c, 2, 4);
    channel.basicQos(1, true);
    drain(c, 0);
    ack(d, true);
    drain(c, 1);
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Delivery(com.rabbitmq.client.QueueingConsumer.Delivery) Test(org.junit.Test)

Example 9 with Delivery

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

the class QosTests method consumerLifecycle.

@Test
public void consumerLifecycle() throws IOException {
    channel.basicQos(1, true);
    QueueingConsumer c = new QueueingConsumer(channel);
    String queue = "qosTest";
    channel.queueDeclare(queue, false, false, false, null);
    channel.queueBind(queue, "amq.fanout", "");
    fill(3);
    String tag;
    for (int i = 0; i < 2; i++) {
        tag = channel.basicConsume(queue, false, c);
        List<Delivery> d = drain(c, 1);
        channel.basicCancel(tag);
        drain(c, 0);
        ack(d, true);
        drain(c, 0);
    }
    channel.queueDelete(queue);
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Delivery(com.rabbitmq.client.QueueingConsumer.Delivery) Test(org.junit.Test)

Example 10 with Delivery

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

the class QosTests method limitingMultipleChannels.

@Test
public void limitingMultipleChannels() throws IOException {
    Channel ch1 = connection.createChannel();
    Channel ch2 = connection.createChannel();
    QueueingConsumer c1 = new QueueingConsumer(ch1);
    QueueingConsumer c2 = new QueueingConsumer(ch2);
    String q1 = declareBindConsume(ch1, c1, false);
    String q2 = declareBindConsume(ch2, c2, false);
    ch1.basicConsume(q2, false, c1);
    ch2.basicConsume(q1, false, c2);
    ch1.basicQos(1, true);
    ch2.basicQos(1, true);
    fill(5);
    List<Delivery> d1 = drain(c1, 1);
    List<Delivery> d2 = drain(c2, 1);
    ackDelivery(ch1, d1.remove(0), true);
    ackDelivery(ch2, d2.remove(0), true);
    drain(c1, 1);
    drain(c2, 1);
    ch1.abort();
    ch2.abort();
}
Also used : Channel(com.rabbitmq.client.Channel) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Delivery(com.rabbitmq.client.QueueingConsumer.Delivery) Test(org.junit.Test)

Aggregations

Delivery (com.rabbitmq.client.QueueingConsumer.Delivery)12 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)9 Test (org.junit.Test)7 Channel (com.rabbitmq.client.Channel)2 Connection (com.rabbitmq.client.Connection)1 Envelope (com.rabbitmq.client.Envelope)1 GetResponse (com.rabbitmq.client.GetResponse)1 ShutdownListener (com.rabbitmq.client.ShutdownListener)1 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)1 Firehose (io.druid.data.input.Firehose)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 ConnectionOptions (net.jodah.lyra.ConnectionOptions)1 Config (net.jodah.lyra.config.Config)1 RetryPolicy (net.jodah.lyra.retry.RetryPolicy)1