一 PacketCollector对象的介绍
PacketCollector对象根据指定的过滤器收集packet信息包,并将其放入结果集队列,PacketCollector可用于在一个结果集上执行阻塞和轮询操作。当向服务器提交查询请求,并等待查询结果时,往往会使用该对象来实现。
Provides a mechanism 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 than a PacketListener
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 retrieved by SmackConfiguration.getPacketCollectorSize()
.
二 PacketCollector对象的方法
cancel()
public void cancel()
-
Explicitly cancels the packet collector so that no more results are queued up. Once a packet collector has been cancelled, 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 returnsnull if no packets are currently in the result queue.
Returns:
-
- the next packet result, or null if 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 thetimeout has elapased. If the timeout elapses without a result,null will 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.
-
三 示例
下面的代码段,是一个在openfire服务器上注册用户的示例。使用XMPPConnection的createPacketCollector方法来创建一个PacketCollector对象,同时要给这个对象传递一个PacketFilter,来对packet进行过滤(有关packetFilter的介绍参见前一篇博文),收集工作完成后,需要调用cancel方法停止packet的收集
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();// 停止请求results(是否成功的结果)
regConnection.disconnect();