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;
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations