public static ReplicatorDocument waitForReplicatorToReachStatus( CloudantClient account, String replicatorDocId, String status) throws Exception { ReplicatorDocument replicatorDoc = null; boolean finished = false; long startTime = System.currentTimeMillis(); long timeout = startTime + TIMEOUT_MILLISECONDS; // initial wait of 100 ms long delay = 100; while (!finished && System.currentTimeMillis() < timeout) { // Sleep before finding replication document Thread.sleep(delay); replicatorDoc = account.replicator().replicatorDocId(replicatorDocId).find(); // Check if replicator doc is in specified state String state; if (replicatorDoc != null && (state = replicatorDoc.getReplicationState()) != null) { // if we've reached the status or we reached an error then we are finished if (state.equalsIgnoreCase(status) || state.equalsIgnoreCase("error")) { finished = true; } } // double the delay for the next iteration delay *= 2; } if (!finished) { throw new TimeoutException("Timed out waiting for replication to complete"); } return replicatorDoc; }