chat.sendMessage() is asynchronous.... I need a blocking call

Sorry, I don’'t know how to format code on this forum…

XMPPConnection con

= new XMPPConnection(“jabber.org”);

// wait for isConnected() to become true or timeout and throw

con.login(“mtucker”, “password”);

// wait for isAuthenticated() to become true or timeout and throw

con.createChat("jsmith@jivesoftware.com")

.sendMessage(“Howdy!”); <-- I want this to block

// sleep???

con.close()

In this case, the message may never be sent because the connection closes too quick. I could put a sleep in here, but the sleep time would be arbitrary, and due to network and cpu loads I may be sleeping to long or not long enough. I would really like a blocking call, so that when I call con.close() I won’'t prevent the message from getting sent. I saw some other forum postings:

http://www.jivesoftware.org/forums/thread.jspa?messageID=90916&#90916

http://www.jivesoftware.org/forums/thread.jspa?messageID=77553&#77553

Both of these posts have been archived, but I don’'t see a fix/workaround as of yet.

I realize I could use a listener to see if the message is sent, but If I have multiple instances all sending the same message, I won’'t know which one arrived correctly.

Any ideas, workarounds, or a blocking method would be appreciated.

Thanks,

Matt Sargent

All you need to do is:

final XMPPConnection con = new XMPPConnection(“jabber.org”);

// wait for isConnected() to become true or timeout and throw

con.login(“mtucker”, “password”);

// wait for isAuthenticated() to become true or timeout and throw

Chat chat = con.createChat("jsmith@jivesoftware.com");

Message message = chat.createMessage();

message.setBody(“Howdy!”);

String id = message.getPacketID();

conn.addPacketWriterListener( new PacketListener() {

public void processPacket(Packet packet) {

con.close();

}

}, new PacketIDFilter(id));

chat.sendMessage(message);

/code

Note: this is only confirmation that the packet was written not that it was received on the other end the only possible solution to this matter would be adding a display message extension to the packet but even that is not a guarantee.

Thank you very much, this fixes my problem perfectly. I’'d give you a gold star for this one if I could, too bad I have no idea how.

hmmm it still does fail to be received on occasion although I’'m now sure it is being sent… I have a JUNIT test where I listen for the message as a recipient, and on occasion I get nothing.

My only suggestion to try would be to put a wait in before you close the connection, or if you are going to be controlling both ends you can setup some sort of acknowledgement system, like adding an ack to the message, but even that strictly speaking is not a fool proof solution. The problem is mostly that xmpp makes no guarantees of message delivery.

Yes, I realize this, but that puts me back at square one again, by putting in an artificial wait. Since XMPP does not guarantee delivery, and there is no built in ack, this is as close as I can get right now I suppose. It works ~90% of the time, and that is satisfactory for now.

I need something similar, but I can’'t watch for a single message to be sent across… there may be multiple messages, and I need to make sure they all get sent. I was thinking about keeping a count of messages that were supposed to go out, but that may not be terribly easy. Any ideas?

You keep a list of the message IDs you have sent and check for them in the PacketWriterListener, though instead of filtering on a specific message ID you remove it from the list when the list is at zero you know at least they have been written.