Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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?)

like image 328
Sorceror Avatar asked Aug 17 '11 08:08

Sorceror


2 Answers

I do not think that you have any seriously better alternative to determine the JMS message size than measuring of its serialized size.

But you can add some optimizations if you want. There are several types of messages (e.g. 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. The fields are primitives or java.util.Date, so it is not a problem to measure them. Object message contains serializable object, so you can measure its size by writing to 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. 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

like image 83
AlexR Avatar answered Oct 19 '22 04:10

AlexR


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();
}
like image 4
Sorceror Avatar answered Oct 19 '22 06:10

Sorceror