Search in sources :

Example 1 with Message

use of org.apache.axis2.transport.msmq.util.Message in project wso2-axis2-transports by wso2.

the class MqttListenerCallback method messageArrived.

public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
    // build the message and hand it over to axisEngine
    MessageContext messageContext = mqttEndpoint.createMessageContext();
    MqttUtils.invoke(mqttMessage, messageContext, contentType);
    AxisEngine.receive(messageContext);
}
Also used : MessageContext(org.apache.axis2.context.MessageContext)

Example 2 with Message

use of org.apache.axis2.transport.msmq.util.Message in project wso2-axis2-transports by wso2.

the class MqttSender method createMqttMessage.

private MqttMessage createMqttMessage(MessageContext messageContext) {
    OMOutputFormat format = BaseUtils.getOMOutputFormat(messageContext);
    MessageFormatter messageFormatter;
    try {
        messageFormatter = MessageProcessorSelector.getMessageFormatter(messageContext);
    } catch (AxisFault axisFault) {
        throw new AxisMqttException("Unable to get the message formatter to use");
    }
    OutputStream out;
    StringWriter sw = new StringWriter();
    try {
        out = new WriterOutputStream(sw, format.getCharSetEncoding());
    } catch (UnsupportedCharsetException ex) {
        throw new AxisMqttException("Unsupported encoding " + format.getCharSetEncoding(), ex);
    }
    try {
        messageFormatter.writeTo(messageContext, format, out, true);
        out.close();
    } catch (IOException e) {
        throw new AxisMqttException("IO Error while creating BytesMessage", e);
    }
    MqttMessage mqttMessage = new MqttMessage();
    mqttMessage.setPayload(sw.toString().getBytes());
    return mqttMessage;
}
Also used : AxisFault(org.apache.axis2.AxisFault) StringWriter(java.io.StringWriter) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) OutputStream(java.io.OutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) MessageFormatter(org.apache.axis2.transport.MessageFormatter) IOException(java.io.IOException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream)

Example 3 with Message

use of org.apache.axis2.transport.msmq.util.Message 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");
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) AxisRabbitMQException(org.apache.axis2.transport.rabbitmq.utils.AxisRabbitMQException) IOException(java.io.IOException) MessageFormatter(org.apache.axis2.transport.MessageFormatter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AMQP(com.rabbitmq.client.AMQP) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 4 with Message

use of org.apache.axis2.transport.msmq.util.Message in project wso2-axis2-transports by wso2.

the class AxisTestClient method send.

protected MessageContext send(ClientOptions options, AxisMessage message, QName operationQName, boolean block, String resultMessageLabel) throws Exception {
    OperationClient mepClient = serviceClient.createClient(operationQName);
    MessageContext mc = new MessageContext();
    mc.setProperty(Constants.Configuration.MESSAGE_TYPE, message.getMessageType());
    mc.setEnvelope(message.getEnvelope());
    Attachments attachments = message.getAttachments();
    if (attachments != null) {
        mc.setAttachmentMap(attachments);
        mc.setDoingSwA(true);
        mc.setProperty(Constants.Configuration.ENABLE_SWA, true);
    }
    for (AxisTestClientConfigurator configurator : configurators) {
        configurator.setupRequestMessageContext(mc);
    }
    mc.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, options.getCharset());
    mc.setServiceContext(serviceClient.getServiceContext());
    if (metrics != null) {
        mc.setProperty(BaseConstants.METRICS_COLLECTOR, metrics);
    }
    mepClient.addMessageContext(mc);
    mepClient.execute(block);
    // mepClient.complete(mc);
    return resultMessageLabel == null ? null : mepClient.getMessageContext(resultMessageLabel);
}
Also used : OperationClient(org.apache.axis2.client.OperationClient) MessageContext(org.apache.axis2.context.MessageContext) Attachments(org.apache.axiom.attachments.Attachments)

Example 5 with Message

use of org.apache.axis2.transport.msmq.util.Message in project wso2-axis2-transports by wso2.

the class RequestResponseTestClientAdapter method sendMessage.

public IncomingMessage<O> sendMessage(ClientOptions options, ContentType contentType, M message) throws Exception {
    IncomingMessage<P> response = target.sendMessage(options, encoder.getContentType(options, contentType), encoder.encode(options, message));
    ContentType responseContentType = response.getContentType();
    return new IncomingMessage<O>(responseContentType, decoder.decode(responseContentType, response.getData()));
}
Also used : ContentType(javax.mail.internet.ContentType) IncomingMessage(org.apache.axis2.transport.testkit.message.IncomingMessage)

Aggregations

AxisFault (org.apache.axis2.AxisFault)25 MessageContext (org.apache.axis2.context.MessageContext)20 IOException (java.io.IOException)9 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)8 MessageFormatter (org.apache.axis2.transport.MessageFormatter)8 ContentType (javax.mail.internet.ContentType)7 OMElement (org.apache.axiom.om.OMElement)7 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)6 IncomingMessage (org.apache.axis2.transport.testkit.message.IncomingMessage)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)5 AxisOperation (org.apache.axis2.description.AxisOperation)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OutputStream (java.io.OutputStream)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 XMPPConnection (org.jivesoftware.smack.XMPPConnection)4 XMPPException (org.jivesoftware.smack.XMPPException)4 Message (org.jivesoftware.smack.packet.Message)4 AMQP (com.rabbitmq.client.AMQP)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3