public void connectionLost(java.lang.Throwable cause){
  int rc=-1;
  led.setAmber();
  led.setFlash();
  setTitleText("Connection Lost!....Reconnecting");
synchronized (this) {
    writeLogln("MQTT Connection Lost!....Reconnecting to " + mqtt.getServerURI());
  }
  try {
    while ((rc == -1) && connected) {
      try {
synchronized (connLostWait) {
          connLostWait.wait(10000);
        }
      }
 catch (      InterruptedException iex) {
      }
synchronized (this) {
        if (connected) {
          writeLog("MQTT reconnecting......");
          try {
            connect(mqtt.getServerURI(),optionsComp.isPersistenceSelected());
            rc=0;
          }
 catch (          MqttException mqte) {
            rc=-1;
          }
          if (rc == -1) {
            writeLogln("failed");
          }
 else {
            writeLogln("success !");
          }
        }
      }
    }
    setTitleText("");
  }
 catch (  Exception ex) {
    setTitleText("MQTT connection broken !");
    ex.printStackTrace();
    disconnect();
  }
 finally {
    if (led.isFlashing()) {
      led.setFlash();
    }
  }
  if (connected) {
    led.setGreen();
    setConnected(true);
  }
 else {
    led.setRed();
    setConnected(false);
  }
}

public void connectionLost ( java . lang . Throwable cause ) { int rc = - 1 ; led . setAmber ( ) ; led . setFlash ( ) ; setTitleText ( "Connection Lost!....Reconnecting" ) ; synchronized ( this ) { writeLogln ( "MQTT Connection Lost!....Reconnecting to " + mqtt . getServerURI ( ) ) ; } try { while ( ( rc == - 1 ) && connected ) { try { synchronized ( connLostWait ) { connLostWait . wait ( 10000 ) ; } } catch ( InterruptedException iex ) { } synchronized ( this ) { if ( connected ) { writeLog ( "MQTT reconnecting......" ) ; try { connect ( mqtt . getServerURI ( ) , optionsComp . isPersistenceSelected ( ) ) ; rc = 0 ; } catch ( MqttException mqte ) { rc = - 1 ; } if ( rc == - 1 ) { writeLogln ( "failed" ) ; } else { writeLogln ( "success !" ) ; } } } } setTitleText ( "" ) ; } catch ( Exception ex ) { setTitleText ( "MQTT connection broken !" ) ; ex . printStackTrace ( ) ; disconnect ( ) ; } finally { if ( led . isFlashing ( ) ) { led . setFlash ( ) ; } } if ( connected ) { led . setGreen ( ) ; setConnected ( true ) ; } else { led . setRed ( ) ; setConnected ( false ) ; } }
the method is part of the mqttsimplecallback interface < br > in the event of the mqtt connection being broken the led is set to colour amber and made to flash . the code then keeps trying to reconnect until either a successful reconnect occurs or the disconnect button is pressed . finally the led is stopped flashing and set to green or red depending upon whether the connect was successful or not .
