powered by Jive Software

Smack creates a new chat after sending a number of messages

Hi all,

I’m using Smack 3.2 with ejabberd. The first client sends messages continuously to the second. For the second client, I attached a chat listener to discover the first created chat, assign a message listener, and then removing this chat listener. The problem is, after successfully receiving a number of messages the assigned message listener stops working while I can see messages still coming in Smack debugger. I found that if I dont remove the chat listener, client 2 message listener works all the time, because what happens is that client 1 keeps creating a new chat after sending a number of messages. Is this an expected behaviour? Please help.

Here is my code:

Sending:

Chat chat= conn1.getChatManager().createChat(jid2, null);

for(int i=0; i<100; i++){

chat.sendMessage(msg);

Thread.sleep(100);

}

Receiving:

conn2.getChatManager().addChatListener(new ChatManagerListener(){

public void chatCreated(Chat chat, boolean createdLocally)

{

int t=0;

if(!createdLocally){

System.out.println("chat created by " + chat.getParticipant());

chat.addMessageListener(new MessageListener(){

public void processMessage(Chat chat, Message msg){

System.out.println(msg.getBody());

}

});

t=1;

}

if(t==1)

conn.getChatManager().removeChatListener(this);

}

});

Since you are creating a new chat every 100 messages (I assume from your description), the receiver will do the same (they will have a different thead id). When you remove the chat listener after the first chat is created, you are no longer listening for those new chats being created, and your message listener is only attached to the first one.

When you leave the chat listener, you will always create a new message listener for each new chat that is created. Be aware that are still creating a new message listener though, not reusing the same one for every chat.

Thanks for your reply, but im not creating a new listener for each 100 messages, sorry that i didnt make it clear. I cant receive all of those 100 messages in one chat. After sending like 80 of them ( different number each run), another chat is created (discovered by client’s 2 chat listener) and the first message listener stops working. I can keep the chat listener running to catch all subsequent chats, but it is undesirable as the number of chats gets bigger when i try to send more messages. I dont know if it could be a server side problem? Please help i have been stuck for a whole week.