長時間実行オペレーション(LRO)

API を呼び出すと、長時間実行オペレーションが返されます。ブロッキング RPC は望ましくないため、長時間実行されるジョブのステータスを追跡します。

OperationFuture クラス

LRO を操作するには、OperationFuture クラスを使用するのが最もわかりやすい方法です。これを使用する場合は、サービス クライアントが破棄されていないことを確認してください。

非推奨:

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);
  }
}

推奨:

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);
}

OperationFuture クラスは、OfflineUserDataJobServiceClient がスコープ内にある場合にのみ使用されます。