Example usage for javax.jms TextMessage setText

List of usage examples for javax.jms TextMessage setText

Introduction

In this page you can find the example usage for javax.jms TextMessage setText.

Prototype


void setText(String string) throws JMSException;

Source Link

Document

Sets the string containing this message's data.

Usage

From source file:gov.nih.nci.cabig.caaers.esb.client.impl.CtmsCaaersMessageConsumer.java

/**
 * This method sends the response out. It uses a producer which is connected to "ctms-caaers.outputQueue"
 * @param responseXml/*from  w  w  w.  j  a  va  2s  .c  o m*/
 * @param jmsCorelationID
 * @param messageType
 */
private void sendResponse(String responseXml, String jmsCorelationID, String messageType) {
    try {
        TextMessage message = session.createTextMessage();
        message.setJMSCorrelationID(jmsCorelationID);
        message.setStringProperty("MESSAGE_TYPE", messageType);
        message.setText(responseXml);
        producer.send(message);
    } catch (JMSException e) {
        logger.error("caught sendResponse", e);
    }
}

From source file:com.eviware.soapui.impl.wsdl.submit.transports.jms.HermesJmsRequestTransport.java

private Message createTextMessageFromAttachment(SubmitContext submitContext, Request request, Session session) {
    try {//w w  w  .  j a  v a  2s.co  m
        String content = convertStreamToString(request.getAttachments()[0].getInputStream());
        TextMessage textMessageSend = session.createTextMessage();
        String messageBody = PropertyExpander.expandProperties(submitContext, content);
        textMessageSend.setText(messageBody);
        return textMessageSend;
    } catch (Exception e) {
        SoapUI.logError(e);
    }
    return null;
}

From source file:org.apache.synapse.transport.jms.JMSSender.java

/**
 * Create a JMS Message from the given MessageContext and using the given
 * session/*w  w  w  . ja va2s.c om*/
 *
 * @param msgContext the MessageContext
 * @param session    the JMS session
 * @return a JMS message from the context and session
 * @throws JMSException on exception
 * @throws AxisFault on exception
 */
private Message createJMSMessage(MessageContext msgContext, Session session) throws JMSException, AxisFault {

    Message message = null;
    String msgType = getProperty(msgContext, JMSConstants.JMS_MESSAGE_TYPE);

    // check the first element of the SOAP body, do we have content wrapped using the
    // default wrapper elements for binary (BaseConstants.DEFAULT_BINARY_WRAPPER) or
    // text (BaseConstants.DEFAULT_TEXT_WRAPPER) ? If so, do not create SOAP messages
    // for JMS but just get the payload in its native format
    String jmsPayloadType = guessMessageType(msgContext);

    if (jmsPayloadType == null) {

        OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
        MessageFormatter messageFormatter = null;
        try {
            messageFormatter = TransportUtils.getMessageFormatter(msgContext);
        } catch (AxisFault axisFault) {
            throw new JMSException("Unable to get the message formatter to use");
        }

        String contentType = messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction());

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            messageFormatter.writeTo(msgContext, format, baos, true);
            baos.flush();
        } catch (IOException e) {
            handleException("IO Error while creating BytesMessage", e);
        }

        if (msgType != null && JMSConstants.JMS_BYTE_MESSAGE.equals(msgType)
                || contentType.indexOf(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED) > -1) {
            message = session.createBytesMessage();
            BytesMessage bytesMsg = (BytesMessage) message;
            bytesMsg.writeBytes(baos.toByteArray());
        } else {
            message = session.createTextMessage(); // default
            TextMessage txtMsg = (TextMessage) message;
            txtMsg.setText(new String(baos.toByteArray()));
        }
        message.setStringProperty(BaseConstants.CONTENT_TYPE, contentType);

    } else if (JMSConstants.JMS_BYTE_MESSAGE.equals(jmsPayloadType)) {
        message = session.createBytesMessage();
        BytesMessage bytesMsg = (BytesMessage) message;
        OMElement wrapper = msgContext.getEnvelope().getBody()
                .getFirstChildWithName(BaseConstants.DEFAULT_BINARY_WRAPPER);
        OMNode omNode = wrapper.getFirstOMChild();
        if (omNode != null && omNode instanceof OMText) {
            Object dh = ((OMText) omNode).getDataHandler();
            if (dh != null && dh instanceof DataHandler) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try {
                    ((DataHandler) dh).writeTo(baos);
                } catch (IOException e) {
                    handleException("Error serializing binary content of element : "
                            + BaseConstants.DEFAULT_BINARY_WRAPPER, e);
                }
                bytesMsg.writeBytes(baos.toByteArray());
            }
        }

    } else if (JMSConstants.JMS_TEXT_MESSAGE.equals(jmsPayloadType)) {
        message = session.createTextMessage();
        TextMessage txtMsg = (TextMessage) message;
        txtMsg.setText(msgContext.getEnvelope().getBody()
                .getFirstChildWithName(BaseConstants.DEFAULT_TEXT_WRAPPER).getText());
    }

    // set the JMS correlation ID if specified
    String correlationId = getProperty(msgContext, JMSConstants.JMS_COORELATION_ID);
    if (correlationId == null && msgContext.getRelatesTo() != null) {
        correlationId = msgContext.getRelatesTo().getValue();
    }

    if (correlationId != null) {
        message.setJMSCorrelationID(correlationId);
    }

    if (msgContext.isServerSide()) {
        // set SOAP Action as a property on the JMS message
        setProperty(message, msgContext, BaseConstants.SOAPACTION);
    } else {
        String action = msgContext.getOptions().getAction();
        if (action != null) {
            message.setStringProperty(BaseConstants.SOAPACTION, action);
        }
    }

    JMSUtils.setTransportHeaders(msgContext, message);
    return message;
}

