Example usage for javax.jms Session createMapMessage

List of usage examples for javax.jms Session createMapMessage

Introduction

In this page you can find the example usage for javax.jms Session createMapMessage.

Prototype


MapMessage createMapMessage() throws JMSException;

Source Link

Document

Creates a MapMessage object.

Usage

From source file:org.mule.transport.jms.JmsMessageUtilsTestCase.java

/**
 * Tests that is able to convert a Map which only contains simple values into a
 * MapMessage./*from  w ww .jav a  2s .c  om*/
 */
@Test
public void testConvertsValidMapWithSimpleValuesToMapMessage() throws JMSException {
    Session session = mock(Session.class);
    when(session.createMapMessage()).thenReturn(new ActiveMQMapMessage());

    // Creates a test Map with data
    Map data = new HashMap();
    data.put("value1", new Float(4));
    data.put("value2", new byte[] { 1, 2, 3 });
    data.put("value3", "value3");
    data.put("value4", new Double(67.9));
    data.put("value5", true);
    data.put("value6", null);

    Message message = JmsMessageUtils.toMessage(data, session);
    assertTrue(message instanceof MapMessage);

    MapMessage mapMessage = (MapMessage) message;
    assertEquals(new Float(4), mapMessage.getFloat("value1"), 0);
    assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, mapMessage.getBytes("value2")));
    assertEquals("value3", mapMessage.getString("value3"));
    assertEquals(new Double(67.9), mapMessage.getDouble("value4"), 0);
    assertTrue(mapMessage.getBoolean("value5"));
    assertNull(mapMessage.getObject("value6"));
}

From source file:org.wso2.carbon.event.output.adapter.jms.internal.util.JMSMessageSender.java

public Message convertToJMSMessage(Object messageObj, Map<String, String> messageProperties, Session session) {
    Message jmsMessage = null;//from   w w  w.j  av a  2  s  .c  om
    try {
        if (messageObj instanceof OMElement) {
            jmsMessage = session.createTextMessage(messageObj.toString());
        } else if (messageObj instanceof String) {
            jmsMessage = session.createTextMessage((String) messageObj);
        } else if (messageObj instanceof Map) {
            MapMessage mapMessage = session.createMapMessage();
            Map sourceMessage = (Map) messageObj;
            for (Object key : sourceMessage.keySet()) {
                mapMessage.setObject((String) key, sourceMessage.get(key));
            }
            jmsMessage = mapMessage;
        }
    } catch (JMSException e) {
        handleException("Failed to publish to topic:" + messageProperties.get(JMSConstants.PARAM_DESTINATION),
                e);
    }

    return jmsMessage;
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.jms.JMSReplySender.java

/**
 * Create a JMS Message from the given MessageContext and using the given
 * session//from w  w w  .  j  a  va2  s . c o  m
 * 
 * @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 synCtx, Session session, String contentTypeProperty)
        throws JMSException {

    Message message = null;
    org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) synCtx)
            .getAxis2MessageContext();
    String msgType = getProperty(msgContext, JMSConstants.JMS_MESSAGE_TYPE);
    String jmsPayloadType = guessMessageType(msgContext);
    if (jmsPayloadType == null) {
        OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
        MessageFormatter messageFormatter = null;
        try {
            messageFormatter = MessageProcessorSelector.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) {
                log.error("Unsupported encoding " + format.getCharSetEncoding(), ex);
                throw new JMSException("Unsupported encoding " + format.getCharSetEncoding());
            }
        }

        try {
            messageFormatter.writeTo(msgContext, format, out, true);
            out.close();
        } catch (IOException e) {
            log.error("IO Error while creating BytesMessage", e);
            throw new JMSException("IO Error while creating BytesMessage");
        }
        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) {
                    log.error("Error serializing binary content of element : "
                            + BaseConstants.DEFAULT_BINARY_WRAPPER, e);
                    throw new JMSException("Error serializing binary content of element : "
                            + BaseConstants.DEFAULT_BINARY_WRAPPER);
                }
            }
        }

    } 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());
    } else if (JMSConstants.JMS_MAP_MESSAGE.equalsIgnoreCase(jmsPayloadType)) {
        message = session.createMapMessage();
        JMSUtils.convertXMLtoJMSMap(
                msgContext.getEnvelope().getBody().getFirstChildWithName(JMSConstants.JMS_MAP_QNAME),
                (MapMessage) message);
    }

    // set the JMS correlation ID if specified
    String correlationId = (String) synCtx.getProperty(JMSConstants.JMS_COORELATION_ID);
    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.wso2.carbon.integration.test.client.JMSPublisherClient.java

/**
 * Each message will be divided into groups and create the map message
 *
 * @param producer      Used for sending messages to a destination
 * @param session       Used to produce the messages to be sent
 * @param messagesList  List of messages to be send. An individual message event data should be in
 *                      "attributeName(attributeType):attributeValue" format
 *
 *///  w  w  w  . j av a 2s . c o  m
public static void publishMapMessages(MessageProducer producer, Session session, List<String> messagesList)
        throws JMSException {
    String regexPattern = "(.*)\\((.*)\\):(.*)";
    Pattern pattern = Pattern.compile(regexPattern);
    for (String message : messagesList) {
        MapMessage jmsMapMessage = session.createMapMessage();
        for (String line : message.split("\\n")) {
            if (line != null && !line.equalsIgnoreCase("")) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    jmsMapMessage.setObject(matcher.group(1),
                            parseAttributeValue(matcher.group(2), matcher.group(3)));
                }
            }
        }
        producer.send(jmsMapMessage);
    }
}

From source file:org.wso2.carbon.sample.jmsclient.JMSClient.java

public static void publishMapMessage(MessageProducer producer, Session session,
        List<Map<String, Object>> messagesList) throws IOException, JMSException {
    for (Map<String, Object> message : messagesList) {
        MapMessage mapMessage = session.createMapMessage();
        message.put("time", System.currentTimeMillis());
        for (Map.Entry<String, Object> entry : message.entrySet()) {
            mapMessage.setObject(entry.getKey(), entry.getValue());
        }//from www.j  a  va 2  s .c  o  m
        producer.send(mapMessage);
    }
    log.info("messages sent (per-thread):" + messagesList.size());
}

From source file:org.wso2.carbon.sample.jmsclient.JMSClientUtil.java

/**
 * Each message will be divided into groups and create the map message
 *
 * @param producer     Used for sending messages to a destination
 * @param session      Used to produce the messages to be sent
 * @param messagesList List of messages to be sent
 *                     individual message event data should be in
 *                     "attributeName(attributeType):attributeValue" format
 *///w ww.j a v a2 s  .  com
public static void publishMapMessage(MessageProducer producer, Session session, List<String> messagesList)
        throws IOException, JMSException {
    String regexPattern = "(.*)\\((.*)\\):(.*)";
    Pattern pattern = Pattern.compile(regexPattern);
    for (String message : messagesList) {
        MapMessage mapMessage = session.createMapMessage();
        for (String line : message.split("\\n")) {
            if (line != null && !line.equalsIgnoreCase("")) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    mapMessage.setObject(matcher.group(1),
                            parseAttributeValue(matcher.group(2), matcher.group(3)));
                }
            }
        }
        producer.send(mapMessage);
    }
}