Search in sources :

Example 6 with BytesMessage

use of javax.jms.BytesMessage in project spring-framework by spring-projects.

the class MessageListenerAdapterTests method testWithMessageContentsDelegateForBytesMessage.

@Test
public void testWithMessageContentsDelegateForBytesMessage() throws Exception {
    BytesMessage bytesMessage = mock(BytesMessage.class);
    // BytesMessage contents must be unwrapped...
    given(bytesMessage.getBodyLength()).willReturn(new Long(TEXT.getBytes().length));
    given(bytesMessage.readBytes(any(byte[].class))).willAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            byte[] bytes = (byte[]) invocation.getArguments()[0];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(TEXT.getBytes());
            return inputStream.read(bytes);
        }
    });
    MessageContentsDelegate delegate = mock(MessageContentsDelegate.class);
    MessageListenerAdapter adapter = new MessageListenerAdapter(delegate);
    adapter.onMessage(bytesMessage);
    verify(delegate).handleMessage(TEXT.getBytes());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BytesMessage(javax.jms.BytesMessage) Test(org.junit.Test)

Example 7 with BytesMessage

use of javax.jms.BytesMessage in project spring-framework by spring-projects.

the class SimpleMessageConverterTests method testByteArrayConversion.

@Test
public void testByteArrayConversion() throws JMSException {
    Session session = mock(Session.class);
    BytesMessage message = mock(BytesMessage.class);
    byte[] content = "test".getBytes();
    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content);
    given(session.createBytesMessage()).willReturn(message);
    given(message.getBodyLength()).willReturn((long) content.length);
    given(message.readBytes(any(byte[].class))).willAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            return byteArrayInputStream.read((byte[]) invocation.getArguments()[0]);
        }
    });
    SimpleMessageConverter converter = new SimpleMessageConverter();
    Message msg = converter.toMessage(content, session);
    assertEquals(content.length, ((byte[]) converter.fromMessage(msg)).length);
    verify(message).writeBytes(content);
}
Also used : TextMessage(javax.jms.TextMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BytesMessage(javax.jms.BytesMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(javax.jms.Session) Test(org.junit.Test)

Example 8 with BytesMessage

use of javax.jms.BytesMessage in project camel by apache.

the class JmsBinding method extractBodyFromJms.

/**
     * Extracts the body from the JMS message
     *
     * @param exchange the exchange
     * @param message  the message to extract its body
     * @return the body, can be <tt>null</tt>
     */
public Object extractBodyFromJms(Exchange exchange, Message message) {
    try {
        // if we are configured to not map the jms message then return it as body
        if (!mapJmsMessage) {
            LOG.trace("Option map JMS message is false so using JMS message as body: {}", message);
            return message;
        }
        if (message instanceof ObjectMessage) {
            LOG.trace("Extracting body as a ObjectMessage from JMS message: {}", message);
            ObjectMessage objectMessage = (ObjectMessage) message;
            Object payload = objectMessage.getObject();
            if (payload instanceof DefaultExchangeHolder) {
                DefaultExchangeHolder holder = (DefaultExchangeHolder) payload;
                DefaultExchangeHolder.unmarshal(exchange, holder);
                return exchange.getIn().getBody();
            } else {
                return objectMessage.getObject();
            }
        } else if (message instanceof TextMessage) {
            LOG.trace("Extracting body as a TextMessage from JMS message: {}", message);
            TextMessage textMessage = (TextMessage) message;
            return textMessage.getText();
        } else if (message instanceof MapMessage) {
            LOG.trace("Extracting body as a MapMessage from JMS message: {}", message);
            return createMapFromMapMessage((MapMessage) message);
        } else if (message instanceof BytesMessage) {
            LOG.trace("Extracting body as a BytesMessage from JMS message: {}", message);
            return createByteArrayFromBytesMessage((BytesMessage) message);
        } else if (message instanceof StreamMessage) {
            LOG.trace("Extracting body as a StreamMessage from JMS message: {}", message);
            return message;
        } else {
            return null;
        }
    } catch (JMSException e) {
        throw new RuntimeCamelException("Failed to extract body due to: " + e + ". Message: " + message, e);
    }
}
Also used : DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) JMSException(javax.jms.JMSException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) TextMessage(javax.jms.TextMessage)

Example 9 with BytesMessage

use of javax.jms.BytesMessage in project ACS by ACS-Community.

the class DefaultPublisherImpl method createBytesMessage.

/**
   * Method createBytesMessage
   *
   *
   * @return BytesMessage
   *
   * @throws JMSException
   *
   */
public BytesMessage createBytesMessage() throws JMSException {
    if (closed) {
        throw (new JMSException("Publisher object has been closed"));
    }
    BytesMessage message = null;
    message = session.createBytesMessage();
    return message;
}
Also used : JMSException(javax.jms.JMSException) BytesMessage(javax.jms.BytesMessage)

Example 10 with BytesMessage

use of javax.jms.BytesMessage in project jmeter by apache.

the class SubscriberSampler method extractContent.

private void extractContent(StringBuilder buffer, StringBuilder propBuffer, Message msg, boolean isLast) {
    if (msg != null) {
        try {
            if (msg instanceof TextMessage) {
                buffer.append(((TextMessage) msg).getText());
            } else if (msg instanceof ObjectMessage) {
                ObjectMessage objectMessage = (ObjectMessage) msg;
                if (objectMessage.getObject() != null) {
                    buffer.append(objectMessage.getObject().getClass());
                } else {
                    buffer.append("object is null");
                }
            } else if (msg instanceof BytesMessage) {
                BytesMessage bytesMessage = (BytesMessage) msg;
                buffer.append(bytesMessage.getBodyLength() + " bytes received in BytesMessage");
            } else if (msg instanceof MapMessage) {
                MapMessage mapm = (MapMessage) msg;
                // MapNames are Strings
                @SuppressWarnings("unchecked") Enumeration<String> enumb = mapm.getMapNames();
                while (enumb.hasMoreElements()) {
                    String name = enumb.nextElement();
                    Object obj = mapm.getObject(name);
                    buffer.append(name);
                    buffer.append(",");
                    buffer.append(obj.getClass().getCanonicalName());
                    buffer.append(",");
                    buffer.append(obj);
                    buffer.append("\n");
                }
            }
            Utils.messageProperties(propBuffer, msg);
            if (!isLast && !StringUtils.isEmpty(separator)) {
                propBuffer.append(separator);
                buffer.append(separator);
            }
        } catch (JMSException e) {
            log.error(e.getMessage());
        }
    }
}
Also used : Enumeration(java.util.Enumeration) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) TextMessage(javax.jms.TextMessage)

Aggregations

BytesMessage (javax.jms.BytesMessage)25 JMSException (javax.jms.JMSException)10 TextMessage (javax.jms.TextMessage)9 MapMessage (javax.jms.MapMessage)8 ObjectMessage (javax.jms.ObjectMessage)7 Test (org.junit.Test)7 StreamMessage (javax.jms.StreamMessage)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Map (java.util.Map)3 Message (javax.jms.Message)3 MessageFormatException (javax.jms.MessageFormatException)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Serializable (java.io.Serializable)2 Enumeration (java.util.Enumeration)2 HashMap (java.util.HashMap)2 Session (javax.jms.Session)2 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2 DefaultExchangeHolder (org.apache.camel.impl.DefaultExchangeHolder)2