Search in sources :

Example 11 with BytesMessage

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

the class ConsumeJmsBytesMessageTest method testConsumeBytesMessage.

@Test
public void testConsumeBytesMessage() throws Exception {
    endpoint.expectedMessageCount(1);
    jmsTemplate.setPubSubDomain(false);
    jmsTemplate.send("test.bytes", new MessageCreator() {

        public Message createMessage(Session session) throws JMSException {
            BytesMessage bytesMessage = session.createBytesMessage();
            bytesMessage.writeByte((byte) 1);
            bytesMessage.writeByte((byte) 2);
            bytesMessage.writeByte((byte) 3);
            return bytesMessage;
        }
    });
    endpoint.assertIsSatisfied();
    assertCorrectBytesReceived();
}
Also used : BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) JMSException(javax.jms.JMSException) BytesMessage(javax.jms.BytesMessage) MessageCreator(org.springframework.jms.core.MessageCreator) Session(javax.jms.Session) Test(org.junit.Test)

Example 12 with BytesMessage

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

the class JmsBinding method createJmsMessageForType.

/**
     *
     * Create the {@link Message}
     *
     * @return jmsMessage or null if the mapping was not successfully
     */
protected Message createJmsMessageForType(Exchange exchange, Object body, Map<String, Object> headers, Session session, CamelContext context, JmsMessageType type) throws JMSException {
    switch(type) {
        case Text:
            {
                TextMessage message = session.createTextMessage();
                if (body != null) {
                    String payload = context.getTypeConverter().convertTo(String.class, exchange, body);
                    message.setText(payload);
                }
                return message;
            }
        case Bytes:
            {
                BytesMessage message = session.createBytesMessage();
                if (body != null) {
                    byte[] payload = context.getTypeConverter().convertTo(byte[].class, exchange, body);
                    message.writeBytes(payload);
                }
                return message;
            }
        case Map:
            {
                MapMessage message = session.createMapMessage();
                if (body != null) {
                    Map<?, ?> payload = context.getTypeConverter().convertTo(Map.class, exchange, body);
                    populateMapMessage(message, payload, context);
                }
                return message;
            }
        case Object:
            ObjectMessage message = session.createObjectMessage();
            if (body != null) {
                try {
                    Serializable payload = context.getTypeConverter().mandatoryConvertTo(Serializable.class, exchange, body);
                    message.setObject(payload);
                } catch (NoTypeConversionAvailableException e) {
                    // cannot convert to serializable then thrown an exception to avoid sending a null message
                    JMSException cause = new MessageFormatException(e.getMessage());
                    cause.initCause(e);
                    throw cause;
                }
            }
            return message;
        default:
            break;
    }
    return null;
}
Also used : MessageFormatException(javax.jms.MessageFormatException) Serializable(java.io.Serializable) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) HashMap(java.util.HashMap) Map(java.util.Map) TextMessage(javax.jms.TextMessage)

Example 13 with BytesMessage

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

the class JmsBinding method createJmsMessageForType.

/**
     * 
     * Create the {@link Message} 
     * 
     * @return jmsMessage or null if the mapping was not successfully
     */
protected Message createJmsMessageForType(Exchange exchange, Object body, Map<String, Object> headers, Session session, CamelContext context, JmsMessageType type) throws JMSException {
    switch(type) {
        case Text:
            {
                TextMessage message = session.createTextMessage();
                if (body != null) {
                    String payload = context.getTypeConverter().convertTo(String.class, exchange, body);
                    message.setText(payload);
                }
                return message;
            }
        case Bytes:
            {
                BytesMessage message = session.createBytesMessage();
                if (body != null) {
                    byte[] payload = context.getTypeConverter().convertTo(byte[].class, exchange, body);
                    message.writeBytes(payload);
                }
                return message;
            }
        case Map:
            {
                MapMessage message = session.createMapMessage();
                if (body != null) {
                    Map<?, ?> payload = context.getTypeConverter().convertTo(Map.class, exchange, body);
                    populateMapMessage(message, payload, context);
                }
                return message;
            }
        case Object:
            ObjectMessage message = session.createObjectMessage();
            if (body != null) {
                try {
                    Serializable payload = context.getTypeConverter().mandatoryConvertTo(Serializable.class, exchange, body);
                    message.setObject(payload);
                } catch (NoTypeConversionAvailableException e) {
                    // cannot convert to serializable then thrown an exception to avoid sending a null message
                    JMSException cause = new MessageFormatException(e.getMessage());
                    cause.initCause(e);
                    throw cause;
                }
            }
            return message;
        default:
            break;
    }
    return null;
}
Also used : MessageFormatException(javax.jms.MessageFormatException) Serializable(java.io.Serializable) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) HashMap(java.util.HashMap) Map(org.apache.camel.component.jms.JmsMessageType.Map) Map(java.util.Map) TextMessage(javax.jms.TextMessage)

