【发布时间】:2018-12-06 18:45:11
【问题描述】:
Smack 版本:4.2.4
当大量消息(普通消息和延迟重发消息)进入android客户端时,smack会创建100~200个休眠的Smack Cache Executor线程。这种突然涌入的线程导致 android 客户端变得无响应(ANR 错误)。
在正常操作下,至少有 2 个 Smack Cache Executor 线程,应用程序将运行总共 50-60 个线程。
延迟离线存储类型的消息由“灵活离线消息检索”协议处理。然而,那些正常和延迟重发的消息似乎没有任何特殊的协议,对于线程数涌入也没有明确的解决方案。
这里的大量消息是指600+条重发消息和几秒钟内收到的正常消息。
非常感谢您就此事提供任何帮助或建议。提前谢谢了!
编辑 1:
当与 XMPP 的连接通过身份验证时,会设置 3 个 Stanza 侦听器。一个用于 Presence,一个用于 Message,一个用于 IQ。它们如下:
public void addPresenceStanzaListener(){
if (presenceStanzaListener != null) {
connection.removeSyncStanzaListener(presenceStanzaListener);
}
StanzaFilter presenceStanzaFilter = new StanzaTypeFilter(Presence.class);
presenceStanzaListener = new StanzaListener() {
@Override
public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
//Process presence stanza
}
};
connection.addSyncStanzaListener(presenceStanzaListener, presenceStanzaFilter);
}
public void addMessageStanzaListener(){
if (messageStanzaListener != null) {
connection.removeSyncStanzaListener(messageStanzaListener);
}
StanzaFilter messageStanzaFilter = new StanzaTypeFilter(Message.class);
messageStanzaListener = new StanzaListener() {
@Override
public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
//Process message stanza
}
};
connection.addSyncStanzaListener(messageStanzaListener, messageStanzaFilter);
}
public void addIQStanzaListener(){
if (iqStanzaListener != null) {
connection.removeSyncStanzaListener(iqStanzaListener);
}
StanzaFilter iqStanzaFilter = new StanzaTypeFilter(IQ.class);
iqStanzaListener = new StanzaListener() {
@Override
public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException {
//Process IQ stanza
}
};
connection.addSyncStanzaListener(iqStanzaListener, iqStanzaFilter);
}
Smack Cache Ex 线程数增加是如何触发的:
- 关闭互联网连接
- 垃圾邮件 600 多条
- 重新打开 Internet 连接(此时,从 ejabberd 的角度来看,android 客户端从未与它断开连接)
- 客户端连接到 XMPP 的那一刻,Smack Cache Ex 线程数至少增加 100。
【问题讨论】:
标签: android multithreading smack