powered by Jive Software

Simple send seems slow

I am new and I create a simple login and send a message app. It seems to be taking a second or more to send a message, this seems a little slow. Is this normal?

Here is my code:

// Create connection to jabber server.

connection = new XMPPConnection( JABBER_SERVER, JABBER_PORT );

// login with send user

connection.login( USER, PASSWORD, LOCATION );

// Wait for login to finish authentication.

int count = 0;

while ( !connection.isAuthenticated() || count < 100 ) {

try{ Thread.sleep(3); } catch ( Exception e ){}

count++;

}

// Create a Message of type: NORMAL

Message mesg = new Message( jabber_id, Message.Type.NORMAL );

// Create a chat session.

Chat chat = connection.createChat( jabber_id );

mesg.setSubject( subject );

mesg.setBody( message );

chat.sendMessage( mesg );

Here are some numbers:

Time elapsed: 616949 milliseconds

Number of messages sent: 500

Does anyone have any ideas?

Hey Kombi,

Have you profiled your application? Do you know where is the bottleneck? Which operation is taking too long? If you don’'t have a profiler, you may want to add to your sample code some System.out.println to print the timestamp and detect where is the problem.

Regards,

– Gato

It seems to be taking 5 seconds to login/authenticate/connect

The isConnected and isAuthenticated returns false after login…

I put a while loop to wait on connection to finish authentication…

Do you get kicked after you send a message?

Are you using a local server or a remote server? If you are using a remote server then the connection might be slow or the server very busy. Smack will wait for a few seconds for a response from the server. If no response was received then an exception is raised.

BTW, the #login() method will answer an exception if there was some problem login into the server. Otherwise, the connection will be authenticated. Therefore, you don’'t need the while loop you are using.

I don’'t think this is a Smack problem you are seeing but a connection problem or a busy server. You may want to use a profiler or debug the application to detect exactly where the time is being spent.

Regards,

– Gato

Time is being spent on re-login b/c I am checking if connection isConnected and isAuthenticated everytime i send a message.

If I remove the loop, then i lose the messages being sent.

It seems to be taking 5 seconds to

login/authenticate/connect

This could be due to a synchronization issue in the code that was reported earlier. I’'ll try to verify…

Thanks,

Matt

Any idea?

Matt,

Did you get a chance to look into this issue?

Matt,

Did you get a chance to check this out? I need an answer.

Try removing the check for isConnected and isAuthenticated everytime you send a message.

They shouldnt be needed as the sendMessage method will throw an exception if the connection has closed/you’‘re not logged in (which you’'ll need to catch).

Out of curisoity, why do you need to send a lot of messages so fast?

I have a patch that solve the slow login problem.

The problem is the following:

in the login process, there is a wait() that waits for the server to set the connection id

The problem is though that it can often happen that notify is called befor wait is called, causing

the login to wait until it times out, which happens to be five seconds by default.

That means on fast servers, this method always times out.

Use and enjoy,

andy

here is the function you have to replace in PacketReader.java

public void startup() throws XMPPException {

/* readerThread.start();

listenerThread.start();

// Wait for stream tag before returing. We’'ll wait a couple of seconds before

// giving up and throwing an error.

try {

synchronized(connectionIDLock) {

connectionIDLock.wait(SmackConfiguration.getPacketReplyTimeout());

}

}

catch (InterruptedException ie) { }

if (connectionID == null) {

throw new XMPPException(“Connection failed. No response from server.”);

}

else {

connection.connectionID = connectionID;

}*/

Thread startupThread = new Thread() {

public void run() {

// Wait for stream tag before returing. We’'ll wait a couple of seconds before

// giving up and throwing an error.

try {

synchronized(connectionIDLock) {

connectionIDLock.wait(SmackConfiguration.getPacketReplyTimeout());

}

}

catch (InterruptedException ie) { }

}

};

//first start the thread that waits

startupThread.start();

// then start the thread that will notify

readerThread.start();

//System.out.println(“readerThread strart”);

listenerThread.start();

// System.out.println(“listenerThread strart”);

try

{

//finally, wait for the startup thread to finish, and then all is well

startupThread.join();

}catch(Exception e)

{

e.printStackTrace();

}

if (connectionID == null) {

System.out.println(“connection failed”);

throw new XMPPException(“Connection failed. No response from server.”);

}

else {

connection.connectionID = connectionID;

}

}