Example 14 with BytesMessage

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

the class JmsBinding method makeJmsMessage.

/**
     * Creates a JMS message from the Camel exchange and message
     *
     * @param exchange the current exchange
     * @param camelMessage the body to make a javax.jms.Message as
     * @param session the JMS session used to create the message
     * @param cause optional exception occurred that should be sent as reply instead of a regular body
     * @return a newly created JMS Message instance containing the
     * @throws JMSException if the message could not be created
     */
public Message makeJmsMessage(Exchange exchange, org.apache.camel.Message camelMessage, Session session, Exception cause) throws JMSException {
    Message answer = null;
    boolean alwaysCopy = endpoint != null && endpoint.getConfiguration().isAlwaysCopyMessage();
    boolean force = endpoint != null && endpoint.getConfiguration().isForceSendOriginalMessage();
    if (!alwaysCopy && camelMessage instanceof JmsMessage) {
        JmsMessage jmsMessage = (JmsMessage) camelMessage;
        if (!jmsMessage.shouldCreateNewMessage() || force) {
            answer = jmsMessage.getJmsMessage();
            if (!force) {
                // answer must match endpoint type
                JmsMessageType type = endpoint != null ? endpoint.getConfiguration().getJmsMessageType() : null;
                if (type != null && answer != null) {
                    if (type == JmsMessageType.Text) {
                        answer = answer instanceof TextMessage ? answer : null;
                    } else if (type == JmsMessageType.Bytes) {
                        answer = answer instanceof BytesMessage ? answer : null;
                    } else if (type == JmsMessageType.Map) {
                        answer = answer instanceof MapMessage ? answer : null;
                    } else if (type == JmsMessageType.Object) {
                        answer = answer instanceof ObjectMessage ? answer : null;
                    } else if (type == JmsMessageType.Stream) {
                        answer = answer instanceof StreamMessage ? answer : null;
                    }
                }
            }
        }
    }
    if (answer == null) {
        if (cause != null) {
            // an exception occurred so send it as response
            LOG.debug("Will create JmsMessage with caused exception: {}", cause);
            // create jms message containing the caused exception
            answer = createJmsMessage(cause, session);
        } else {
            ObjectHelper.notNull(camelMessage, "message");
            // create regular jms message using the camel message body
            answer = createJmsMessage(exchange, camelMessage, session, exchange.getContext());
            appendJmsProperties(answer, exchange, camelMessage);
        }
    }
    if (answer != null && messageCreatedStrategy != null) {
        messageCreatedStrategy.onMessageCreated(answer, session, exchange, null);
    }
    return answer;
}
Also used : MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) TextMessage(javax.jms.TextMessage)

Example 15 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 {
        // based on message type
        if (endpoint != null && endpoint.getMessageConverter() != null) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Extracting body using a custom MessageConverter: {} from JMS message: {}", endpoint.getMessageConverter(), message);
            }
            return endpoint.getMessageConverter().fromMessage(message);
        }
        // if we are configured to not map the jms message then return it as body
        if (endpoint != null && !endpoint.getConfiguration().isMapJmsMessage()) {
            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) Object(org.apache.camel.component.jms.JmsMessageType.Object) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) JMSException(javax.jms.JMSException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) 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