10
votes

I have found some quite conflicting information on the web and I think that each different JMS provider may also alter the answer too.

I'm trying to understand when sending XML into a JMS system (e.g. ActiveMQ) whether I should use a

  • BytesMessage : I can guarantee that the XML is serialized correctly and the preamble will match the actual encoding. Furthermore I can be sure that the client will be able to get the raw representation correctly.

  • TextMessage : There are APIs in many of the queue implementations for sending XML easily. I also understand that there are "encoding" information attached to the messages. But I risk encoding the message (and writing it's preamble) in one format and receiving it as another.

Does anyone have a definitive answer, or at least some reasons why you would choose one over the other?

1
no definite answer, sorry mate. My 2cts: XML is a text format, hence, use TextMessage. If you be sending binary data, say a zip or jpg, -then- use BytesMessage. Let stuff make sense on its own (w/o documentation). It keeps (developers/testers/maintenance) life easier. - jos
try working for a defence project and telling your manager "We aren't going to document it to make it easier...". Still I get what you are saying. Base 64 encoded UTF8 isn't too bad, without having to resort to something like MTOM. - Spence

1 Answers

5
votes

I agree with jos' comment to your question. First off, you should choose the kind of message type that best expresses the semantics of your content. Reading the TextMessage Javadoc, I'd go for that:

This message type can be used to transport text-based messages, including those with XML content.

So if you do run into trouble with your text message encoding, then there's probably some mis-configuration on the client / server side. But that shouldn't be a motivation for abusing a different message type that was not primarily intended for text transfer, such as BytesMessage.

N.B: Even with BytesMessage, you can get the encoding wrong. Imagine:

// Send that data through JMS
byte[] data1 = "source text".getBytes("ISO-8859-1");

// Receive the byte stream on the other side. Ooops
String data2 = new String(data1, "UTF-8");