Open zaur opened 8 years ago
Probably a duplicate of https://github.com/fusesource/mqtt-client/issues/48
i merge the commit to my code,still OOM,how can i solve this problem? can you help me?
Maybe you are trying to connect to the server repeated while it is already connected to server. I had meet this problem, and i used a flag to sign the connection status, like this:
private void connect() {
//AndroidUtils.checkNotMain();
// 连接服务器
if (isConnected()) {
return;
}
mCallbackConnection = mMQTT.callbackConnection();
mCallbackConnection.listener(new ExtendedListener() {
@Override public void onConnected() {
mConnected = true;
}
@Override public void onDisconnected() {
mConnected = false;
}
...
}
}
private boolean isConnected() {
return mConnected;
}
Now it's ok, no OOM. But i got another problem ,somtimes it will crash:
Exception in thread "hawtdispatch-DEFAULT-2" java.lang.NullPointerException
at org.fusesource.mqtt.client.CallbackConnection$7.run(CallbackConnection.java:450)
......
public void onSessionEstablished(Transport transport) {
if(mqtt.getKeepAlive()>0) {
heartBeatMonitor = new HeartBeatMonitor();
heartBeatMonitor.setWriteInterval((mqtt.getKeepAlive() * 1000) / 2);
heartBeatMonitor.setTransport(this.transport);
heartBeatMonitor.suspendRead(); // to match the suspended state of the transport.
heartBeatMonitor.setOnKeepAlive(new Task() {
@Override
public void run() {
// Don't care if the offer is rejected, just means we have data outbound.
if(!disconnected && pingedAt==0) {
MQTTFrame encoded = new PINGREQ().encode();
// -------------this.transport == null----------------
if(CallbackConnection.this.transport.offer(encoded)) {
............
}
The commit #48 & #51,This line has been changed , like this: if(CallbackConnection.this.transport != null && CallbackConnection.this.transport.offer(encoded)) and delete this: //handleSessionFailure(new ProtocolException("Ping TimeOut").fillInStackTrace()); and i have tried this ,there is no some bad effects, i hope this will help for your NullPointException.
THKS.
@zhlmgithub
I don't know why delete this line:
java handleSessionFailure(new ProtocolException("Ping TimeOut").fillInStackTrace());
It seems there will not reconnect to server when heartbeat timeout if delete this line.
void handleSessionFailure(Throwable error) {
// Socket failure, should we try to reconnect?
if (!disconnected && (mqtt.reconnectAttemptsMax < 0
|| reconnects < mqtt.reconnectAttemptsMax)) {
mqtt.tracer.debug("Reconnecting transport");
// Cleanup the previous transport.
if (heartBeatMonitor != null) {
heartBeatMonitor.stop();
heartBeatMonitor = null;
}
final Transport t = transport;
transport = null;
if (t != null) {
t.stop(new Task() {
@Override public void run() {
listener.onDisconnected();
reconnect();
}
});
} else {
reconnect();
}
} else {
// nope.
handleFatalFailure(error);
}
}
This is my code:
I launch the app and client successfully connects. Then I switch wifi off and on and wait until the client reconnects. After repeating this 2-3 times mqttclient starts opening and closing connections like crazy until the app crashes with OutOfMemoryError:
and here is what happens in mosquitto logs: