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 {
// based on message type
if (endpoint != null && endpoint.getMessageConverter() != null) {
if (LOG.isTraceEnabled()) {
LOG.trace("Extracting body using a custom MessageConverter: {} from JMS message: {}", endpoint.getMessageConverter(), message);
}
return endpoint.getMessageConverter().fromMessage(message);
}
// if we are configured to not map the jms message then return it as body
if (endpoint != null && !endpoint.getConfiguration().isMapJmsMessage()) {
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 MessageTest method getJmsBytesMessage.
@Test
public void getJmsBytesMessage() throws Exception {
final byte[] content = new byte[512];
IntStream.range(0, content.length).forEachOrdered(i -> content[i] = (byte) (i % 256));
Connection connection = getConnection();
try {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(session.createQueue(QUEUE_NAME));
BytesMessage message = session.createBytesMessage();
message.writeBytes(content);
producer.send(message);
} finally {
connection.close();
}
List<Map<String, Object>> messages = getHelper().postJson("queue/myqueue/getMessageInfo", Collections.singletonMap("includeHeaders", Boolean.TRUE), LIST_MAP_TYPE_REF, SC_OK);
assertThat(messages.size(), is(equalTo(1)));
Map<String, Object> message = messages.get(0);
int messageId = (int) message.get("id");
byte[] receivedContent = getHelper().getBytes(String.format("queue/myqueue/getMessageContent?messageId=%d", messageId));
assumeThat("AMQP1.0 messages return the AMQP type", getProtocol(), is(not(equalTo(Protocol.AMQP_1_0))));
assertThat(receivedContent, is(equalTo(content)));
}
use of javax.jms.BytesMessage in project qpid-broker-j by apache.
the class BDBAMQP10V0UpgradeTest method testRecoverAmqpV0Message.
public void testRecoverAmqpV0Message() throws Exception {
Connection connection = getConnection();
connection.start();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Queue queue = createTestQueue(session, "queue");
MessageConsumer consumer = session.createConsumer(queue);
Message message = consumer.receive(getReceiveTimeout());
assertNotNull("Recovered message not received", message);
assertTrue(message instanceof BytesMessage);
BytesMessage bytesMessage = ((BytesMessage) message);
long length = bytesMessage.getBodyLength();
String expectedContentHash = message.getStringProperty("sha256hash");
byte[] content = new byte[(int) length];
bytesMessage.readBytes(content);
assertEquals("Unexpected content length", EXPECTED_MESSAGE_LENGTH, length);
assertNotNull("Message should carry expectedShaHash property", expectedContentHash);
String contentHash = computeContentHash(content);
assertEquals("Unexpected content hash", expectedContentHash, contentHash);
session.commit();
}
use of javax.jms.BytesMessage in project qpid-broker-j by apache.
the class BytesMessageTest method sendAndReceiveEmpty.
@Test
public void sendAndReceiveEmpty() throws Exception {
Queue queue = createQueue(getTestName());
Connection connection = getConnection();
try {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
BytesMessage message = session.createBytesMessage();
producer.send(message);
MessageConsumer consumer = session.createConsumer(queue);
connection.start();
Message receivedMessage = consumer.receive(getReceiveTimeout());
assertTrue("BytesMessage should be received", receivedMessage instanceof BytesMessage);
assertEquals("Unexpected body length", 0, ((BytesMessage) receivedMessage).getBodyLength());
} finally {
connection.close();
}
}
use of javax.jms.BytesMessage in project qpid-broker-j by apache.
the class ProducerFlowControlTest method nextMessage.
private Message nextMessage(int msg, Session producerSession) throws JMSException {
BytesMessage send = producerSession.createBytesMessage();
send.writeBytes(BYTE_300);
send.setIntProperty("msg", msg);
return send;
}
Aggregations