การดําเนินการระยะยาว (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
อยู่ในขอบเขต
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-21 UTC
[null,null,["อัปเดตล่าสุด 2025-08-21 UTC"],[[["\u003cp\u003eSeveral API calls initiate long-running operations, tracked by jobs that execute over time, making blocking RPCs undesirable.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eOperationFuture\u003c/code\u003e class facilitates interaction with long-running operations, but requires the service client to remain active during its usage.\u003c/p\u003e\n"],["\u003cp\u003eDirectly using \u003ccode\u003eOperationFuture\u003c/code\u003e within a method without ensuring the service client's lifespan can lead to issues.\u003c/p\u003e\n"],["\u003cp\u003eIt's recommended to utilize \u003ccode\u003eOperationFuture\u003c/code\u003e within the scope of the service client, as demonstrated in the "Recommended" code example, to prevent premature client destruction.\u003c/p\u003e\n"]]],[],null,["# Long-running operations (LROs)\n\nSeveral calls to the API return long-running operations. These track the status\nof a job which executes over an extended period of time, such that having a\nblocking RPC is not desirable.\n\nOperationFuture class\n---------------------\n\nThe most obvious way to interact with LROs is with the\n[`OperationFuture`](//googleapis.dev/java/gax/latest/com/google/api/gax/longrunning/OperationFuture.html) class. If you use this, make sure that the service client is not destroyed.\n\nNot recommended: \n\n private void doSomething() {\n OperationFuture\u003cEmpty, Empty\u003e future = startLongRunningOperation(jobName);\n future.get();\n }\n\n private OperationFuture\u003cEmpty, Empty\u003e startLongRunningOperation(String jobToStart)\n throws UnsupportedEncodingException {\n try (OfflineUserDataJobServiceClient offlineUserDataJobServiceClient =\n googleAdsClient.getLatestVersion().createOfflineUserDataJobServiceClient()) {\n // Issues an asynchronous request to run the offline user data job for executing\n // all added operations.\n return offlineUserDataJobServiceClient.runOfflineUserDataJobAsync(jobToStart);\n }\n }\n\nRecommended: \n\n private void doSomethingElse() {\n try (OfflineUserDataJobServiceClient offlineUserDataJobServiceClient =\n googleAdsClient.getLatestVersion().createOfflineUserDataJobServiceClient()) {\n OperationFuture\u003cEmpty, Empty\u003e future = startLongRunningOperation(offlineUserDataJobServiceClient, jobName);\n future.get();\n }\n }\n\n private OperationFuture\u003cEmpty, Empty\u003e startLongRunningOperation(String jobToStart)\n throws UnsupportedEncodingException {\n offlineUserDataJobServiceClient.runOfflineUserDataJobAsync(jobToStart);\n }\n\nNotice how the `OperationFuture` class is only used while the\n`OfflineUserDataJobServiceClient` is in scope."]]