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