लंबे समय तक चलने वाली कार्रवाइयां (LRO)

एपीआई को किए गए कई कॉल में, लंबे समय तक चलने वाले ऑपरेशन दिखते हैं. ये सुविधाएं, स्टेटस को ट्रैक करती हैं काम करती है, जो लंबे समय तक काम करती है, जैसे कि RPC को ब्लॉक करना ज़रूरी नहीं है.

OperationsFuture क्लास

एलआरओ के साथ इंटरैक्ट करने का सबसे आसान तरीका है OperationFuture क्लास. अगर इसका इस्तेमाल किया जाता है, तो पक्का करें कि सर्विस क्लाइंट खत्म न हुआ हो.

इसका सुझाव नहीं दिया जाता:

private void doSomething() {
  OperationFuture<Empty, Empty> future = startLongRunningOperation(jobName);
  // The service client has already been destroyed because of the try-with-resources
  // block, so the `future.get()` call will fail.
  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 दायरे में आता है.