use of com.rabbitmq.client.QueueingConsumer in project rabbitmq-java-client by rabbitmq.
the class QosScaling method run.
public long run() throws IOException, TimeoutException {
connectionFactory.setHost(params.host);
connectionFactory.setPort(params.port);
connection = connectionFactory.newConnection();
channel = connection.createChannel();
channel.basicQos(1);
QueueingConsumer consumer = new QueueingConsumer(channel);
try {
publish(consume(consumer));
return drain(consumer);
} finally {
connection.abort();
}
}
use of com.rabbitmq.client.QueueingConsumer in project wso2-axis2-transports by wso2.
the class RabbitMQRPCMessageSender method processResponse.
private RabbitMQMessage processResponse(String correlationID) throws IOException {
QueueingConsumer consumer = dualChannel.getConsumer();
QueueingConsumer.Delivery delivery = null;
RabbitMQMessage message = new RabbitMQMessage();
String replyToQueue = dualChannel.getReplyToQueue();
String queueAutoDeclareStr = epProperties.get(RabbitMQConstants.QUEUE_AUTODECLARE);
boolean queueAutoDeclare = true;
if (!StringUtils.isEmpty(queueAutoDeclareStr)) {
queueAutoDeclare = Boolean.parseBoolean(queueAutoDeclareStr);
}
if (queueAutoDeclare && !RabbitMQUtils.isQueueAvailable(dualChannel.getChannel(), replyToQueue)) {
log.info("Reply-to queue : " + replyToQueue + " not available, hence creating a new one");
RabbitMQUtils.declareQueue(dualChannel, replyToQueue, epProperties);
}
int timeout = RabbitMQConstants.DEFAULT_REPLY_TO_TIMEOUT;
String timeoutStr = epProperties.get(RabbitMQConstants.REPLY_TO_TIMEOUT);
if (!StringUtils.isEmpty(timeoutStr)) {
try {
timeout = Integer.parseInt(timeoutStr);
} catch (NumberFormatException e) {
log.warn("Number format error in reading replyto timeout value. Proceeding with default value (30000ms)", e);
}
}
try {
if (log.isDebugEnabled()) {
log.debug("Waiting for delivery from reply to queue " + replyToQueue + " corr id : " + correlationID);
}
delivery = consumer.nextDelivery(timeout);
if (delivery != null) {
if (!StringUtils.isEmpty(delivery.getProperties().getCorrelationId())) {
if (delivery.getProperties().getCorrelationId().equals(correlationID)) {
if (log.isDebugEnabled()) {
log.debug("Found matching response with correlation ID : " + correlationID + ".");
}
} else {
log.error("Response not queued in " + replyToQueue + " for correlation ID : " + correlationID);
return null;
}
}
} else {
log.error("Response not queued in " + replyToQueue);
}
} catch (ShutdownSignalException e) {
log.error("Error receiving message from RabbitMQ broker " + e.getLocalizedMessage());
} catch (InterruptedException e) {
log.error("Error receiving message from RabbitMQ broker " + e.getLocalizedMessage());
} catch (ConsumerCancelledException e) {
log.error("Error receiving message from RabbitMQ broker" + e.getLocalizedMessage());
}
if (delivery != null) {
log.debug("Processing response from reply-to queue");
AMQP.BasicProperties properties = delivery.getProperties();
Map<String, Object> headers = properties.getHeaders();
message.setBody(delivery.getBody());
message.setDeliveryTag(delivery.getEnvelope().getDeliveryTag());
message.setReplyTo(properties.getReplyTo());
message.setMessageId(properties.getMessageId());
// get content type from message
String contentType = properties.getContentType();
if (contentType == null) {
// if not get content type from transport parameter
contentType = epProperties.get(RabbitMQConstants.REPLY_TO_CONTENT_TYPE);
if (contentType == null) {
// if none is given, set to default content type
log.warn("Setting default content type " + RabbitMQConstants.DEFAULT_CONTENT_TYPE);
contentType = RabbitMQConstants.DEFAULT_CONTENT_TYPE;
}
}
message.setContentType(contentType);
message.setContentEncoding(properties.getContentEncoding());
message.setCorrelationId(properties.getCorrelationId());
if (headers != null) {
message.setHeaders(headers);
if (headers.get(RabbitMQConstants.SOAP_ACTION) != null) {
message.setSoapAction(headers.get(RabbitMQConstants.SOAP_ACTION).toString());
}
}
}
return message;
}
use of com.rabbitmq.client.QueueingConsumer in project microservices by pwillhan.
the class FailoverReceiver 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 WSO2Sender 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.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