Bug Report: Error when broadcasting to roster

I think I found a misbehavior of the smack lib, V. 1.3. I am trying to do a broadcast over all contacts in my roster. But if I do not sleep for a while in the loop, smack will only send the messages (the number of messages is correct!) to the first recipient. Example: agent1 & agent2 are in my roster (yes, I use smack to build a distributed system…)

then only e.g. agent2 will get broadcasted messages; if I insert a “Thread.sleep(500)” in the messaging loop, smack behaves as expected. Sounds like a concurrency thing…

The Method is attached as a file.
bugreport.txt (978 Bytes)

This is actually a simple issue and I wouldn’‘t call it a Smack bug. You have a single Message object that you are sending again and again. Each time you say createChat and pass in the Message object, the “to” property of that message object is getting updated. You then send the packet, which means it gets queued up and then sent across the wire. Everything will work when you wait 500 ms between method calls since the Message will have enough time to wait in the queue and be sent before you modify it again. Instead of having a single Message object, you want a different Message object for each user you’‘re sending a message to. A very simple fix is to just pass in a String to your method and call createChat and then sendMessage with the String value. Smack will then manage creating a new Message object for each user you’'re sending a message to.

Regards,

Matt

I see - this is not the behavior I expected, but it seems to be reasonable to use a message queue. Anyway, I am using the smack lib to send multiple properties to the roster. So, in my messages I attach several properties and serialized data. It would not be a clean design to pass all data to the place where the message will be sent.

Is there another way to change the recipient (or create a message with the same content, but a different ID)? Sorry if I overlooked something in the javadocs.

You could try to clone() the messages before you send to each user? Also, if you’'re sending just a single message, there is no need to create Chat objects – just set the To field of the message to each recipient and then send the packet through the XMPPConnection.

Regards,

Matt