Search in sources :

Example 21 with QueueingConsumer

use of com.rabbitmq.client.QueueingConsumer in project voltdb by VoltDB.

the class ExportRabbitMQVerifier method run.

public void run() throws IOException, InterruptedException {
    final Connection connection = m_connFactory.newConnection();
    final Channel channel = connection.createChannel();
    try {
        channel.exchangeDeclare(m_exchangeName, "topic", true);
        String dataQueue = channel.queueDeclare().getQueue();
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE2.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_REPLICATED_TABLE.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE_FOO.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE2_FOO.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_REPLICATED_TABLE_FOO.#");
        String doneQueue = channel.queueDeclare().getQueue();
        channel.queueBind(doneQueue, m_exchangeName, "EXPORT_DONE_TABLE.#");
        channel.queueBind(doneQueue, m_exchangeName, "EXPORT_DONE_TABLE_FOO.#");
        // Setup callback for data stream
        channel.basicConsume(dataQueue, false, createConsumer(channel));
        // Setup callback for the done message
        QueueingConsumer doneConsumer = new QueueingConsumer(channel);
        channel.basicConsume(doneQueue, true, doneConsumer);
        // Wait until the done message arrives, then verify count
        final QueueingConsumer.Delivery doneMsg = doneConsumer.nextDelivery();
        final long expectedRows = Long.parseLong(ExportOnServerVerifier.RoughCSVTokenizer.tokenize(new String(doneMsg.getBody(), Charsets.UTF_8))[6]);
        while (expectedRows > m_verifiedRows) {
            Thread.sleep(1000);
            System.err.println("Expected " + expectedRows + " " + m_verifiedRows);
        }
    } finally {
        tearDown(channel);
        channel.close();
        connection.close();
    }
}
Also used : Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) QueueingConsumer(com.rabbitmq.client.QueueingConsumer)

Example 22 with QueueingConsumer

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

the class RequestReceiver method receive.

public void receive() {
    if (channel == null) {
        initialize();
    }
    String message = null;
    try {
        channel.queueDeclare(REQUEST_QUEUE, false, false, false, null);
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(REQUEST_QUEUE, true, consumer);
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        message = new String(delivery.getBody());
        LOGGER.info("Request received: " + message);
        // do something with the request message ...
        BasicProperties properties = delivery.getProperties();
        if (properties != null) {
            AMQP.BasicProperties amqpProps = new AMQP.BasicProperties();
            amqpProps = amqpProps.builder().correlationId(String.valueOf(properties.getCorrelationId())).build();
            channel.basicPublish(DEFAULT_QUEUE, properties.getReplyTo(), amqpProps, "Response message.".getBytes());
        } else {
            LOGGER.warn("Cannot determine response destination for 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);
    }
}
Also used : BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) AMQP(com.rabbitmq.client.AMQP) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) IOException(java.io.IOException)

Example 23 with QueueingConsumer

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

the class ClusterReceiver method receive.

public String receive(Address... hosts) {
    if (channel == null) {
        initialize(hosts);
    }
    String message = null;
    try {
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(QUEUE_NAME, 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 24 with QueueingConsumer

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

the class TransactionalReceiver method receive.

public void receive() {
    Connection connection = null;
    Channel channel = null;
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        connection = factory.newConnection();
        channel = connection.createChannel();
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(REQUEST_QUEUE, false, consumer);
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        LOGGER.info("Request received: " + message);
        channel.txSelect();
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        channel.txCommit();
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        if (channel != null) {
            try {
                channel.txRollback();
            } catch (IOException re) {
                LOGGER.error("Rollback failed: " + re.getMessage(), re);
            }
        }
    } catch (ShutdownSignalException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (ConsumerCancelledException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                LOGGER.warn("Failed to close connection: " + e.getMessage(), e);
            }
        }
    }
}
Also used : ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) IOException(java.io.IOException)

Example 25 with QueueingConsumer

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

the class Sender method waitForResponse.

public String waitForResponse(final String correlationId) {
    QueueingConsumer consumer = new QueueingConsumer(channel);
    String result = null;
    try {
        channel.basicConsume(RESPONSE_QUEUE, true, consumer);
        QueueingConsumer.Delivery delivery = consumer.nextDelivery(3000);
        String message = new String(delivery.getBody());
        if (delivery.getProperties() != null) {
            String msgCorrelationId = delivery.getProperties().getCorrelationId();
            if (!correlationId.equals(msgCorrelationId)) {
                LOGGER.warn("Received response of another request.");
            } else {
                result = message;
            }
        }
        LOGGER.info("Message received: " + 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 result;
}
Also used : ConsumerCancelledException(com.rabbitmq.client.ConsumerCancelledException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) 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