When to close the connection if I have a listener on it?

I have a connection and I am expecting a subscription request very soon (my program generates the actual subscription request) how do I ensure I don’'t close the connection prematurely? I understand that the listener on a connection exists while the connection is alive. So if I close before I have delt with the request - it will be pending. When can I be certain I can close the connection?

final XMPPConnection conn = new XMPPConnection(imServer);

conn.login(userName, password);

PacketListener packetListener = new PacketListener()

{

public void processPacket(Packet packet)

{

if (packet instanceof Presence)

{

Presence presencePacket = (Presence) packet;

if (presencePacket.getType() == Presence.Type.SUBSCRIBE)

{

// accept the subscription

conn.sendPacket(new Presence(Presence.Type.SUBSCRIBED));

}

}

}

};

PacketFilter packetFilter = new PacketTypeFilter(Presence.class);

conn.createPacketCollector(packetFilter);

conn.addPacketListener(packetListener, packetFilter);

// some code here to generate the subscription request

// ??? conn.close(); ???

/code

Am really waiting on this question! Anyone have any ideas?

Hi Alex,

Can’'t you just close the connection after you accept the subscription? i.e.:

// accept the subscription

conn.sendPacket(new Presence(Presence.Type.SUBSCRIBED));

conn.close();

/code

Hope that helps,

Ryan

Thanks for the reply ryang.

That wouldn’'t work because the outer method would exit and the listener would go out of scope before getting any packets.

Having a sleep before exiting would probably work - but this would be bad in my situation as I need this to be as quick as possible!

In the end I used a packet collector - which is better than a listener if you are waiting on a packet that you know is due to arrive - since my code generates the subscription request.

PacketCollector collector = conn.createPacketCollector(new PacketTypeFilter(Presence.class));

Packet response = collector.nextResultSmackConfiguration.getPacketReplyTimeout());

collector.cancel();

// deal with response

/code

This worked a very well!

One question remains should the conn.login(…) go before or after the conn.createPAcketColector(…). Can packets be missed if it goes before?

Alex.

Hi Alex,

That wouldn’'t work because the outer method would

exit and the listener would go out of scope before

getting any packets.

Are you sure about that? If that is the case then you would have had the same problem with accepting the subscription.

But, I’'m glad to hear you got things to work.

One question remains should the conn.login(…) go

before or after the conn.createPAcketColector(…).

Can packets be missed if it goes before?

It should go after for the very reason you mentioned, you don’'t want any of those packets getting lost.

~Ryan

Yes - it would because:

final XMPPConnection conn = new XMPPConnection(imServer);

is declared as local variable within the method. So once the method exists - even without closing the connection. It is out of scope.

Ok - so login should go after!

Yes - it would because:

final XMPPConnection conn = new

XMPPConnection(imServer);

is declared as local variable within the method. So

once the method exists - even without closing the

connection. It is out of scope.

Ah, ok, you’‘re absolutely right. I didn’'t realize that you were working with a method level rather than a class level variable.

Cheers,

Ryan