Google Ads API への複数の呼び出しは、長時間実行オペレーション(LRO)を返します。これらは、単一のブロッキング RPC を必要とせずに長期間実行されるジョブのステータスを追跡します。
OperationFuture クラス
Java クライアント ライブラリで LRO を操作するには、OperationFuture クラスを使用します。OperationFuture は、基盤となるサービス クライアントに依存して API のステータス更新をポーリングするため、オペレーションが完了するまでサービス クライアントが開いてアクティブな状態を維持するようにしてください。
非推奨:
private void executeOperation(String jobName) throws Exception {
OperationFuture<Empty, OfflineUserDataJobMetadata> future =
startLongRunningOperation(jobName);
// FAILS: The service client was already closed when
// startLongRunningOperation() exited its try-with-resources block, so
// future.get() fails when polling.
future.get();
}
private OperationFuture<Empty, OfflineUserDataJobMetadata>
startLongRunningOperation(String jobToStart) {
try (OfflineUserDataJobServiceClient offlineUserDataJobServiceClient =
googleAdsClient
.getLatestVersion()
.createOfflineUserDataJobServiceClient()) {
// Issues an asynchronous request to run the offline user data job.
return offlineUserDataJobServiceClient.runOfflineUserDataJobAsync(
jobToStart);
}
}
推奨:
private void executeOperation(String jobName) throws Exception {
try (OfflineUserDataJobServiceClient offlineUserDataJobServiceClient =
googleAdsClient
.getLatestVersion()
.createOfflineUserDataJobServiceClient()) {
OperationFuture<Empty, OfflineUserDataJobMetadata> future =
startLongRunningOperation(offlineUserDataJobServiceClient, jobName);
// Waits for the operation to complete (with a timeout) while the client is
// still open.
future.get(5, TimeUnit.MINUTES);
}
}
private OperationFuture<Empty, OfflineUserDataJobMetadata>
startLongRunningOperation(
OfflineUserDataJobServiceClient offlineUserDataJobServiceClient,
String jobToStart) {
return offlineUserDataJobServiceClient.runOfflineUserDataJobAsync(jobToStart);
}
OperationFuture は、OfflineUserDataJobServiceClient が try-with-resources ブロック内で開いたままになっている間のみ使用されます。