private boolean isLastHealthyInstance(TargetDescription target, TargetGroup targetGroup) {
    DescribeTargetHealthRequest targetHealthRequest = new DescribeTargetHealthRequest()
        .withTargetGroupArn(targetGroup.getTargetGroupArn());
    List<TargetHealthDescription> targetHealthDescriptions = elbClient
        .describeTargetHealth(targetHealthRequest)
        .getTargetHealthDescriptions();

    boolean instanceIsHealthy = false;
    int healthyCount = 0;

    for (TargetHealthDescription targetHealthDescription : targetHealthDescriptions) {
      if (targetHealthDescription.getTargetHealth().getState()
          .equals(TargetHealthStateEnum.Healthy.toString())) {
        healthyCount += 1;
        if (targetHealthDescription.getTarget().equals(target)) {
          instanceIsHealthy = true;
        }
      }
    }

    return instanceIsHealthy && healthyCount == 1;
  }
private boolean isLastHealthyInstance ( TargetDescription target , TargetGroup targetGroup ) { DescribeTargetHealthRequest targetHealthRequest = new DescribeTargetHealthRequest ( ) . withTargetGroupArn ( targetGroup . getTargetGroupArn ( ) ) ; List < TargetHealthDescription > targetHealthDescriptions = elbClient . describeTargetHealth ( targetHealthRequest ) . getTargetHealthDescriptions ( ) ; boolean instanceIsHealthy = false ; int healthyCount = 0 ; for ( TargetHealthDescription targetHealthDescription : targetHealthDescriptions ) { if ( targetHealthDescription . getTargetHealth ( ) . getState ( ) . equals ( TargetHealthStateEnum . Healthy . toString ( ) ) ) { healthyCount += 1 ; if ( targetHealthDescription . getTarget ( ) . equals ( target ) ) { instanceIsHealthy = true ; } } } return instanceIsHealthy && healthyCount == 1 ; }
@param target Target to check
@param targetGroup Group to check in
@return if the given target is the last healthy target in the given target group