Search in sources :

Example 1 with Map

use of org.apache.camel.component.jms.JmsMessageType.Map 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)

Aggregations

Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BytesMessage (javax.jms.BytesMessage)1 JMSException (javax.jms.JMSException)1 MapMessage (javax.jms.MapMessage)1 MessageFormatException (javax.jms.MessageFormatException)1 ObjectMessage (javax.jms.ObjectMessage)1 TextMessage (javax.jms.TextMessage)1 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)1 Map (org.apache.camel.component.jms.JmsMessageType.Map)1