powered by Jive Software

Can I pool XMPPConnection?

I’'m writing an service application that runs on the server side,

For each request to the application, we create a new connection to the IM

server, and do the job then close it.

The flow I’'m using is(simplified):

XMPPConnection mConn = new XMPPConnection(mXMPPServerName);

mConn.login(mName, mPassword, mClientName);

if ( mConn.isAuthenticated() && mConn.isConnected() ) {

PacketFilter mFilter;

PacketCollector mCollector;

PacketListener mListener;

Authentication mRequest;

Packet mResponse;

mFilter = new PacketTypeFilter(Authentication.class);

mCollector = mConn.createPacketCollector(mFilter);

mListener = new PacketListener() {

public void processPacket(Packet packet) {

// do something with incoming packet.

}

};

mConn.addPacketListener(mListener, mFilter);

mRequest = new Authentication();

mRequest.setTo(mClientName);

mRequest.setResource(mResource);

mRequest.setUsername(mUserName);

mRequest.setPassword(mUserPass);

mConn.sendPacket(mRequest);

mRequest = (Authentication)mCollector.nextResult();

}

mConn.close();[/code]

My questions are:

  1. is XMPPConnection thread safe?

  2. Can I pool the connection? or do I need to? or I can just use one connection

for all request, i.e. 1 must be true.

Thanks for any response.

Hey Heng,

  1. is XMPPConnection thread safe?

Connections are thread safe. A common mistake, though, is to reuse the same packet that was just sent and modify it with new values. So as long as you create new Packets you will be fine.

  1. Can I pool the connection? or do I need to? or I can just use one connection

for all request, i.e. 1 must be true.

Depending on the amount of traffic and parallelism you may need to use 1 or more connections to the server. Remember that you can use anonymous connections (if the server supports them) so you may create as many connections as you may need.

Regards,

– Gato