Search in sources :

Example 26 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project microservices by pwillhan.

the class PublishSubscribeReceiver method receive.

public String receive(String queue) {
    if (channel == null) {
        initialize();
    }
    String message = null;
    try {
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        channel.queueDeclare(queue, false, false, false, null);
        channel.queueBind(queue, EXCHANGE_NAME, "");
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(queue, true, consumer);
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        message = new String(delivery.getBody());
        LOGGER.info("Message received: " + message);
        return message;
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (ShutdownSignalException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (ConsumerCancelledException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return message;
}
Also used : ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) IOException(java.io.IOException)

Example 27 with QueueingConsumer

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

the class RequeueOnClose method publishLotsAndGet.

private void publishLotsAndGet() throws IOException, InterruptedException, ShutdownSignalException, TimeoutException {
    openConnection();
    open();
    channel.queueDeclare(Q, false, false, false, null);
    channel.queueDelete(Q);
    channel.queueDeclare(Q, false, false, false, null);
    for (int i = 0; i < MESSAGE_COUNT; i++) {
        channel.basicPublish("", Q, null, "in flight message".getBytes());
    }
    QueueingConsumer c = new QueueingConsumer(channel);
    channel.basicConsume(Q, c);
    c.nextDelivery();
    close();
    open();
    for (int i = 0; i < MESSAGE_COUNT; i++) {
        assertNotNull("only got " + i + " out of " + MESSAGE_COUNT + " messages", channel.basicGet(Q, true));
    }
    assertNull("got more messages than " + MESSAGE_COUNT + " expected", channel.basicGet(Q, true));
    channel.queueDelete(Q);
    close();
    closeConnection();
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer)

Example 28 with QueueingConsumer

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

the class RequeueOnClose method requeueingConsumer.

/**
 * Test we requeue unacknowledged message (using consumer)
 * @throws Exception untested
 */
@Test
public void requeueingConsumer() throws Exception {
    openConnection();
    open();
    injectMessage();
    QueueingConsumer c = new QueueingConsumer(channel);
    channel.basicConsume(Q, c);
    c.nextDelivery();
    close();
    open();
    assertNotNull(getMessage());
    close();
    closeConnection();
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Test(org.junit.Test)

Example 29 with QueueingConsumer

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

the class TTLHandling method expiryWithReQueueAfterConsume.

/*
    * Test expiry of re-queued messages after being consumed instantly
    */
@Test
public void expiryWithReQueueAfterConsume() throws Exception {
    declareAndBindQueue(100);
    QueueingConsumer c = new QueueingConsumer(channel);
    channel.basicConsume(TTL_QUEUE_NAME, c);
    publish(MSG[0]);
    assertNotNull(c.nextDelivery(100));
    closeChannel();
    Thread.sleep(150);
    openChannel();
    assertNull("Re-queued message not expired", get());
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Test(org.junit.Test)

Example 30 with QueueingConsumer

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

the class TTLHandling method zeroTTLDelivery.

@Test
public void zeroTTLDelivery() throws Exception {
    declareAndBindQueue(0);
    // when there is no consumer, message should expire
    publish(MSG[0]);
    assertNull(get());
    // when there is a consumer, message should be delivered
    QueueingConsumer c = new QueueingConsumer(channel);
    channel.basicConsume(TTL_QUEUE_NAME, c);
    publish(MSG[0]);
    QueueingConsumer.Delivery d = c.nextDelivery(100);
    assertNotNull(d);
    // requeued messages should expire
    channel.basicReject(d.getEnvelope().getDeliveryTag(), true);
    assertNull(c.nextDelivery(100));
}
Also used : QueueingConsumer(com.rabbitmq.client.QueueingConsumer) Test(org.junit.Test)

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