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);
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
PacketFilter packetFilter = new PacketTypeFilter(Presence.class);
// some code here to generate the subscription request
// ??? conn.close(); ???