Example usage for javax.jms StreamMessage readObject

List of usage examples for javax.jms StreamMessage readObject

Introduction

In this page you can find the example usage for javax.jms StreamMessage readObject.

Prototype


Object readObject() throws JMSException;

Source Link

Document

Reads an object from the stream message.

Usage

From source file:com.mirth.connect.connectors.jms.JmsMessageUtils.java

public static Object getObjectForMessage(Message source) throws JMSException {
    Object result = null;/*from w  w w .  j a  v  a  2 s .c o  m*/
    try {
        if (source instanceof ObjectMessage) {
            result = ((ObjectMessage) source).getObject();
        } else if (source instanceof MapMessage) {
            Hashtable map = new Hashtable();
            MapMessage m = (MapMessage) source;

            for (Enumeration e = m.getMapNames(); e.hasMoreElements();) {
                String name = (String) e.nextElement();
                Object obj = m.getObject(name);
                map.put(name, obj);
            }

            result = map;
        } else if (source instanceof javax.jms.BytesMessage) {

            javax.jms.BytesMessage bm = (javax.jms.BytesMessage) source;
            java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();

            byte[] buffer = new byte[1024 * 4];
            int len = 0;
            bm.reset();
            while ((len = bm.readBytes(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            baos.flush();
            result = baos.toByteArray();
            baos.close();
            if (result != null) {
                if (logger.isDebugEnabled())
                    logger.debug("JMSToObject: extracted " + ((byte[]) result).length
                            + " bytes from JMS BytesMessage");
            }
        } else if (source instanceof TextMessage) {
            result = ((TextMessage) source).getText();

        } else if (source instanceof BytesMessage) {
            byte[] bytes = getBytesFromMessage(source);
            return CompressionHelper.uncompressByteArray(bytes);
        } else if (source instanceof StreamMessage) {

            StreamMessage sm = (javax.jms.StreamMessage) source;

            result = new java.util.Vector();
            try {
                Object obj = null;
                while ((obj = sm.readObject()) != null) {
                    ((java.util.Vector) result).addElement(obj);
                }
            } catch (MessageEOFException eof) {
            } catch (Exception e) {
                throw new JMSException("Failed to extract information from JMS Stream Message: " + e);
            }
        } else {
            result = source;
        }
    } catch (Exception e) {
        throw new JMSException("Failed to transform message: " + e.getMessage());
    }
    return result;
}

From source file:hermes.renderers.DefaultMessageRenderer.java

/**
 * List out all the properties in the stream message.
 * //from   www  . j  ava2s  . c o  m
 * @param streamMessage
 * @return
 * @throws JMSException
 */
protected JComponent handleStreamMessage(StreamMessage streamMessage) throws JMSException {
    JTextArea textPane = new JTextArea();
    StringBuffer buffer = new StringBuffer();

    textPane.setEditable(false);

    streamMessage.reset();

    try {
        for (;;) {
            buffer.append(streamMessage.readObject().toString()).append("\n");
        }
    } catch (MessageEOFException ex) {
        // NOP
    }

    return textPane;
}

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

public static Object toObject(Message source, String jmsSpec, String encoding)
        throws JMSException, IOException {
    if (source instanceof ObjectMessage) {
        return ((ObjectMessage) source).getObject();
    } else if (source instanceof MapMessage) {
        Map<String, Object> map = new HashMap<String, Object>();
        MapMessage m = (MapMessage) source;

        for (Enumeration<?> e = m.getMapNames(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            Object obj = m.getObject(name);
            map.put(name, obj);/*from ww  w  .  ja va2s.  c  om*/
        }

        return map;
    } else if (source instanceof TextMessage) {
        return ((TextMessage) source).getText();
    } else if (source instanceof BytesMessage) {
        return toByteArray(source, jmsSpec, encoding);
    } else if (source instanceof StreamMessage) {
        List<Object> result = new ArrayList<Object>();
        try {
            StreamMessage sMsg = (StreamMessage) source;
            Object obj;
            while ((obj = sMsg.readObject()) != null) {
                result.add(obj);
            }
        } catch (MessageEOFException eof) {
            // ignored
        } catch (Exception e) {
            throw new JMSException("Failed to extract information from JMS Stream Message: " + e);
        }
        return result;
    }

    // what else is there to do?
    return source;
}

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

@Test
public void testStreamMessageSerialization() throws Exception {
    Session session = mock(Session.class);
    when(session.createStreamMessage()).thenReturn(new ActiveMQStreamMessage());

    // Creates a test list with data
    List data = new ArrayList();
    data.add(Boolean.TRUE);//from  w w w. ja  va 2 s  .  c  o  m
    data.add(new Byte("1"));
    data.add(new Short("2"));
    data.add(new Character('3'));
    data.add(new Integer("4"));
    // can't write Longs: https://issues.apache.org/activemq/browse/AMQ-1965
    // data.add(new Long("5"));
    data.add(new Float("6"));
    data.add(new Double("7"));
    data.add(new String("8"));
    data.add(null);
    data.add(new byte[] { 9, 10 });

    StreamMessage result = (StreamMessage) JmsMessageUtils.toMessage(data, session);

    // Resets so it's readable
    result.reset();
    assertEquals(Boolean.TRUE, result.readObject());
    assertEquals(new Byte("1"), result.readObject());
    assertEquals(new Short("2"), result.readObject());
    assertEquals(new Character('3'), result.readObject());
    assertEquals(new Integer("4"), result.readObject());
    // can't write Longs: https://issues.apache.org/activemq/browse/AMQ-1965
    // assertEquals(new Long("5"), result.readObject());
    assertEquals(new Float("6"), result.readObject());
    assertEquals(new Double("7"), result.readObject());
    assertEquals(new String("8"), result.readObject());
    assertNull(result.readObject());
    assertTrue(Arrays.equals(new byte[] { 9, 10 }, (byte[]) result.readObject()));
}

From source file:org.skyscreamer.nevado.jms.message.NevadoStreamMessage.java

protected NevadoStreamMessage(StreamMessage message) throws JMSException {
    super(message);
    message.reset();// w  w  w .  j a  v a2 s.c om
    while (true) {
        try {
            Object o = message.readObject();
            writeObject(o);
        } catch (MessageEOFException e) {
            break;
        }
    }
    storeContent();
}

From source file:org.wso2.carbon.andes.core.internal.util.Utils.java

/**
 * A stream message can have java primitives plus objects, as its content. This method is used
 * for getting the valid message content from the stream.
 *
 * @param streamMessage - input message/*from   w  ww .  jav a 2s  .  c o  m*/
 * @param sb            - a string builder to build the whole message content
 * @return - complete message content inside the stream message
 * @throws JMSException
 */
private static String getContentFromStreamMessage(StreamMessage streamMessage, StringBuilder sb)
        throws JMSException {

    boolean eofReached = false;

    while (!eofReached) {

        try {
            Object obj = streamMessage.readObject();
            // obj could be null if the wire type is AbstractBytesTypedMessage.NULL_STRING_TYPE
            if (null != obj) {
                sb.append(obj.toString()).append(", ");
            }
        } catch (MessageEOFException ex) {
            eofReached = true;
        }
    }

    return StringEscapeUtils.escapeHtml(sb.toString());
}

From source file:org.wso2.carbon.andes.ui.client.QueueBrowserClient.java

/**
 * A stream message can have java primitives plus objects, as its content. This message it used to retrieve the
 *
 * @param queueMessage - input message// w ww.  j a v  a 2 s .c  o  m
 * @param sb           - a string builder to build the whole message content
 * @return - complete message content inside the stream message
 * @throws JMSException
 */
private String getContentFromStreamMessage(StreamMessage queueMessage, StringBuilder sb) throws JMSException {

    boolean eofReached = false;

    while (!eofReached) {

        try {
            Object obj = queueMessage.readObject();
            // obj could be null if the wire type is AbstractBytesTypedMessage.NULL_STRING_TYPE
            if (null != obj) {
                sb.append(obj.toString()).append(", ");
            }
        } catch (MessageEOFException ex) {
            eofReached = true;
        }

    }

    return StringEscapeUtils.escapeHtml(sb.toString());
}