Długotrwałe operacje (LRO)

Kilka wywołań interfejsu API zwraca długotrwałe operacje. Śledzą one stan zadania wykonywanego przez dłuższy czas, więc stosowanie blokującego wywołania RPC nie jest pożądane.

Klasa OperationFuture

Najbardziej oczywistym sposobem interakcji z LRO jest użycie klasy OperationFuture. Jeśli używasz tej funkcji, upewnij się, że klient usługi nie został zniszczony.

Niezalecane:

private void doSomething() {
  OperationFuture<Empty, Empty> future = startLongRunningOperation(jobName);
  future.get();
}

private OperationFuture<Empty, Empty> startLongRunningOperation(String jobToStart)
    throws UnsupportedEncodingException {
  try (OfflineUserDataJobServiceClient offlineUserDataJobServiceClient =
      googleAdsClient.getLatestVersion().createOfflineUserDataJobServiceClient()) {
    // Issues an asynchronous request to run the offline user data job for executing
    // all added operations.
    return offlineUserDataJobServiceClient.runOfflineUserDataJobAsync(jobToStart);
  }
}

Zalecane:

private void doSomethingElse() {
  try (OfflineUserDataJobServiceClient offlineUserDataJobServiceClient =
      googleAdsClient.getLatestVersion().createOfflineUserDataJobServiceClient()) {
    OperationFuture<Empty, Empty> future = startLongRunningOperation(offlineUserDataJobServiceClient, jobName);
    future.get();
  }
}

private OperationFuture<Empty, Empty> startLongRunningOperation(String jobToStart)
    throws UnsupportedEncodingException {
    offlineUserDataJobServiceClient.runOfflineUserDataJobAsync(jobToStart);
}

Zwróć uwagę, że klasa OperationFuture jest używana tylko wtedy, gdy OfflineUserDataJobServiceClient jest w zakresie.