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();
}
}
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);
}
}
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;
}
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);
}
}
}
}
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;
}
Aggregations