From source file:org.apache.axis2.transport.jms.JMSSender.java

/**
 * Create a JMS Message from the given MessageContext and using the given
 * session/*from ww  w .  j a  va  2s . c  om*/
 *
 * @param msgContext the MessageContext
 * @param session    the JMS session
 * @param contentTypeProperty the message property to be used to store the
 *                            content type
 * @return a JMS message from the context and session
 * @throws JMSException on exception
 * @throws AxisFault on exception
 */
private Message createJMSMessage(MessageContext msgContext, Session session, String contentTypeProperty)
        throws JMSException, AxisFault {

    Message message = null;
    String msgType = getProperty(msgContext, JMSConstants.JMS_MESSAGE_TYPE);

    // check the first element of the SOAP body, do we have content wrapped using the
    // default wrapper elements for binary (BaseConstants.DEFAULT_BINARY_WRAPPER) or
    // text (BaseConstants.DEFAULT_TEXT_WRAPPER) ? If so, do not create SOAP messages
    // for JMS but just get the payload in its native format
    String jmsPayloadType = guessMessageType(msgContext);

    if (jmsPayloadType == null) {

        OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
        MessageFormatter messageFormatter = null;
        try {
            messageFormatter = TransportUtils.getMessageFormatter(msgContext);
        } catch (AxisFault axisFault) {
            throw new JMSException("Unable to get the message formatter to use");
        }

        String contentType = messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction());

        boolean useBytesMessage = msgType != null && JMSConstants.JMS_BYTE_MESSAGE.equals(msgType)
                || contentType.indexOf(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED) > -1;

        OutputStream out;
        StringWriter sw;
        if (useBytesMessage) {
            BytesMessage bytesMsg = session.createBytesMessage();
            sw = null;
            out = new BytesMessageOutputStream(bytesMsg);
            message = bytesMsg;
        } else {
            sw = new StringWriter();
            try {
                out = new WriterOutputStream(sw, format.getCharSetEncoding());
            } catch (UnsupportedCharsetException ex) {
                handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
                return null;
            }
        }

        try {
            messageFormatter.writeTo(msgContext, format, out, true);
            out.close();
        } catch (IOException e) {
            handleException("IO Error while creating BytesMessage", e);
        }

        if (!useBytesMessage) {
            TextMessage txtMsg = session.createTextMessage();
            txtMsg.setText(sw.toString());
            message = txtMsg;
        }

        if (contentTypeProperty != null) {
            message.setStringProperty(contentTypeProperty, contentType);
        }

    } else if (JMSConstants.JMS_BYTE_MESSAGE.equals(jmsPayloadType)) {
        message = session.createBytesMessage();
        BytesMessage bytesMsg = (BytesMessage) message;
        OMElement wrapper = msgContext.getEnvelope().getBody()
                .getFirstChildWithName(BaseConstants.DEFAULT_BINARY_WRAPPER);
        OMNode omNode = wrapper.getFirstOMChild();
        if (omNode != null && omNode instanceof OMText) {
            Object dh = ((OMText) omNode).getDataHandler();
            if (dh != null && dh instanceof DataHandler) {
                try {
                    ((DataHandler) dh).writeTo(new BytesMessageOutputStream(bytesMsg));
                } catch (IOException e) {
                    handleException("Error serializing binary content of element : "
                            + BaseConstants.DEFAULT_BINARY_WRAPPER, e);
                }
            }
        }

    } else if (JMSConstants.JMS_TEXT_MESSAGE.equals(jmsPayloadType)) {
        message = session.createTextMessage();
        TextMessage txtMsg = (TextMessage) message;
        txtMsg.setText(msgContext.getEnvelope().getBody()
                .getFirstChildWithName(BaseConstants.DEFAULT_TEXT_WRAPPER).getText());
    }

    // set the JMS correlation ID if specified
    String correlationId = getProperty(msgContext, JMSConstants.JMS_COORELATION_ID);
    if (correlationId == null && msgContext.getRelatesTo() != null) {
        correlationId = msgContext.getRelatesTo().getValue();
    }

    if (correlationId != null) {
        message.setJMSCorrelationID(correlationId);
    }

    if (msgContext.isServerSide()) {
        // set SOAP Action as a property on the JMS message
        setProperty(message, msgContext, BaseConstants.SOAPACTION);
    } else {
        String action = msgContext.getOptions().getAction();
        if (action != null) {
            message.setStringProperty(BaseConstants.SOAPACTION, action);
        }
    }

    JMSUtils.setTransportHeaders(msgContext, message);
    return message;
}

