我正在使用Smack Java库(版本4.3.1)(JDK 8)。经过一些随机的时间后,我有时会遇到以下异常。有什么办法可以避免吗?
org.jivesoftware.smack.SmackException$NotConnectedException: The connection [...] (117) is no longer connected. done=true smResumptionPossible=false at
org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketWriter.throwNotConnectedExceptionIfDoneAndResumptionNotPossible(XMPPTCPConnection.java:1320) at
org.jivesoftware.smack.tcp.XMPPTCPConnection.throwNotConnectedExceptionIfAppropriate(XMPPTCPConnection.java:359) at
org.jivesoftware.smack.AbstractXMPPConnection.sendStanza(AbstractXMPPConnection.java:688)
XMPPTCPConnectionConfiguration:
XMPPTCPConnectionConfiguration.builder(
).setSecurityMode(
ConnectionConfiguration.SecurityMode.disabled
).setXmppDomain(
this.domain
).setHostAddress(
InetAddress.getByName(
this.host
)
)
.allowEmptyOrNullUsernames(
).performSaslAnonymousAuthentication(
).build();
XMPPTCPConnection:
conn.setReplyTimeout(30 * 1000);
conn.setUseStreamManagement(true);
conn.setUseStreamManagementResumption(true);
conn.connect();
conn.login();
Maven Central工件:
<artifactId>smack-core</artifactId>
<version>4.3.1</version>
<artifactId>smack-tcp</artifactId>
<version>4.3.1</version>
<artifactId>smack-extensions</artifactId>
<version>4.3.1</version>
<artifactId>smack-java7</artifactId>
<version>4.3.1</version>
<artifactId>smack-resolver-dnsjava</artifactId>
<version>4.3.1</version>
其他信息:
问题是conn.isConnected
返回true。在内部,Smack在发送节(conn.sendStanza
)之前执行以下检查:If({conn.isConnected
)然后if(conn.isSmResumptionPossible
)然后sendStanza()
。如果conn.isSmResumptionPossible()
返回false,则抛出我得到的异常。
答案 0 :(得分:1)
安装连接侦听器,如果侦听器的回调告诉您连接断开,则重新连接。
答案 1 :(得分:0)
以下是帮助我缓解此问题的原因:
final XMPPTCPConnection conn = new XMPPTCPConnection(config);
conn.setReplyTimeout(30_000);
conn.setUseStreamManagement(true);
conn.setUseStreamManagementResumption(true);
conn.setPreferredResumptionTime(5);
ReconnectionManager.getInstanceFor(conn).enableAutomaticReconnection();
PingManager.getInstanceFor(conn).setPingInterval(60);