JMS消息大小

Sor*_*ror 7 java spring activemq-classic jms bandwidth-throttling

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

我发现很多限制方法来限制传入的JMS消息(但没有基于实际的带宽负载),但是我没有找到任何限制传出消息流的方法.所以我决定自己编写Leaky bucket算法.

有没有办法获取JMS消息的大小?除了Java中的'sizeof'实现(在Java中,确定对象大小的最佳方法是什么?)

Sor*_*ror 4

由于 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();
}
Run Code Online (Sandbox Code Playgroud)