当发送XML到JMS时​​,我应该使用TextMessage或BytesMessage [英] When sending XML to JMS should I use TextMessage or BytesMessage

查看:737
本文介绍了当发送XML到JMS时​​,我应该使用TextMessage或BytesMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网络上发现了一些相当矛盾的信息,我认为每个不同的JMS提供者也可能改变答案。

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

我想了解什么时候发送XML到JMS系统(例如ActiveMQ)是否应该使用

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


  • BytesMessage:我可以保证XML被序列化并且前导码将匹配实际编码。

  • 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:在许多队列实现中都有API用于发送XML容易。我也理解,有编码信息附加到消息。

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?

推荐答案

我同意 jos' 评论您的问题。首先,您应该选择最能表达您内容的语义的邮件类型。阅读 TextMessage Javadoc,我会去的:

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:


这个消息类型可以用来传输基于文本的消息,包括那些具有XML内容的。

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

因此,如果您的文本消息编码遇到麻烦,那么客户端/服务器端可能会出现一些错误配置。但是,这不应该是滥用不主要用于文本传输的不同消息类型的动机,例如 BytesMessage

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:即使使用 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");

这篇关于当发送XML到JMS时​​,我应该使用TextMessage或BytesMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