Example usage for javax.jms StreamMessage reset

List of usage examples for javax.jms StreamMessage reset

Introduction

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

Prototype


void reset() throws JMSException;

Source Link

Document

Puts the message body in read-only mode and repositions the stream to the beginning.

Usage

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

/**
 * @param message/*from w ww .  j a v a2 s  . com*/
 *            the message to receive the bytes from. Note this only works
 *            for TextMessge, ObjectMessage, StreamMessage and BytesMessage.
 * @return a byte array corresponding with the message payload
 * @throws JMSException
 *             if the message can't be read or if the message passed is a
 *             MapMessage
 * @throws java.io.IOException
 *             if a failiare occurs while stream and converting the message
 *             data
 */
public static byte[] getBytesFromMessage(Message message) throws JMSException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024 * 2];
    int len;

    if (message instanceof BytesMessage) {
        BytesMessage bMsg = (BytesMessage) message;
        // put message in read-only mode
        bMsg.reset();
        while ((len = bMsg.readBytes(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
    } else if (message instanceof StreamMessage) {
        StreamMessage sMsg = (StreamMessage) message;
        sMsg.reset();
        while ((len = sMsg.readBytes(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
    } else if (message instanceof ObjectMessage) {
        ObjectMessage oMsg = (ObjectMessage) message;
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(bs);
        os.writeObject(oMsg.getObject());
        os.flush();
        baos.write(bs.toByteArray());
        os.close();
        bs.close();
    } else if (message instanceof TextMessage) {
        TextMessage tMsg = (TextMessage) message;
        baos.write(tMsg.getText().getBytes());
    } else {
        throw new JMSException("Cannot get bytes from Map Message");
    }

    baos.flush();
    byte[] bytes = baos.toByteArray();
    baos.close();
    return bytes;
}

From source file:com.jkoolcloud.tnt4j.streams.parsers.ActivityJMSMessageParser.java

/**
 * Parse JMS {@link StreamMessage} activity info into activity data map.
 *
 * @param streamMessage/*from  w w  w.j av  a 2s .  c  o  m*/
 *            JMS stream message
 * @param dataMap
 *            activity data map collected from JMS {@link StreamMessage}
 * @throws JMSException
 *             if JMS exception occurs while reading bytes from message.
 */
protected void parseStreamMessage(StreamMessage streamMessage, Map<String, Object> dataMap)
        throws JMSException {
    streamMessage.reset();

    byte[] buffer = new byte[BYTE_BUFFER_LENGTH];

    int bytesRead = 0;
    ByteArrayOutputStream baos = new ByteArrayOutputStream(buffer.length);

    try {
        do {
            bytesRead = streamMessage.readBytes(buffer);

            baos.write(buffer);
        } while (bytesRead != 0);
    } catch (IOException exc) {
        logger().log(OpLevel.ERROR, StreamsResources.getString(JMSStreamConstants.RESOURCE_BUNDLE_NAME,
                "ActivityJMSMessageParser.bytes.buffer.error"), exc);
    }

    byte[] bytes = baos.toByteArray();
    Utils.close(baos);

    if (ArrayUtils.isNotEmpty(bytes)) {
        dataMap.put(StreamsConstants.ACTIVITY_DATA_KEY, convertToString ? Utils.getString(bytes) : bytes);
    }
}

From source file:hermes.renderers.DefaultMessageRenderer.java

/**
 * List out all the properties in the stream message.
 * /*from   w  ww  .j  av  a2s. com*/
 * @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.integration.JmsTransformersTestCase.java

@Test
public void testTransformStreamMessage() throws Exception {
    RequestContext.setEvent(getTestEvent("test"));

    String text = "Test Text";
    int i = 97823;
    double d = 0923.2143E124;
    List list = new ArrayList();
    list.add(new Integer(i));
    list.add(new Double(d));
    list.add(text);//from   ww w.ja va2 s. com

    StreamMessage message = session.createStreamMessage();
    message.writeString(text);
    message.writeInt(i);
    message.writeDouble(d);
    message.reset();

    AbstractJmsTransformer trans = createObject(JMSMessageToObject.class);
    Object transformedObject = trans.transform(message);
    assertTrue("Transformed object should be a List", transformedObject instanceof List);

    final List result = (List) transformedObject;
    String newText = (String) result.get(0);
    Integer newI = (Integer) result.get(1);
    Double newD = (Double) result.get(2);
    assertEquals(i, newI.intValue());
    assertEquals(new Double(d), newD);
    assertEquals(text, newText);
}

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

/**
 * @param message the message to receive the bytes from. Note this only works for
 *                TextMessge, ObjectMessage, StreamMessage and BytesMessage.
 * @param jmsSpec indicates the JMS API version, either
 *                {@link JmsConstants#JMS_SPECIFICATION_102B} or
 *                {@link JmsConstants#JMS_SPECIFICATION_11}. Any other value
 *                including <code>null</code> is treated as fallback to
 *                {@link JmsConstants#JMS_SPECIFICATION_102B}.
 * @return a byte array corresponding with the message payload
 * @throws JMSException        if the message can't be read or if the message passed is
 *                             a MapMessage
 * @throws java.io.IOException if a failure occurs while reading the stream and
 *                             converting the message data
 *///from   w ww .  jav a2  s .  co m
public static byte[] toByteArray(Message message, String jmsSpec, String encoding)
        throws JMSException, IOException {
    if (message instanceof BytesMessage) {
        BytesMessage bMsg = (BytesMessage) message;
        bMsg.reset();

        if (JmsConstants.JMS_SPECIFICATION_11.equals(jmsSpec)) {
            long bmBodyLength = bMsg.getBodyLength();
            if (bmBodyLength > Integer.MAX_VALUE) {
                throw new JMSException("Size of BytesMessage exceeds Integer.MAX_VALUE; "
                        + "please consider using JMS StreamMessage instead");
            }

            if (bmBodyLength > 0) {
                byte[] bytes = new byte[(int) bmBodyLength];
                bMsg.readBytes(bytes);
                return bytes;
            } else {
                return ArrayUtils.EMPTY_BYTE_ARRAY;
            }
        } else {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
            byte[] buffer = new byte[4096];
            int len;

            while ((len = bMsg.readBytes(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }

            if (baos.size() > 0) {
                return baos.toByteArray();
            } else {
                return ArrayUtils.EMPTY_BYTE_ARRAY;
            }
        }
    } else if (message instanceof StreamMessage) {
        StreamMessage sMsg = (StreamMessage) message;
        sMsg.reset();

        ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
        byte[] buffer = new byte[4096];
        int len;

        while ((len = sMsg.readBytes(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }

        return baos.toByteArray();
    } else if (message instanceof ObjectMessage) {
        ObjectMessage oMsg = (ObjectMessage) message;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(baos);
        os.writeObject(oMsg.getObject());
        os.flush();
        os.close();
        return baos.toByteArray();
    } else if (message instanceof TextMessage) {
        TextMessage tMsg = (TextMessage) message;
        String tMsgText = tMsg.getText();

        if (null == tMsgText) {
            // Avoid creating new instances of byte arrays, even empty ones. The
            // load on this part of the code can be high.
            return ArrayUtils.EMPTY_BYTE_ARRAY;
        } else {
            return tMsgText.getBytes(encoding);
        }
    } else {
        throw new JMSException("Cannot get bytes from Map Message");
    }
}

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 www. j  ava2s  .com*/
    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();
    while (true) {
        try {/*from  ww w  . j  av a2s.c om*/
            Object o = message.readObject();
            writeObject(o);
        } catch (MessageEOFException e) {
            break;
        }
    }
    storeContent();
}