From source file:org.apache.activemq.usecases.ConcurrentProducerDurableConsumerTest.java

protected TextMessage createTextMessage(Session session, String initText) throws Exception {
    TextMessage msg = session.createTextMessage();

    // Pad message text
    if (initText.length() < messageSize) {
        char[] data = new char[messageSize - initText.length()];
        Arrays.fill(data, '*');
        String str = new String(data);
        msg.setText(initText + str);

        // Do not pad message text
    } else {/*from ww  w .j a  va2s .  c  om*/
        msg.setText(initText);
    }

    return msg;
}

From source file:org.bremersee.common.jms.DefaultJmsConverter.java

private Message createXmlMessage(Object object, Session session) throws JMSException {
    try {//from  w  ww.j  a v  a  2  s.  c  o  m
        TextMessage msg = session.createTextMessage();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        StreamResult streamResult = new StreamResult(outputStream);
        marshaller.marshal(object, streamResult);
        String payload = CodingUtils.toStringSilently(outputStream.toByteArray(), StandardCharsets.UTF_8);
        msg.setText(payload);
        msg.setJMSType(object.getClass().getName());
        return msg;

    } catch (Throwable t) { // NOSONAR
        log.info("Creating XML JMS from object of type ["
                + (object == null ? "null" : object.getClass().getName()) + "] failed.");
        return null;
    }
}

From source file:org.apache.falcon.messaging.JMSMessageConsumerTest.java

private Message getMockOozieMessage(int i, Session session, boolean isFalconWF)
        throws FalconException, JMSException {
    TextMessage message = session.createTextMessage();
    message.setStringProperty("appType", "WORKFLOW_JOB");
    if (isFalconWF) {
        message.setStringProperty("appName", "FALCON_PROCESS_DEFAULT_process1");
    } else {/*from   w  ww .  j  a  v a2 s . com*/
        message.setStringProperty("appName", "OozieSampleShellWF");
    }
    message.setStringProperty("user", "falcon");
    switch (i % 4) {
    case 0:
        message.setText("{\"status\":\"RUNNING\",\"id\":\"0000042-130618221729631-oozie-oozi-W\""
                + ",\"startTime\":1342915200000}");
        break;
    case 1:
        message.setText("{\"status\":\"FAILED\",\"errorCode\":\"EL_ERROR\","
                + "\"errorMessage\":\"variable [dummyvalue] cannot be resolved\","
                + "\"id\":\"0000042-130618221729631-oozie-oozi-W\",\"startTime\":1342915200000,"
                + "\"endTime\":1366672183543}");
        break;
    case 2:
        message.setText("{\"status\":\"SUCCEEDED\",\"id\":\"0000039-130618221729631-oozie-oozi-W\""
                + ",\"startTime\":1342915200000,"
                + "\"parentId\":\"0000025-130618221729631-oozie-oozi-C@1\",\"endTime\":1366676224154}");
        break;
    case 3:
        message.setText("{\"status\":\"SUSPENDED\",\"id\":\"0000039-130618221729631-oozie-oozi-W\","
                + "\"startTime\":1342915200000,\"parentId\":\"0000025-130618221729631-oozie-oozi-C@1\"}");
        break;
    default:
    }
    return message;
}

From source file:nl.nn.adapterframework.extensions.ifsa.jms.IfsaFacade.java

/**
 * Intended for server-side reponse sending and implies that the received
 * message *always* contains a reply-to address.
 *//*  w w  w .  ja  v  a  2  s .c om*/
public void sendReply(QueueSession session, Message received_message, String response) throws IfsaException {
    QueueSender tqs = null;
    try {
        TextMessage answer = session.createTextMessage();
        answer.setText(response);
        Queue replyQueue = (Queue) received_message.getJMSReplyTo();
        tqs = session.createSender(replyQueue);
        if (log.isDebugEnabled())
            log.debug(getLogPrefix() + "sending reply to [" + received_message.getJMSReplyTo() + "]");
        ((IFSAServerQueueSender) tqs).sendReply(received_message, answer);
    } catch (Throwable t) {
        throw new IfsaException(t);
    } finally {
        if (tqs != null) {
            try {
                tqs.close();
            } catch (JMSException e) {
                log.warn(getLogPrefix() + "exception closing reply queue sender", e);
            }
        }
    }
}

