Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When sending XML to JMS should I use TextMessage or BytesMessage

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?

like image 939
Spence Avatar asked Jun 01 '12 11:06

Spence


People also ask

What is the use of textmessage object in Java?

Bookmark this question. Show activity on this post. A TextMessage object is used to send a message containing a java.lang.String. It inherits >from the Message interface and adds a text message body. This message type can be used to transport text-based messages, including those with XML >content.

How to send/receive messages in JMS?

After obtaining a Connection, you will need to open up a Session to send/receive messages. There are basically 2 main sets of interface for sending and receiving the messages that you need to learn. JMS 1.1 – Also known as Classic API, this is the legacy API. JMS 2.0 – Known as Simplified API, offers simpler API needs fewer interfaces.

What is the difference between jmsmapmessage and jmsbytesmessage?

Unlike JMSMapMessage and JMSStreamMessage, JMSBytesMessage contains only data written by the application. No additional data is stored in the message data, such as the XML tags used to define the items in a JMSMapMessage and JMSStreamMessage.

What is the format of jmstextmessage?

JMSTextMessage contains a single text string. When a text message is sent, the text Format is set to MQSTR , WMQConstants.MQFMT_STRING. The CodedCharacterSetId of the text is set to the coded character set identifier defined for its destination. The text is encoded into the CodedCharacterSetId by IBM® MQ.


1 Answers

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");
like image 150
Lukas Eder Avatar answered Nov 15 '22 19:11

Lukas Eder