Search in sources :

Example 1 with Delivery

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

the class QosTests method setLimitAfterConsume.

@Test
public void setLimitAfterConsume() throws IOException {
    QueueingConsumer c = new QueueingConsumer(channel);
    declareBindConsume(c);
    channel.basicQos(1, true);
    fill(3);
    // We actually only guarantee that the limit takes effect
    // *eventually*, so this can in fact fail. It's pretty unlikely
    // though.
    List<Delivery> d = drain(c, 1);
    ack(d, true);
    drain(c, 1);
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Delivery(com.rabbitmq.client.QueueingConsumer.Delivery) Test(org.junit.Test)

Example 2 with Delivery

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

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

the class RabbitMQFirehoseFactory method connect.

@Override
public Firehose connect(final ByteBufferInputRowParser firehoseParser) throws IOException {
    ConnectionOptions lyraOptions = new ConnectionOptions(this.connectionFactory);
    Config lyraConfig = new Config().withRecoveryPolicy(new RetryPolicy().withMaxRetries(config.getMaxRetries()).withRetryInterval(Duration.seconds(config.getRetryIntervalSeconds())).withMaxDuration(Duration.seconds(config.getMaxDurationSeconds())));
    String queue = config.getQueue();
    String exchange = config.getExchange();
    String routingKey = config.getRoutingKey();
    boolean durable = config.isDurable();
    boolean exclusive = config.isExclusive();
    boolean autoDelete = config.isAutoDelete();
    final Connection connection = Connections.create(lyraOptions, lyraConfig);
    connection.addShutdownListener(new ShutdownListener() {

        @Override
        public void shutdownCompleted(ShutdownSignalException cause) {
            log.warn(cause, "Connection closed!");
        }
    });
    final Channel channel = connection.createChannel();
    channel.queueDeclare(queue, durable, exclusive, autoDelete, null);
    channel.queueBind(queue, exchange, routingKey);
    channel.addShutdownListener(new ShutdownListener() {

        @Override
        public void shutdownCompleted(ShutdownSignalException cause) {
            log.warn(cause, "Channel closed!");
        }
    });
    // We create a QueueingConsumer that will not auto-acknowledge messages since that
    // happens on commit().
    final QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);
    return new Firehose() {

        /**
       * Storing the latest delivery as a member variable should be safe since this will only be run
       * by a single thread.
       */
        private Delivery delivery;

        /**
       * Store the latest delivery tag to be able to commit (acknowledge) the message delivery up to
       * and including this tag. See commit() for more detail.
       */
        private long lastDeliveryTag;

        @Override
        public boolean hasMore() {
            delivery = null;
            try {
                // Wait for the next delivery. This will block until something is available.
                delivery = consumer.nextDelivery();
                if (delivery != null) {
                    lastDeliveryTag = delivery.getEnvelope().getDeliveryTag();
                    // If delivery is non-null, we report that there is something more to process.
                    return true;
                }
            } catch (InterruptedException e) {
                // A little unclear on how we should handle this.
                // At any rate, we're in an unknown state now so let's log something and return false.
                log.wtf(e, "Got interrupted while waiting for next delivery. Doubt this should ever happen.");
            }
            // nothing more to process.
            return false;
        }

        @Override
        public InputRow nextRow() {
            if (delivery == null) {
                //Just making sure.
                log.wtf("I have nothing in delivery. Method hasMore() should have returned false.");
                return null;
            }
            return firehoseParser.parse(ByteBuffer.wrap(delivery.getBody()));
        }

        @Override
        public Runnable commit() {
            // acknowledge values up to and including that value.
            return new Runnable() {

                // Store (copy) the last delivery tag to "become" thread safe.
                final long deliveryTag = lastDeliveryTag;

                @Override
                public void run() {
                    try {
                        log.info("Acknowledging delivery of messages up to tag: " + deliveryTag);
                        // Acknowledge all messages up to and including the stored delivery tag.
                        channel.basicAck(deliveryTag, true);
                    } catch (IOException e) {
                        log.error(e, "Unable to acknowledge message reception to message queue.");
                    }
                }
            };
        }

        @Override
        public void close() throws IOException {
            log.info("Closing connection to RabbitMQ");
            channel.close();
            connection.close();
        }
    };
}
Also used : Config(net.jodah.lyra.config.Config) Firehose(io.druid.data.input.Firehose) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) IOException(java.io.IOException) ShutdownListener(com.rabbitmq.client.ShutdownListener) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) ConnectionOptions(net.jodah.lyra.ConnectionOptions) Delivery(com.rabbitmq.client.QueueingConsumer.Delivery) RetryPolicy(net.jodah.lyra.retry.RetryPolicy)

Example 4 with Delivery

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

the class ExchangeExchangeBindings method consumeNoDuplicates.

protected void consumeNoDuplicates(QueueingConsumer consumer) throws ShutdownSignalException, InterruptedException {
    assertNotNull(consumer.nextDelivery(TIMEOUT));
    Delivery markerDelivery = consumer.nextDelivery(TIMEOUT);
    assertEquals(new String(MARKER), new String(markerDelivery.getBody()));
}
Also used : Delivery(com.rabbitmq.client.QueueingConsumer.Delivery)

Example 5 with Delivery

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

the class PerConsumerPrefetch method testPrefetch.

private void testPrefetch(Closure closure) throws IOException {
    QueueingConsumer c = new QueueingConsumer(channel);
    publish(q, 15);
    consume(c, 5, false);
    List<Delivery> deliveries = drain(c, 5);
    ack(channel.basicGet(q, false), false);
    drain(c, 0);
    closure.makeMore(deliveries);
    drain(c, 5);
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Delivery(com.rabbitmq.client.QueueingConsumer.Delivery)

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