简体   繁体   English

JMS 消息大小

[英]JMS message size

I'm currently working on bandwidth limiting feature (don't ask me why, its not my decision) for application which use JMS (Spring framework JMS and Active MQ namely) to sending messages with payload between server and clients.我目前正在为使用 JMS(即 Spring 框架 JMS 和 Active MQ)在服务器和客户端之间发送带有有效负载的消息的应用程序开发带宽限制功能(不要问我为什么,这不是我的决定)。

I found lot of throttling methods to limit incoming JMS messages (but none of them based on actual bandwidth load), however I didn't find any possible way to limit outgoing message flow.我发现了很多限制传入 JMS 消息的限制方法(但没有一个基于实际带宽负载),但是我没有找到任何可能的方法来限制传出消息流。 So I decided to write Leaky bucket algorithm on my own.所以我决定自己写漏桶算法

Is there some way how to obtain size of JMS message?有什么方法可以获取 JMS 消息的大小吗? Other than 'sizeof' implementation in Java ( In Java, what is the best way to determine the size of an object? )除了 Java 中的“sizeof”实现( 在 Java 中,确定 object 的大小的最佳方法是什么?

I do not think that you have any seriously better alternative to determine the JMS message size than measuring of its serialized size.我认为您没有比测量其序列化大小更好的方法来确定 JMS 消息大小。

But you can add some optimizations if you want.但是您可以根据需要添加一些优化。 There are several types of messages (eg MapMessage, ObjectMessage, TextMessage).有几种类型的消息(例如 MapMessage、ObjectMessage、TextMessage)。

The size of text message is the length of its text.文本消息的大小是其文本的长度。 The size of map message is the total size of all its fields. map 消息的大小是其所有字段的总大小。 The fields are primitives or java.util.Date, so it is not a problem to measure them.这些字段是原语或 java.util.Date,因此测量它们不是问题。 Object message contains serializable object, so you can measure its size by writing to ByteOutputStream. Object 消息包含可序列化的 object,因此您可以通过写入 ByteOutputStream 来测量其大小。

I think that implementation of leaky bucket using JMS may be simplified if you are using hidden feature of most JMS providers to send delayed messages.如果您使用大多数 JMS 提供程序的隐藏功能来发送延迟消息,我认为使用 JMS 实现漏桶可能会被简化。 You can measure the message when enqueueing it and decide when do you want the subscriber to receive it.您可以在将消息排入队列时对其进行测量,并决定您希望订阅者何时接收它。 Please read here for details of how to send delayed messages: http://alexradzin.blogspot.com/2010/10/send-delayed-jms-messages.html有关如何发送延迟消息的详细信息,请阅读此处: http://alexradzin.blogspot.com/2010/10/send-delayed-jms-messages.html

Because JMS messages are serialized in sending process, the best way how to obtain size of message is through ObjectOutputStream .由于 JMS 消息在发送过程中是序列化的,因此获取消息大小的最佳方法是通过 ObjectOutputStream

private int getMessageSizeInBytes(MessageWrapper message) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(message);
    oos.close();
    return baos.size();
}

In addition to the earlier comment by @AlexR, BytesMessage has a getBodyLength() method除了@AlexR 之前的评论,BytesMessage 还有一个 getBodyLength() 方法

http://download.oracle.com/javaee/1.4/api/javax/jms/BytesMessage.html#getBodyLength http://download.oracle.com/javaee/1.4/api/javax/jms/BytesMessage.html#getBodyLength

and you can probably estimate typical header size in transit by capturing a few objects created using Session.createMessage() - ie the JMS message type with no payload.您可以通过捕获使用 Session.createMessage() 创建的一些对象来估计传输中典型的 header 大小 - 即没有有效负载的 JMS 消息类型。 I think i'd be tempted to work directly with a payload in a byte[], using a BytesMessage, and compress via a ZipOutputStream if bandwidth is critical.我想我很想直接使用 byte[] 中的有效负载,使用 BytesMessage,如果带宽很关键,则通过 ZipOutputStream 进行压缩。 In addition, JMS allows hints to suppress message timestamp and message id, which may help reduce message size eg此外,JMS 允许提示抑制消息时间戳和消息 ID,这可能有助于减少消息大小,例如

http://download.oracle.com/javaee/1.4/api/javax/jms/MessageProducer.html#setDisableMessageTimestamp%28boolean%29 http://download.oracle.com/javaee/1.4/api/javax/jms/MessageProducer.html#setDisableMessageTimestamp%28boolean%29

although providers are not required to support it,尽管不要求提供者支持它,

If the JMS provider accepts this hint, these messages must have the message ID set to null;如果 JMS 提供者接受此提示,则这些消息的消息 ID 必须设置为 null; if the provider ignores the hint, the message ID must be set to its normal unique value如果提供者忽略提示,则消息 ID 必须设置为其正常的唯一值

I once worked with very bandwidth-limited JMS on WebSphere MQ everyplace, and it was possible to get the message size pretty small in this way - although the native wireframe format was also optimised for size in that case我曾经在 WebSphere MQ 各处使用带宽非常有限的 JMS,并且可以通过这种方式获得非常小的消息大小 - 尽管在这种情况下本机线框格式也针对大小进行了优化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM