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)
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.