use of org.apache.axis2.transport.msmq.util.Message 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.msmq.util.Message 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.msmq.util.Message in project wso2-axis2-transports by wso2.
the class SMSManager method dispatchToAxis2.
/**
* Dispatch the SMS message to Axis2 Engine
* @param sms
*/
public void dispatchToAxis2(SMSMessage sms) {
try {
MessageContext msgctx = messageBuilder.buildMessaage(sms, configurationContext);
msgctx.setReplyTo(new EndpointReference("sms://" + sms.getSender() + "/"));
AxisEngine.receive(msgctx);
} catch (InvalidMessageFormatException e) {
log.debug("Invalid message format " + e);
} catch (AxisFault axisFault) {
log.debug(axisFault);
} catch (Throwable e) {
log.debug("Unknown Exception ", e);
}
}
use of org.apache.axis2.transport.msmq.util.Message in project wso2-axis2-transports by wso2.
the class GSMDispatcher method run.
public void run() {
while (keepPolling) {
List<InboundMessage> arrayList = new ArrayList<InboundMessage>();
try {
service.readMessages(arrayList, InboundMessage.MessageClasses.UNREAD);
for (InboundMessage msg : arrayList) {
SMSMessage sms = null;
synchronized (this) {
sms = new SMSMessage(msg.getOriginator(), null, msg.getText(), SMSMessage.IN_MESSAGE);
}
smsManager.dispatchToAxis2(sms);
// delete the message form inbox
service.deleteMessage(msg);
}
} catch (Exception ex) {
log.error("Error Occured while reading messages", ex);
}
try {
Thread.sleep(pollInterval);
} catch (InterruptedException e) {
}
}
}
use of org.apache.axis2.transport.msmq.util.Message in project wso2-axis2-transports by wso2.
the class SMPPImplManager method sendSMS.
public void sendSMS(SMSMessage sm) throws AxisFault {
TypeOfNumber sourceTon = TypeOfNumber.UNKNOWN;
NumberingPlanIndicator sourceNpi = NumberingPlanIndicator.UNKNOWN;
TypeOfNumber destTon = TypeOfNumber.UNKNOWN;
NumberingPlanIndicator destNpi = NumberingPlanIndicator.UNKNOWN;
try {
if (outSession == null) {
outSession = new SMPPSession();
outSession.setEnquireLinkTimer(smppTransportOutDetails.getEnquireLinkTimer());
outSession.setTransactionTimer(smppTransportOutDetails.getTransactionTimer());
outSession.connectAndBind(smppTransportOutDetails.getHost(), smppTransportOutDetails.getPort(), new BindParameter(BindType.BIND_TX, smppTransportOutDetails.getSystemId(), smppTransportOutDetails.getPassword(), smppTransportOutDetails.getSystemType(), TypeOfNumber.UNKNOWN, NumberingPlanIndicator.UNKNOWN, null));
log.debug("Conected and bind to " + smppTransportOutDetails.getHost());
}
boolean invert = true;
if ("false".equals(sm.getProperties().get(SMSTransportConstents.INVERT_SOURCE_AND_DESTINATION))) {
invert = false;
}
if (invert) {
if (sm.getProperties().get(DESTINATION_ADDRESS_NPI) != null) {
sourceNpi = NumberingPlanIndicator.valueOf((String) sm.getProperties().get(DESTINATION_ADDRESS_NPI));
}
if (sm.getProperties().get(DESTINATION_ADDRESS_TON) != null) {
sourceTon = TypeOfNumber.valueOf((String) sm.getProperties().get(DESTINATION_ADDRESS_TON));
}
if (sm.getProperties().get(SOURCE_ADDRESS_NPI) != null) {
destNpi = NumberingPlanIndicator.valueOf((String) sm.getProperties().get(SOURCE_ADDRESS_NPI));
}
if (sm.getProperties().get(SOURCE_ADDRESS_TON) != null) {
destTon = TypeOfNumber.valueOf((String) sm.getProperties().get(SOURCE_ADDRESS_TON));
}
} else {
if (sm.getProperties().get(DESTINATION_ADDRESS_NPI) != null) {
destNpi = NumberingPlanIndicator.valueOf((String) sm.getProperties().get(DESTINATION_ADDRESS_NPI));
}
if (sm.getProperties().get(DESTINATION_ADDRESS_TON) != null) {
destTon = TypeOfNumber.valueOf((String) sm.getProperties().get(DESTINATION_ADDRESS_TON));
}
if (sm.getProperties().get(SOURCE_ADDRESS_NPI) != null) {
sourceNpi = NumberingPlanIndicator.valueOf((String) sm.getProperties().get(SOURCE_ADDRESS_NPI));
}
if (sm.getProperties().get(SOURCE_ADDRESS_TON) != null) {
sourceTon = TypeOfNumber.valueOf((String) sm.getProperties().get(SOURCE_ADDRESS_TON));
}
}
String messageId = outSession.submitShortMessage("CMT", sourceTon, sourceNpi, sm.getSender(), destTon, destNpi, sm.getReceiver(), new ESMClass(), (byte) 0, (byte) 1, timeFormatter.format(new Date()), null, new RegisteredDelivery(SMSCDeliveryReceipt.DEFAULT), (byte) 0, new GeneralDataCoding(false, false, MessageClass.CLASS1, Alphabet.ALPHA_DEFAULT), (byte) 0, sm.getContent().getBytes());
log.debug("Message Submited with id" + messageId);
} catch (Exception e) {
this.outSession = null;
throw new AxisFault(e.getMessage(), e);
}
}
Aggregations