认识smack中的基本对象 - PacketCollector对象

一 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();


 

 

原文:http://www.igniterealtime.org/builds/smack/docs/3.2.2/javadoc/index.html?org/jivesoftware/smack/AccountManager.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值