Search in sources :

Example 1 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 2 with BytesMessage

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

the class MappingJackson2MessageConverter method mapToBytesMessage.

/**
	 * Map the given object to a {@link BytesMessage}.
	 * @param object the object to be mapped
	 * @param session current JMS session
	 * @param objectWriter the writer to use
	 * @return the resulting message
	 * @throws JMSException if thrown by JMS methods
	 * @throws IOException in case of I/O errors
	 * @since 4.3
	 * @see Session#createBytesMessage
	 */
protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
    OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding);
    objectWriter.writeValue(writer, object);
    BytesMessage message = session.createBytesMessage();
    message.writeBytes(bos.toByteArray());
    if (this.encodingPropertyName != null) {
        message.setStringProperty(this.encodingPropertyName, this.encoding);
    }
    return message;
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) BytesMessage(javax.jms.BytesMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with BytesMessage

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

the class MarshallingMessageConverter method marshalToBytesMessage.

/**
	 * Marshal the given object to a {@link BytesMessage}.
	 * @param object the object to be marshalled
	 * @param session current JMS session
	 * @param marshaller the marshaller to use
	 * @return the resulting message
	 * @throws JMSException if thrown by JMS methods
	 * @throws IOException in case of I/O errors
	 * @throws XmlMappingException in case of OXM mapping errors
	 * @see Session#createBytesMessage
	 * @see Marshaller#marshal(Object, Result)
	 */
protected BytesMessage marshalToBytesMessage(Object object, Session session, Marshaller marshaller) throws JMSException, IOException, XmlMappingException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
    StreamResult streamResult = new StreamResult(bos);
    marshaller.marshal(object, streamResult);
    BytesMessage message = session.createBytesMessage();
    message.writeBytes(bos.toByteArray());
    return message;
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) BytesMessage(javax.jms.BytesMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 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 5 with BytesMessage

use of javax.jms.BytesMessage in project qpid-broker-j by apache.

the class MessageCreator method createMessage.

private static Message createMessage(final MessageDescription messageDescription, final Session session) throws Exception {
    Message message;
    try {
        switch(messageDescription.getMessageType()) {
            case MESSAGE:
                message = session.createTextMessage();
                break;
            case BYTES_MESSAGE:
                message = session.createBytesMessage();
                ((BytesMessage) message).writeBytes(((byte[]) messageDescription.getContent()));
                break;
            case MAP_MESSAGE:
                message = session.createMapMessage();
                for (Map.Entry<String, Object> entry : ((Map<String, Object>) messageDescription.getContent()).entrySet()) {
                    ((MapMessage) message).setObject(entry.getKey(), entry.getValue());
                }
                break;
            case OBJECT_MESSAGE:
                message = session.createObjectMessage((Serializable) messageDescription.getContent());
                break;
            case STREAM_MESSAGE:
                message = session.createStreamMessage();
                for (Object item : (Collection<?>) messageDescription.getContent()) {
                    ((StreamMessage) message).writeObject(item);
                }
                break;
            case TEXT_MESSAGE:
                message = session.createTextMessage(messageDescription.getContent().toString());
                break;
            default:
                throw new RuntimeException(String.format("unexpected message type '%s'", messageDescription.getMessageType()));
        }
    } catch (ClassCastException e) {
        throw new RuntimeException(String.format("Could not create message of type '%s' with this body: %s", messageDescription.getMessageType(), messageDescription.getContent().toString()), e);
    }
    return message;
}
Also used : Serializable(java.io.Serializable) StreamMessage(javax.jms.StreamMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) MapMessage(javax.jms.MapMessage) Collection(java.util.Collection) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) Map(java.util.Map) HashMap(java.util.HashMap)

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