From source file:org.apache.camel.component.jms.JmsBinding.java

/**
 * //from ww w  .  ja v a2 s .c  o m
 * Create the {@link Message} 
 * 
 * @return jmsMessage or null if the mapping was not successfully
 */
protected Message createJmsMessageForType(Exchange exchange, Object body, Map<String, Object> headers,
        Session session, CamelContext context, JmsMessageType type) throws JMSException {
    switch (type) {
    case Text: {
        TextMessage message = session.createTextMessage();
        String payload = context.getTypeConverter().convertTo(String.class, exchange, body);
        message.setText(payload);
        return message;
    }
    case Bytes: {
        BytesMessage message = session.createBytesMessage();
        byte[] payload = context.getTypeConverter().convertTo(byte[].class, exchange, body);
        message.writeBytes(payload);
        return message;
    }
    case Map: {
        MapMessage message = session.createMapMessage();
        Map payload = context.getTypeConverter().convertTo(Map.class, exchange, body);
        populateMapMessage(message, payload, context);
        return message;
    }
    case Object:
        Serializable payload;
        try {
            payload = context.getTypeConverter().mandatoryConvertTo(Serializable.class, exchange, body);
        } catch (NoTypeConversionAvailableException e) {
            // cannot convert to serializable then thrown an exception to avoid sending a null message
            JMSException cause = new MessageFormatException(e.getMessage());
            cause.initCause(e);
            throw cause;
        }
        return session.createObjectMessage(payload);
    default:
        break;
    }
    return null;
}

From source file:org.apache.falcon.messaging.JMSMessageConsumerTest.java

private Message getMockOozieCoordMessage(int i, Session session, boolean isFalconWF)
        throws FalconException, JMSException {
    TextMessage message = session.createTextMessage();
    message.setStringProperty("appType", "COORDINATOR_ACTION");
    if (isFalconWF) {
        message.setStringProperty("appName", "FALCON_PROCESS_DEFAULT_process1");
    } else {// www. java  2s. com
        message.setStringProperty("appName", "OozieSampleShellWF");
    }
    message.setStringProperty("user", "falcon");
    switch (i % 5) {
    case 0:
        message.setText("{\"status\":\"WAITING\",\"nominalTime\":1310342400000,\"missingDependency\""
                + ":\"hdfs://gsbl90107.blue.com:8020/user/john/dir1/file1\","
                + "\"id\":\"0000025-130618221729631-oozie-oozi-C@1\",\"startTime\":1342915200000,"
                + "\"parentId\":\"0000025-130618221729631-oozie-oozi-C\"}");
        message.setStringProperty("eventStatus", "WAITING");
        break;
    case 1:
        message.setText("{\"status\":\"RUNNING\",\"nominalTime\":1310342400000,"
                + "\"id\":\"0000025-130618221729631-oozie-oozi-C@1\","
                + "\"startTime\":1342915200000,\"parentId\":\"0000025-130618221729631-oozie-oozi-C\"}");
        message.setStringProperty("eventStatus", "STARTED");
        break;
    case 2:
        message.setText("{\"status\":\"SUCCEEDED\",\"nominalTime\":1310342400000,"
                + "\"id\":\"0000025-130618221729631-oozie-oozi-C@1\","
                + "\"startTime\":1342915200000,\"parentId\":\"0000025-130618221729631-oozie-oozi-C\","
                + "\"endTime\":1366677082799}");
        message.setStringProperty("eventStatus", "SUCCESS");
        break;
    case 3:
        message.setText("{\"status\":\"FAILED\",\"errorCode\":\"E0101\",\"errorMessage\":"
                + "\"dummyError\",\"nominalTime\":1310342400000,"
                + "\"id\":\"0000025-130618221729631-oozie-oozi-C@1\",\"startTime\":1342915200000,"
                + "\"parentId\":\"0000025-130618221729631-oozie-oozi-C\",\"endTime\":1366677140818}");
        message.setStringProperty("eventStatus", "FAILURE");
        break;
    case 4:
        message.setText("{\"status\":\"TIMEDOUT\",\"nominalTime\":1310342400000,"
                + "\"id\":\"0000025-130618221729631-oozie-oozi-C@1\",\"startTime\":1342915200000,"
                + "\"parentId\":\"0000025-130618221729631-oozie-oozi-C\",\"endTime\":1366677140818}");
        message.setStringProperty("eventStatus", "FAILURE");
    default:
    }
    return message;
}