JMS消息大小

JMS消息大小

问题描述:

我目前正在为使用JMS(即Spring框架JMS和Active MQ)在服务器和客户端之间发送带有有效负载的消息的应用程序开发带宽限制功能(不要问我为什么,这不是我的决定).

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消息的节流方法(但都不基于实际的带宽负载),但是我没有找到任何限制输出消息流的方法.因此,我决定自己编写漏斗算法.

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. So I decided to write Leaky bucket algorithm on my own.

是否有某种方法可以获取JMS消息的大小?除了Java中的"sizeof"实现(

Is there some way how to obtain size of JMS message? Other than 'sizeof' implementation in Java (In Java, what is the best way to determine the size of an object?)

由于JMS消息在发送过程中被序列化,因此获取消息大小的最佳方法是

Because JMS messages are serialized in sending process, the best way how to obtain size of message is through 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();
}