use of com.rabbitmq.client.ShutdownSignalException 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.ShutdownSignalException 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.ShutdownSignalException 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.ShutdownSignalException 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;
}
use of com.rabbitmq.client.ShutdownSignalException 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;
}
Aggregations