use of javax.jms.BytesMessage in project spring-framework by spring-projects.
the class MessageListenerAdapterTests method testWithMessageContentsDelegateForBytesMessage.
@Test
public void testWithMessageContentsDelegateForBytesMessage() throws Exception {
BytesMessage bytesMessage = mock(BytesMessage.class);
// BytesMessage contents must be unwrapped...
given(bytesMessage.getBodyLength()).willReturn(new Long(TEXT.getBytes().length));
given(bytesMessage.readBytes(any(byte[].class))).willAnswer(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
byte[] bytes = (byte[]) invocation.getArguments()[0];
ByteArrayInputStream inputStream = new ByteArrayInputStream(TEXT.getBytes());
return inputStream.read(bytes);
}
});
MessageContentsDelegate delegate = mock(MessageContentsDelegate.class);
MessageListenerAdapter adapter = new MessageListenerAdapter(delegate);
adapter.onMessage(bytesMessage);
verify(delegate).handleMessage(TEXT.getBytes());
}
use of javax.jms.BytesMessage in project spring-framework by spring-projects.
the class SimpleMessageConverterTests method testByteArrayConversion.
@Test
public void testByteArrayConversion() throws JMSException {
Session session = mock(Session.class);
BytesMessage message = mock(BytesMessage.class);
byte[] content = "test".getBytes();
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content);
given(session.createBytesMessage()).willReturn(message);
given(message.getBodyLength()).willReturn((long) content.length);
given(message.readBytes(any(byte[].class))).willAnswer(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return byteArrayInputStream.read((byte[]) invocation.getArguments()[0]);
}
});
SimpleMessageConverter converter = new SimpleMessageConverter();
Message msg = converter.toMessage(content, session);
assertEquals(content.length, ((byte[]) converter.fromMessage(msg)).length);
verify(message).writeBytes(content);
}
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 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 jmeter by apache.
the class SubscriberSampler method extractContent.
private void extractContent(StringBuilder buffer, StringBuilder propBuffer, Message msg, boolean isLast) {
if (msg != null) {
try {
if (msg instanceof TextMessage) {
buffer.append(((TextMessage) msg).getText());
} else if (msg instanceof ObjectMessage) {
ObjectMessage objectMessage = (ObjectMessage) msg;
if (objectMessage.getObject() != null) {
buffer.append(objectMessage.getObject().getClass());
} else {
buffer.append("object is null");
}
} else if (msg instanceof BytesMessage) {
BytesMessage bytesMessage = (BytesMessage) msg;
buffer.append(bytesMessage.getBodyLength() + " bytes received in BytesMessage");
} else if (msg instanceof MapMessage) {
MapMessage mapm = (MapMessage) msg;
// MapNames are Strings
@SuppressWarnings("unchecked") Enumeration<String> enumb = mapm.getMapNames();
while (enumb.hasMoreElements()) {
String name = enumb.nextElement();
Object obj = mapm.getObject(name);
buffer.append(name);
buffer.append(",");
buffer.append(obj.getClass().getCanonicalName());
buffer.append(",");
buffer.append(obj);
buffer.append("\n");
}
}
Utils.messageProperties(propBuffer, msg);
if (!isLast && !StringUtils.isEmpty(separator)) {
propBuffer.append(separator);
buffer.append(separator);
}
} catch (JMSException e) {
log.error(e.getMessage());
}
}
}
Aggregations