Search in sources :

Example 1 with MessageFormatException

use of javax.jms.MessageFormatException in project qpid by apache.

the class AMQPMessage method getList.

/**
     * JMS QMF returns amqp/list types as a BytesMessage this method decodes that into a java.util.List
     * <p>
     * Taken from Gordon Sim's initial JMS QMF Example using the BBDecoder
     * <p>
     * Trivia: This block of code from Gordon Sim is the seed that spawned the whole of this Java QMF2 API
     * implementation - cheers Gordon.
     *
     * @param message amqp/list encoded JMS Message
     * @return a java.util.List decoded from Message
     */
@SuppressWarnings("unchecked")
public static <T> List<T> getList(final Message message) throws JMSException {
    if (message == null) {
        throw new MessageFormatException("Attempting to do AMQPMessage.getList() on null Message");
    } else if (message instanceof BytesMessage) {
        BytesMessage msg = (BytesMessage) message;
        //only handles responses up to 2^31-1 bytes long
        byte[] data = new byte[(int) msg.getBodyLength()];
        msg.readBytes(data);
        BBDecoder decoder = new BBDecoder();
        decoder.init(ByteBuffer.wrap(data));
        return (List<T>) decoder.readList();
    } else if (message instanceof MapMessage) {
        /*
             * In Qpid version 0.20 instead of exposing amqp/list as a BytesMessage as above rather it is exposed
             * as a MapMessage!!??? the Object Keys are the indices into the List. We create a java.util.List
             * out of this by iterating through the getMapNames() Enumeration and copying the Objects into the List.
             * This amount of copying doesn't feel healthy and we can't even work out the capacity for the List
             * a priori, but I'm not sure of a better way at present. I can't say I much like how amqp/list or indeed
             * amqp/map are currently encoded. I'd *much* prefer to see them exposed as JMS ObjectMessage.
             */
        MapMessage msg = (MapMessage) message;
        // Initial capacity of 50, can we better estimate this?
        List resultList = new ArrayList(50);
        for (Enumeration e = msg.getMapNames(); e.hasMoreElements(); ) {
            String key = (String) e.nextElement();
            resultList.add(msg.getObject(key));
        }
        return resultList;
    } else {
        return null;
    }
}
Also used : MessageFormatException(javax.jms.MessageFormatException) Enumeration(java.util.Enumeration) MapMessage(javax.jms.MapMessage) ArrayList(java.util.ArrayList) BytesMessage(javax.jms.BytesMessage) ArrayList(java.util.ArrayList) List(java.util.List) BBDecoder(org.apache.qpid.transport.codec.BBDecoder)

Example 2 with MessageFormatException

use of javax.jms.MessageFormatException 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 3 with MessageFormatException

use of javax.jms.MessageFormatException 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

BytesMessage (javax.jms.BytesMessage)3 MapMessage (javax.jms.MapMessage)3 MessageFormatException (javax.jms.MessageFormatException)3 Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JMSException (javax.jms.JMSException)2 ObjectMessage (javax.jms.ObjectMessage)2 TextMessage (javax.jms.TextMessage)2 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)2 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 List (java.util.List)1 Map (org.apache.camel.component.jms.JmsMessageType.Map)1 BBDecoder (org.apache.qpid.transport.codec.BBDecoder)1