I maintain a system which uses Apache QPID as message interchange middleware. Its clients are mostly written in Java, using JMS as the abstraction layer in Java for QPID.
On the other hand, I usually use a client, also written in Java using the same technology set, which I use to listen to some EXCHANGE/TOPICS as monitoring tool.
Everything is working perfectly but I am intrigued by the fact that the order of the message elements varies for exactly messages containing exactly the same information and, what is more, generated using the same algorithm.
As you can see in the fragment of code below, I iterate over the JMS MapMessage
properties, inserting each element into a HashMap which will be iterated to insert the tree elements of the screenshot.
Map<String,Object> c = new HashMap<>();
MapMessage mm = (MapMessage)msg;
Enumeration props = mm.getMapNames();
while(props.hasMoreElements())
{
String key = (String)props.nextElement();
Object value = mm.getObject(key); //BREAK POINT to check key value.
c.put(key, value);
}
I know that HashMap doesn't necessarily store its items ordered but I tend to think that for the equal keysets inserted following the same order you should get equivalent iterations (concerning order) over its elements. I may be wrong.
Anyway, I've debugged the application at the point where props
its being traversed and I can see differences in the order too.
This doesn't affect system at all but I wonder:
- If two HashMaps filled with the same keys whose elements has been inserted in the same order generate the same iteration order over its keys. As I wrote above, I tend to think that they do.
- If QPID changes the order of its messages elements to make some kind of alignnment and thus optimize transmission. Or may JMS layer change this order?
EDIT
I thought the first part of this question was worth of its own question: Is the order of HashMap elements reproducible?
It seems that HashMaps should not change but it is not guaranteed at all and it can change at the very fist moment HashMap implementation changes.
Now, this question should be centered on the QPID side.