Example usage for javax.jms Session createStreamMessage

List of usage examples for javax.jms Session createStreamMessage

Introduction

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

Prototype


StreamMessage createStreamMessage() throws JMSException;

Source Link

Document

Creates a StreamMessage object.

Usage

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

public static Message getMessageForObject(Object object, Session session) throws JMSException {
    if (object instanceof Message) {
        return (Message) object;
    } else if (object instanceof String) {
        TextMessage text = session.createTextMessage((String) object);
        return text;
    } else if (object instanceof Map) {
        MapMessage map = session.createMapMessage();
        Map.Entry entry = null;//w w w .j  ava  2 s.c  om
        Map temp = (Map) object;

        for (Iterator i = temp.entrySet().iterator(); i.hasNext();) {
            entry = (Map.Entry) i.next();
            map.setObject(entry.getKey().toString(), entry.getValue());
        }

        return map;
    } else if (object instanceof InputStream) {
        StreamMessage stream = session.createStreamMessage();
        InputStream temp = (InputStream) object;

        byte[] buffer = new byte[1024 * 4];
        int len = 0;
        try {
            while ((len = temp.read(buffer)) != -1) {
                stream.writeBytes(buffer, 0, len);
            }
        } catch (IOException e) {
            throw new JMSException("Failed to read input stream to create a stream message: " + e);
        }

        return stream;
    } else if (object instanceof byte[]) {
        BytesMessage bytes = session.createBytesMessage();
        byte[] buf = (byte[]) object;
        for (int i = 0; i < buf.length; i++) {
            bytes.writeByte(buf[i]);
        }

        return bytes;
    } else if (object instanceof Serializable) {
        ObjectMessage oMsg = session.createObjectMessage();
        oMsg.setObject((Serializable) object);
        return oMsg;
    } else {
        throw new JMSException(
                "Source was not a supported type, data must be Serializable, String, byte[], Map or InputStream");
    }
}

From source file:org.apache.qpid.test.utils.QpidBrokerTestCase.java

public Message createMessage(Session session, int messageSize) throws JMSException
{
    String payload = new String(new byte[messageSize]);

    Message message;/*from   w w  w .j  av a2 s. co m*/

    switch (_messageType)
    {
        case BYTES:
            message = session.createBytesMessage();
            ((BytesMessage) message).writeUTF(payload);
            break;
        case MAP:
            message = session.createMapMessage();
            ((MapMessage) message).setString(CONTENT, payload);
            break;
        default: // To keep the compiler happy
        case TEXT:
            message = session.createTextMessage();
            ((TextMessage) message).setText(payload);
            break;
        case OBJECT:
            message = session.createObjectMessage();
            ((ObjectMessage) message).setObject(payload);
            break;
        case STREAM:
            message = session.createStreamMessage();
            ((StreamMessage) message).writeString(payload);
            break;
    }

    return message;
}

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

private static Message inputStreamToMessage(InputStream value, Session session) throws JMSException {
    StreamMessage streamMessage = session.createStreamMessage();
    byte[] buffer = new byte[4096];
    int len;/* w  w w .j  a  v a2s.  c o m*/

    try {
        while ((len = value.read(buffer)) != -1) {
            streamMessage.writeBytes(buffer, 0, len);
        }
    } catch (IOException e) {
        throw new JMSException("Failed to read input stream to create a stream message: " + e);
    } finally {
        IOUtils.closeQuietly(value);
    }

    return streamMessage;
}

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

private static Message listToMessage(List<?> value, Session session) throws JMSException {
    StreamMessage sMsg = session.createStreamMessage();

    for (Iterator<?> iter = value.iterator(); iter.hasNext();) {
        Object o = iter.next();/*  w w w.  ja va 2s  .c  om*/
        if (validateStreamMessageType(o)) {
            sMsg.writeObject(o);
        } else {
            throw new MessageFormatException(
                    String.format("Invalid type passed to StreamMessage: %s . Allowed types are: "
                            + "Boolean, Byte, Short, Character, Integer, Long, Float, Double,"
                            + "String and byte[]", ClassUtils.getShortClassName(o, "null")));
        }
    }
    return sMsg;
}

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);/*  ww w.ja  v a 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.mule.transport.jms.JmsMessageUtilsTestCase.java

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

    // Creates a test list with data
    List data = new ArrayList();
    data.add(new Object());

    JmsMessageUtils.toMessage(data, session);
}