Example usage for javax.jms BytesMessage readBytes

List of usage examples for javax.jms BytesMessage readBytes

Introduction

In this page you can find the example usage for javax.jms BytesMessage readBytes.

Prototype


int readBytes(byte[] value, int length) throws JMSException;

Source Link

Document

Reads a portion of the bytes message stream.

Usage

From source file:org.perfcake.message.sender.RequestResponseJmsSenderTest.java

@Test
public void testResponseSend() throws Exception {
    final String queueName = "queue/test";
    final String replyQueueName = "queue/test_reply";

    final JmsHelper.Wiretap wiretap = JmsHelper.wiretap(queueName, replyQueueName);
    wiretap.start();/*from www .  java2  s. c  om*/

    final Properties props = new Properties();
    props.setProperty("messagetType", "STRING");
    props.setProperty("target", queueName);
    props.setProperty("responseTarget", replyQueueName);
    props.setProperty("connectionFactory", "ConnectionFactory");
    props.setProperty("transacted", "true");
    props.setProperty("autoAck", "false");

    final RequestResponseJmsSender sender = (RequestResponseJmsSender) ObjectFactory
            .summonInstance(RequestResponseJmsSender.class.getName(), props);

    Assert.assertEquals(sender.getMessageType(), RequestResponseJmsSender.MessageType.STRING);
    Assert.assertEquals(sender.getTarget(), queueName);
    Assert.assertEquals(sender.getResponseTarget(), replyQueueName);
    Assert.assertEquals(sender.isTransacted(), true);
    Assert.assertEquals(sender.isAutoAck(), false);

    try {
        sender.init();

        // make sure the queues are empty
        Assert.assertNull(JmsHelper.readMessage(factory, 500, queue));
        Assert.assertNull(JmsHelper.readMessage(factory, 500, queueReply));

        // STRING
        org.perfcake.message.Message message = new org.perfcake.message.Message();
        final String payload = "Hello World!";
        message.setPayload(payload);
        sender.preSend(message, null, null);
        Serializable response = sender.send(message, null);
        sender.postSend(message);

        Assert.assertTrue(response instanceof String);
        Assert.assertEquals((String) response, payload);

        // what did the wiretap see
        Message jmsResponse = wiretap.getLastMessage();
        Assert.assertTrue(jmsResponse instanceof TextMessage);
        Assert.assertEquals(((TextMessage) jmsResponse).getText(), payload);

        // OBJECT
        sender.setMessageType(JmsSender.MessageType.OBJECT);
        message = new org.perfcake.message.Message();
        final Long payloadObject = 42L;
        message.setPayload(payloadObject);
        sender.preSend(message, null, null);
        response = sender.send(message, null);
        sender.postSend(message);

        Assert.assertTrue(response instanceof Long);
        Assert.assertEquals((Long) response, payloadObject);

        // what did the wiretap see
        jmsResponse = wiretap.getLastMessage();
        Assert.assertTrue(jmsResponse instanceof ObjectMessage);
        Assert.assertTrue(((ObjectMessage) jmsResponse).getObject() instanceof Long);
        Assert.assertEquals((Long) ((ObjectMessage) jmsResponse).getObject(), payloadObject);

        // BYTEARRAY
        sender.setMessageType(JmsSender.MessageType.BYTEARRAY);
        message = new org.perfcake.message.Message();
        message.setPayload(payload);
        sender.preSend(message, null, null);
        response = sender.send(message, null);
        sender.postSend(message);

        Assert.assertTrue(response instanceof byte[]);
        Assert.assertEquals(new String((byte[]) response, "UTF-8").trim(), payload);

        // what did the wiretap see
        jmsResponse = wiretap.getLastMessage();
        Assert.assertTrue(jmsResponse instanceof BytesMessage);
        final BytesMessage bytesMessage = (BytesMessage) jmsResponse;
        bytesMessage.reset();
        final byte[] bytes = new byte[(int) bytesMessage.getBodyLength()];
        bytesMessage.readBytes(bytes, bytes.length);
        Assert.assertEquals(new String(bytes, "UTF-8").trim(), payload);

        wiretap.stop();

        // make sure the queue is empty
        Assert.assertNull(JmsHelper.readMessage(factory, 500, queue));
        Assert.assertNull(JmsHelper.readMessage(factory, 500, queueReply));
    } finally {
        sender.close();
    }
}