@SuppressWarnings("SleepWhileInLoop") public boolean waitForCompletion(ApplicationId appId,AppStatusCallback callback,long timeoutMillis) throws YarnException, IOException {
  long startMillis=System.currentTimeMillis();
  while (true) {
    try {
      Thread.sleep(1000);
    }
 catch (    InterruptedException e) {
      LOG.debug("Thread sleep in monitoring loop interrupted");
    }
    ApplicationReport report=clientRM.getApplicationReport(appId);
    if (callback.exitLoop(report) == true) {
      return true;
    }
    YarnApplicationState state=report.getYarnApplicationState();
    FinalApplicationStatus dsStatus=report.getFinalApplicationStatus();
    if (YarnApplicationState.FINISHED == state) {
      if (FinalApplicationStatus.SUCCEEDED == dsStatus) {
        LOG.info("Application has completed successfully. Breaking monitoring loop");
        return true;
      }
 else {
        LOG.info("Application finished unsuccessfully." + " YarnState=" + state.toString() + ", DSFinalStatus="+ dsStatus.toString()+ ". Breaking monitoring loop");
        return false;
      }
    }
 else     if (YarnApplicationState.KILLED == state || YarnApplicationState.FAILED == state) {
      LOG.info("Application did not finish." + " YarnState=" + state.toString() + ", DSFinalStatus="+ dsStatus.toString()+ ". Breaking monitoring loop");
      return false;
    }
    if (System.currentTimeMillis() - startMillis > timeoutMillis) {
      LOG.info("Reached specified timeout. Killing application");
      clientRM.killApplication(appId);
      return false;
    }
  }
}

@ SuppressWarnings ( "SleepWhileInLoop" ) public boolean waitForCompletion ( ApplicationId appId , AppStatusCallback callback , long timeoutMillis ) throws YarnException , IOException { long startMillis = System . currentTimeMillis ( ) ; while ( true ) { try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException e ) { LOG . debug ( "Thread sleep in monitoring loop interrupted" ) ; } ApplicationReport report = clientRM . getApplicationReport ( appId ) ; if ( callback . exitLoop ( report ) == true ) { return true ; } YarnApplicationState state = report . getYarnApplicationState ( ) ; FinalApplicationStatus dsStatus = report . getFinalApplicationStatus ( ) ; if ( YarnApplicationState . FINISHED == state ) { if ( FinalApplicationStatus . SUCCEEDED == dsStatus ) { LOG . info ( "Application has completed successfully. Breaking monitoring loop" ) ; return true ; } else { LOG . info ( "Application finished unsuccessfully." + " YarnState=" + state . toString ( ) + ", DSFinalStatus=" + dsStatus . toString ( ) + ". Breaking monitoring loop" ) ; return false ; } } else if ( YarnApplicationState . KILLED == state || YarnApplicationState . FAILED == state ) { LOG . info ( "Application did not finish." + " YarnState=" + state . toString ( ) + ", DSFinalStatus=" + dsStatus . toString ( ) + ". Breaking monitoring loop" ) ; return false ; } if ( System . currentTimeMillis ( ) - startMillis > timeoutMillis ) { LOG . info ( "Reached specified timeout. Killing application" ) ; clientRM . killApplication ( appId ) ; return false ; } } }
monitor the submitted application for completion . kill application if time expires .
