Introduction to a packetcollector object
The packetcollector object collects packet information packages based on the specified filter and puts them into the result set queue. packetcollector can be used to perform blocking and polling operations on a result set. This object is often used when you submit a query request to the server and wait for the query results.
Provides a mechanic to collect packets into a result queue that pass a specified filter. The Collector lets you perform blocking and polling operations on the result queue. So, a packetcollector is more suitable to use
APacketListener
When you need to wait for a specific result.
Each packet collector will queue up a configured number of packets for processing before older packets are automatically dropped. The default number is retrievedSmackConfiguration.getPacketCollectorSize()
.
Method 2 of packetcollector object
Cancel ()
public void cancel()
-
Explicitly cancels the packet collector so that no more results are queued up. Once a packet collector has been canceled, it cannot be re-enabled. Instead, a new packet collector must be created.
Getpacketfilter
public PacketFilter getPacketFilter()
-
Returns the packet filter associated with this packet collector. The packet filter is used to determine what packets are queued as results.
Returns:
-
-
The packet filter.
Pollresult
public Packet pollResult()
-
Polls to see if a packet is currently available and returns it, or immediately returns NullIf no packets are currently in the result queue.
Returns:
-
-
The next packet result, or NullIf there are no more results.
Nextresult
public Packet nextResult()
-
Returns the next available packet. The method call will block (not return) until a packet is available.
Returns:
-
-
The next available packet.
Nextresult
public Packet nextResult(long timeout)
-
Returns the next available packet. The method call will block (not return) until a packet is available or TimeoutHas elapased. If the timeout elapses without a result, NullWill be returned.
Parameters:
-
-
timeout
-The amount of time to wait for the next packet (in milleseconds ).
-
Returns:
-
The next available packet.
Processpacket
protected void processPacket(Packet packet)
-
Processes a packet to see if it meets the criteria for this packet collector. If so, the packet is added to the result queue.
Parameters:
-
-
packet
-The packet to process.
Example 3
The following code snippet is an example of registering a user on the openfire server. Use the createpacketcollector method of xmppconnection to create a packetcollector object. At the same time, you must pass a packetfilter to this object to filter packet. (For more information about packetfilter, see the previous blog post ), after the collection is complete, you need to call the Cancel Method to stop packet collection.
IQ result = NULL; registration Reg = new registration (); Reg. settype (IQ. type. set); Reg. setto (connectionutils. getconnection (). getservicename (); Reg. setusername (username); Reg. setpassword (password); Reg. addattribute ("android", "geolo_createuser_android"); packetfilter filter = new andfilter (New packetidfilter (Reg. getpacketid (), new packettypefilter (IQ. class); xmppconnection regconnection = connectionutils. getregconnection (); try {regconnection. connect ();} catch (xmppexception e) {return NULL;} packetcollector collector = regconnection. createpacketcollector (filter); regconnection. sendpacket (REG); Result = (IQ) Collector. nextresult (smackconfiguration. getpacketreplytimeout (); collector. cancel (); // stop the request results (whether the result is successful) regconnection. disconnect ();
Http://www.igniterealtime.org/builds/smack/docs/3.2.2/javadoc/index.html? Org/jivesoftware/smack/accountmanager.html