Search in sources :

Example 86 with BytesMessage

use of javax.jms.BytesMessage in project zipkin by openzipkin.

the class ActiveMQSpanConsumer method onMessage.

@Override
public void onMessage(Message message) {
    metrics.incrementMessages();
    // TODO: consider how to reuse buffers here
    byte[] serialized;
    try {
        if (message instanceof BytesMessage) {
            BytesMessage bytesMessage = (BytesMessage) message;
            serialized = new byte[(int) bytesMessage.getBodyLength()];
            bytesMessage.readBytes(serialized);
        } else if (message instanceof TextMessage) {
            String text = ((TextMessage) message).getText();
            serialized = text.getBytes(UTF_8);
        } else {
            metrics.incrementMessagesDropped();
            return;
        }
    } catch (Exception e) {
        metrics.incrementMessagesDropped();
        return;
    }
    metrics.incrementBytes(serialized.length);
    // lenient on empty messages
    if (serialized.length == 0)
        return;
    collector.acceptSpans(serialized, NOOP);
}
Also used : BytesMessage(javax.jms.BytesMessage) TextMessage(javax.jms.TextMessage) IOException(java.io.IOException) JMSException(javax.jms.JMSException)

Example 87 with BytesMessage

use of javax.jms.BytesMessage 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 88 with BytesMessage

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

the class AMQPMessage method createListMessage.

/**
 * Creates an amqp/list encoded Message out of a BytesMessage.
 * <p>
 * This is somewhat of a dirty hack that needs to be monitored as qpid versions change.
 * <p>
 * Unfortunately there's no "clean" way to encode or decode amqp/list messages via the JMS API.
 *
 * @param session used to create the JMS Message
 * @return an amqp/list encoded JMS Message
 */
public static Message createListMessage(final Session session) throws JMSException {
    BytesMessage message = session.createBytesMessage();
    setContentType(message, "amqp/list");
    return message;
}
Also used : BytesMessage(javax.jms.BytesMessage)

Example 89 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 90 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)

Aggregations

BytesMessage (javax.jms.BytesMessage)180 Session (javax.jms.Session)79 MessageProducer (javax.jms.MessageProducer)77 MessageConsumer (javax.jms.MessageConsumer)70 Test (org.junit.Test)59 Message (javax.jms.Message)54 TextMessage (javax.jms.TextMessage)47 Connection (javax.jms.Connection)41 Queue (javax.jms.Queue)37 JMSException (javax.jms.JMSException)34 MapMessage (javax.jms.MapMessage)29 ObjectMessage (javax.jms.ObjectMessage)26 StreamMessage (javax.jms.StreamMessage)20 HashMap (java.util.HashMap)18 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)17 Topic (javax.jms.Topic)14 Map (java.util.Map)12 ActiveMQMessage (org.apache.activemq.command.ActiveMQMessage)12 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)10 Destination (javax.jms.Destination)9