use of org.apache.axis2.transport.rabbitmq.RabbitMQMessage in project wso2-axis2-transports by wso2.
the class RabbitMQMessageSender method publish.
/**
* Perform the creation of exchange/queue and the Outputstream
*
* @param message the RabbitMQ AMQP message
* @param msgContext the Axis2 MessageContext
*/
public void publish(RabbitMQMessage message, MessageContext msgContext) throws AxisRabbitMQException, IOException {
String exchangeName = null;
AMQP.BasicProperties basicProperties = null;
byte[] messageBody = null;
if (rmqChannel.isOpen()) {
String queueName = properties.get(RabbitMQConstants.QUEUE_NAME);
String routeKey = properties.get(RabbitMQConstants.QUEUE_ROUTING_KEY);
exchangeName = properties.get(RabbitMQConstants.EXCHANGE_NAME);
String exchangeType = properties.get(RabbitMQConstants.EXCHANGE_TYPE);
String replyTo = properties.get(RabbitMQConstants.REPLY_TO_NAME);
String correlationID = properties.get(RabbitMQConstants.CORRELATION_ID);
String queueAutoDeclareStr = properties.get(RabbitMQConstants.QUEUE_AUTODECLARE);
String exchangeAutoDeclareStr = properties.get(RabbitMQConstants.EXCHANGE_AUTODECLARE);
boolean queueAutoDeclare = true;
boolean exchangeAutoDeclare = true;
if (!StringUtils.isEmpty(queueAutoDeclareStr)) {
queueAutoDeclare = Boolean.parseBoolean(queueAutoDeclareStr);
}
if (!StringUtils.isEmpty(exchangeAutoDeclareStr)) {
exchangeAutoDeclare = Boolean.parseBoolean(exchangeAutoDeclareStr);
}
message.setReplyTo(replyTo);
if (StringUtils.isEmpty(correlationID)) {
// if reply-to is enabled a correlationID must be available. If not specified, use messageID
correlationID = message.getMessageId();
}
if (!StringUtils.isEmpty(correlationID)) {
message.setCorrelationId(correlationID);
}
if (queueName == null || queueName.equals("")) {
log.debug("No queue name is specified");
}
if (routeKey == null && !"x-consistent-hash".equals(exchangeType)) {
if (queueName == null || queueName.equals("")) {
log.debug("Routing key is not specified");
} else {
log.debug("Routing key is not specified. Using queue name as the routing key.");
routeKey = queueName;
}
}
// read publish properties corr id and route key from message context
Object prRouteKey = msgContext.getProperty(RabbitMQConstants.QUEUE_ROUTING_KEY);
Object prCorrId = msgContext.getProperty(RabbitMQConstants.CORRELATION_ID).toString();
if (prRouteKey != null) {
routeKey = prRouteKey.toString();
log.debug("Specifying routing key from axis2 properties");
}
if (prCorrId != null) {
message.setCorrelationId(prCorrId.toString());
log.debug("Specifying correlation id from axis2 properties");
}
// Declaring the queue
if (queueAutoDeclare && queueName != null && !queueName.equals("")) {
RabbitMQUtils.declareQueue(rmqChannel, queueName, properties);
}
// Declaring the exchange
if (exchangeAutoDeclare && exchangeName != null && !exchangeName.equals("")) {
RabbitMQUtils.declareExchange(rmqChannel, exchangeName, properties);
if (queueName != null && !"x-consistent-hash".equals(exchangeType)) {
// Create bind between the queue and exchange with the routeKey
try {
rmqChannel.getChannel().queueBind(queueName, exchangeName, routeKey);
} catch (IOException e) {
handleException("Error occurred while creating the bind between the queue: " + queueName + " & exchange: " + exchangeName + " with route-key " + routeKey, e);
}
}
}
AMQP.BasicProperties.Builder builder = buildBasicProperties(message);
String deliveryModeString = properties.get(RabbitMQConstants.QUEUE_DELIVERY_MODE);
int deliveryMode = RabbitMQConstants.DEFAULT_DELIVERY_MODE;
if (deliveryModeString != null) {
deliveryMode = Integer.parseInt(deliveryModeString);
}
builder.deliveryMode(deliveryMode);
if (!StringUtils.isEmpty(replyTo)) {
builder.replyTo(replyTo);
}
basicProperties = builder.build();
OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
MessageFormatter messageFormatter = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);
} catch (AxisFault axisFault) {
throw new AxisRabbitMQException("Unable to get the message formatter to use", axisFault);
}
// given. Queue/exchange creation, bindings should be done at the broker
try {
// x-consistent-hash type
if (exchangeType != null && exchangeType.equals("x-consistent-hash")) {
routeKey = UUID.randomUUID().toString();
}
} catch (UnsupportedCharsetException ex) {
handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
}
try {
messageFormatter.writeTo(msgContext, format, out, false);
messageBody = out.toByteArray();
} catch (IOException e) {
handleException("IO Error while creating BytesMessage", e);
} finally {
if (out != null) {
out.close();
}
}
try {
/**
* Check for parameter on confirmed delivery in RabbitMq to ensure reliable delivery.
* Can be enabled by setting the parameter "rabbitmq.confirm.delivery" to "true" in broker URL.
*/
boolean isConfirmedDeliveryEnabled = Boolean.parseBoolean(properties.get(RabbitMQConstants.CONFIRMED_DELIVERY));
if (isConfirmedDeliveryEnabled) {
rmqChannel.getChannel().confirmSelect();
}
if (exchangeName != null && exchangeName != "") {
rmqChannel.getChannel().basicPublish(exchangeName, routeKey, basicProperties, messageBody);
} else {
rmqChannel.getChannel().basicPublish("", routeKey, basicProperties, messageBody);
}
if (isConfirmedDeliveryEnabled) {
rmqChannel.getChannel().waitForConfirmsOrDie();
}
} catch (IOException e) {
handleException("Error while publishing the message", e);
} catch (InterruptedException e) {
handleException("InterruptedException while waiting for AMQP message confirm :", e);
}
} else {
handleException("Channel cannot be created");
}
}
use of org.apache.axis2.transport.rabbitmq.RabbitMQMessage in project wso2-axis2-transports by wso2.
the class RabbitMQMessageReceiver method processThroughAxisEngine.
/**
* Process the new message through Axis2
*
* @param message the RabbitMQMessage
* @return true if the caller should commit
* @throws AxisFault on Axis2 errors
*/
private boolean processThroughAxisEngine(RabbitMQMessage message) throws AxisFault {
MessageContext msgContext = endpoint.createMessageContext();
String amqpCorrelationID = message.getCorrelationId();
if (amqpCorrelationID != null && amqpCorrelationID.length() > 0) {
msgContext.setProperty(RabbitMQConstants.CORRELATION_ID, amqpCorrelationID);
} else {
msgContext.setProperty(RabbitMQConstants.CORRELATION_ID, message.getMessageId());
}
String contentType = message.getContentType();
if (contentType == null) {
log.warn("Unable to determine content type for message " + msgContext.getMessageID() + " setting to text/plain");
contentType = RabbitMQConstants.DEFAULT_CONTENT_TYPE;
message.setContentType(contentType);
}
msgContext.setProperty(RabbitMQConstants.CONTENT_TYPE, contentType);
if (message.getContentEncoding() != null) {
msgContext.setProperty(RabbitMQConstants.CONTENT_ENCODING, message.getContentEncoding());
}
String soapAction = message.getSoapAction();
if (soapAction == null) {
soapAction = RabbitMQUtils.getSOAPActionHeader(message);
}
String replyTo = message.getReplyTo();
if (replyTo != null) {
msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, new RabbitMQOutTransportInfo(rabbitMQConnectionFactory, replyTo, contentType));
}
RabbitMQUtils.setSOAPEnvelope(message, msgContext, contentType);
try {
listener.handleIncomingMessage(msgContext, RabbitMQUtils.getTransportHeaders(message), soapAction, contentType);
} catch (AxisFault axisFault) {
log.error("Error when trying to read incoming message ...", axisFault);
return false;
}
return true;
}
use of org.apache.axis2.transport.rabbitmq.RabbitMQMessage in project wso2-axis2-transports by wso2.
the class RabbitMQRPCMessageSender method publish.
/**
* Perform the creation of exchange/queue and the Outputstream
*
* @param message the RabbitMQ AMQP message
* @param msgContext the Axis2 MessageContext
*/
private void publish(RabbitMQMessage message, MessageContext msgContext) throws AxisRabbitMQException, IOException {
String exchangeName = null;
AMQP.BasicProperties basicProperties = null;
byte[] messageBody = null;
if (dualChannel.isOpen()) {
String queueName = epProperties.get(RabbitMQConstants.QUEUE_NAME);
String routeKey = epProperties.get(RabbitMQConstants.QUEUE_ROUTING_KEY);
exchangeName = epProperties.get(RabbitMQConstants.EXCHANGE_NAME);
String exchangeType = epProperties.get(RabbitMQConstants.EXCHANGE_TYPE);
String correlationID = epProperties.get(RabbitMQConstants.CORRELATION_ID);
String replyTo = dualChannel.getReplyToQueue();
String queueAutoDeclareStr = epProperties.get(RabbitMQConstants.QUEUE_AUTODECLARE);
String exchangeAutoDeclareStr = epProperties.get(RabbitMQConstants.EXCHANGE_AUTODECLARE);
boolean queueAutoDeclare = true;
boolean exchangeAutoDeclare = true;
if (!StringUtils.isEmpty(queueAutoDeclareStr)) {
queueAutoDeclare = Boolean.parseBoolean(queueAutoDeclareStr);
}
if (!StringUtils.isEmpty(exchangeAutoDeclareStr)) {
exchangeAutoDeclare = Boolean.parseBoolean(exchangeAutoDeclareStr);
}
message.setReplyTo(replyTo);
if ((!StringUtils.isEmpty(replyTo)) && (StringUtils.isEmpty(correlationID))) {
// if reply-to is enabled a correlationID must be available. If not specified, use messageID
correlationID = message.getMessageId();
}
if (!StringUtils.isEmpty(correlationID)) {
message.setCorrelationId(correlationID);
}
if (queueName == null || queueName.equals("")) {
log.info("No queue name is specified");
}
if (routeKey == null && !"x-consistent-hash".equals(exchangeType)) {
if (queueName == null || queueName.equals("")) {
log.info("Routing key is not specified");
} else {
log.info("Routing key is not specified. Using queue name as the routing key.");
routeKey = queueName;
}
}
// Declaring the queue
if (queueAutoDeclare && queueName != null && !queueName.equals("")) {
// get channel with dualChannel.getChannel() since it will create a new channel if channel is closed
RabbitMQUtils.declareQueue(dualChannel, queueName, epProperties);
}
// Declaring the exchange
if (exchangeAutoDeclare && exchangeName != null && !exchangeName.equals("")) {
RabbitMQUtils.declareExchange(dualChannel, exchangeName, epProperties);
if (queueName != null && !"x-consistent-hash".equals(exchangeType)) {
// Create bind between the queue and exchange with the routeKey
try {
dualChannel.getChannel().queueBind(queueName, exchangeName, routeKey);
} catch (IOException e) {
handleException("Error occurred while creating the bind between the queue: " + queueName + " & exchange: " + exchangeName + " with route-key " + routeKey, e);
}
}
}
// build basic properties from message
AMQP.BasicProperties.Builder builder = buildBasicProperties(message);
String deliveryModeString = epProperties.get(RabbitMQConstants.QUEUE_DELIVERY_MODE);
int deliveryMode = RabbitMQConstants.DEFAULT_DELIVERY_MODE;
if (deliveryModeString != null) {
deliveryMode = Integer.parseInt(deliveryModeString);
}
// TODO : override properties from message with ones from transport properties
// set builder properties from transport properties (overrides current properties)
builder.deliveryMode(deliveryMode);
builder.replyTo(replyTo);
basicProperties = builder.build();
OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
MessageFormatter messageFormatter = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);
} catch (AxisFault axisFault) {
throw new AxisRabbitMQException("Unable to get the message formatter to use", axisFault);
}
// given. Queue/exchange creation, bindings should be done at the broker
try {
// x-consistent-hash type
if (exchangeType != null && exchangeType.equals("x-consistent-hash")) {
routeKey = UUID.randomUUID().toString();
}
} catch (UnsupportedCharsetException ex) {
handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
}
try {
messageFormatter.writeTo(msgContext, format, out, false);
messageBody = out.toByteArray();
} catch (IOException e) {
handleException("IO Error while creating BytesMessage", e);
} finally {
if (out != null) {
out.close();
}
}
try {
if (exchangeName != null && exchangeName != "") {
if (log.isDebugEnabled()) {
log.debug("Publishing message to exchange " + exchangeName + " with route key " + routeKey);
}
dualChannel.getChannel().basicPublish(exchangeName, routeKey, basicProperties, messageBody);
} else {
if (log.isDebugEnabled()) {
log.debug("Publishing message with route key " + routeKey);
}
dualChannel.getChannel().basicPublish("", routeKey, basicProperties, messageBody);
}
} catch (IOException e) {
handleException("Error while publishing the message", e);
}
} else {
handleException("Channel cannot be created");
}
}
use of org.apache.axis2.transport.rabbitmq.RabbitMQMessage 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 org.apache.axis2.transport.rabbitmq.RabbitMQMessage in project wso2-axis2-transports by wso2.
the class RabbitMQRPCMessageSender method send.
public RabbitMQMessage send(RabbitMQMessage message, MessageContext msgContext) throws AxisRabbitMQException, IOException {
publish(message, msgContext);
RabbitMQMessage responseMessage = processResponse(message.getCorrelationId());
// release the dual channel to the pool
try {
connectionFactory.pushRPCChannel(dualChannel);
} catch (InterruptedException e) {
handleException(e.getMessage());
}
return responseMessage;
}
Aggregations