Chatting too fast makes the chat thread listener breaks?

Hi I’m currently making chatting application using swing and smack.

Here’s how I put the MessageListener

.
.
final ChatMessageListener cetingListener = new ChatMessageListener(); public void rosterTreeInitiate()
{
        copyListRosterGroup();
                Collection<RosterEntry> entries = roster.getEntries();
        for (RosterEntry entry : entries)
        {
            listRosterUser.add(entry.getName());
                        chatManager.createChat(entry.getUser(), entry.getName(), cetingListener);
        }
.
.
.
.
}

Here’s the MessageListener

class ChatMessageListener implements MessageListener
{        @Override
    public void processMessage(final Chat chat, final Message message) {
        System.out.println("From: " + message.getFrom() + " | pesan: " + message.getBody());
                final Runnable runnable = new Runnable() {
            public void run() {
                System.out.println("process runnable: " + Thread.currentThread().getName());
                  MainController.getInstance().getRosterBox().getMessageBox().putMessage(StringUtils.parseName(message.getFrom()),message.getBody());
            }
        };
        SwingUtilities.invokeLater(runnable);
                System.out.println("process message end");
    }
}

and here’s how I send the message

.
.
.
.
     try {
                        Chat chat = mainController.getRosterBox().getUserChat(userName);
                                                if (chat != null)
                        chat.sendMessage(pesan);
                                                else {                             System.out.println("chat null");   // This is the chat null message come from
                            }
                    } catch (XMPPException e) {
                        System.out.println("fail to send message");
                    }
.
.
.
.
.

I’m confused because whenever I chatted too fast or chat with more than one user, the chat thread died so the message stopped coming and I got “chat null” in my console. After that I cannot receive any message from the participant. I found out that when the “chat null” appears, smack create new chat thread with random ID. I checked it using ChatManagerListener.

My question is how to prevent the Chat thread died?

Thanks

hum… Why do you use a single ChatListener for each instance of your Chat ? Personnaly i used one instance for one Chat at the begining of my project and i don’t have your problem. Try this way …

If your problem persist i see two other solutions which could more efficient :

  • first : look at the PacketCollector classes (in smack javadoc) which allow you to keep every message in a queue, and to read then synchronously.

  • second : make your own QueueManager in using concurrent classes of java like : LinkedBlockingQueue<>.

This is the solution i’m using now because it’s offer the best integrity and rapidity for messages managing. I’m paste you bellow an example of my code source (a short example) :

public void addChat(Chat chat){

chat.addMessageListener(new MessageListener(){

public void processMessage(Chat chat, Message message){

QueueChatMessage.addMessageMessage(message);

}

};

}

public class QueueChatMessage implements Runnable{

private static LinkedBlockingQueue fileMessage = new LinkedBlockingQueue();

/***/

public void run(){

Message message;

while(isWorking){

try{

message = fileMessage.take();

//Your traitment

}catch (InterruptedException ex){

/exception traitment/

}

}

}

public void addMessage(Message msg){

fileMessage.add(msg);

}

public void addChat(Chat chat){

chat.addMessageListener(new MessageListener(){

public void processMessage(Chat chat, Message message){

QueueChatMessage.addMessageMessage(message);

}

};

}

public class QueueChatMessage implements Runnable{

private static LinkedBlockingQueue fileMessage = new LinkedBlockingQueue();

/***/

public void run(){

Message message;

while(isWorking){

try{

message = fileMessage.take();

//Your traitment

}catch (InterruptedException ex){

/exception traitment/

}

}

}

public void addMessage(Message msg){

fileMessage.add(msg);

}

I hope my little advice will help you. If you have questions don’t hesitate!

1 Like

Hi, thanks a lot for your guidance. I’ve stuck by this matter for week so I really appreciate your post. I now use packetlistener to gather messages because I’m confused how to use packetcollector(and dunno why it sometimes stuck the GUI).

The reason I use chatlistener is because that’s what I saw in the smack tutorial, I don’t know I can use packetcollector and packetlistener to gather messages.

So far I have no problem receiving and sending messages, I can even open two instances of my chat messenger now (before I got xmpp error for opening more than one connection).

If I encounter similiar trouble, I’ll try using your queue method. I’m new to java, I don’t even know java has feature like that

Ok I’m happy that my guidance help you .

For your matter with the stuck fo the GUI when using packetcollector it’s maybe because you launch the runnable with :

SwingUtilities.invokeLater(yourRunnable);

This methode use the GUI thread to make an action so it could block the GUI.

Personnaly i not use this method but i use that :

Thread myThread = new Thread(yourRunnable);

myThread.start();

Try this if you have some other problem with the running of the GUI …

For the packetlistener, it receive each type of message sending by your server XMPP to your client. So you catch them all with this methode. The only matter is that processPacket using one only thread so if you have a lot of different message arriving in your processPacket methode some packet could be lost. If your encounter this problem use packetCollector or queuemethode .