@BetaApi public class OperationsClient extends Object implements com.google.api.gax.core.BackgroundResource
When an API method normally takes long time to complete, it can be designed to return [Operation][google.longrunning.Operation] to the client, and the client can use this interface to receive the real response asynchronously by polling the operation resource, or pass the operation resource to another API (such as Google Cloud Pub/Sub API) to receive the response. Any API service that returns long-running operations should implement the `Operations` interface so developers can have a consistent client experience.
This class provides the ability to make remote calls to the backing service through method calls that map to API methods. Sample code to get started:
try (OperationsClient operationsClient = OperationsClient.create()) {
String name = "name3373707";
Operation response = operationsClient.getOperation(name);
}
Note: close() needs to be called on the OperationsClient object to clean up resources such as threads. In the example above, try-with-resources is used, which automatically calls close().
The surface of this class includes several types of Java methods for each of the API's methods:
See the individual methods for example code.
Many parameters require resource names to be formatted in a particular way. To assist with these names, this class includes a format method for each type of name, and additionally a parse method to extract the individual identifiers contained within names that are returned.
This class can be customized by passing in a custom instance of OperationsSettings to create(). For example:
To customize credentials:
OperationsSettings operationsSettings =
OperationsSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
.build();
OperationsClient operationsClient = OperationsClient.create(operationsSettings);
To customize the endpoint:
OperationsSettings operationsSettings =
OperationsSettings.newBuilder().setEndpoint(myEndpoint).build();
OperationsClient operationsClient = OperationsClient.create(operationsSettings);
Please refer to the GitHub repository's samples for more quickstart code snippets.
| Modifier and Type | Class and Description |
|---|---|
static class |
OperationsClient.ListOperationsFixedSizeCollection |
static class |
OperationsClient.ListOperationsPage |
static class |
OperationsClient.ListOperationsPagedResponse |
| Modifier | Constructor and Description |
|---|---|
protected |
OperationsClient(OperationsSettings settings)
Constructs an instance of OperationsClient, using the given settings.
|
protected |
OperationsClient(OperationsStub stub) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
awaitTermination(long duration,
TimeUnit unit) |
void |
cancelOperation(com.google.longrunning.CancelOperationRequest request)
Starts asynchronous cancellation on a long-running operation.
|
void |
cancelOperation(String name)
Starts asynchronous cancellation on a long-running operation.
|
com.google.api.gax.rpc.UnaryCallable<com.google.longrunning.CancelOperationRequest,com.google.protobuf.Empty> |
cancelOperationCallable()
Starts asynchronous cancellation on a long-running operation.
|
void |
close() |
static OperationsClient |
create()
Constructs an instance of OperationsClient with default settings.
|
static OperationsClient |
create(com.google.api.gax.core.BackgroundResource stub)
Constructs an instance of OperationsClient, using the given stub for making calls.
|
static OperationsClient |
create(OperationsSettings settings)
Constructs an instance of OperationsClient, using the given settings.
|
static OperationsClient |
create(OperationsStub stub)
Constructs an instance of OperationsClient, using the given stub for making calls.
|
void |
deleteOperation(com.google.longrunning.DeleteOperationRequest request)
Deletes a long-running operation.
|
void |
deleteOperation(String name)
Deletes a long-running operation.
|
com.google.api.gax.rpc.UnaryCallable<com.google.longrunning.DeleteOperationRequest,com.google.protobuf.Empty> |
deleteOperationCallable()
Deletes a long-running operation.
|
com.google.longrunning.Operation |
getOperation(com.google.longrunning.GetOperationRequest request)
Gets the latest state of a long-running operation.
|
com.google.longrunning.Operation |
getOperation(String name)
Gets the latest state of a long-running operation.
|
com.google.api.gax.rpc.UnaryCallable<com.google.longrunning.GetOperationRequest,com.google.longrunning.Operation> |
getOperationCallable()
Gets the latest state of a long-running operation.
|
OperationsSettings |
getSettings() |
OperationsStub |
getStub() |
boolean |
isShutdown() |
boolean |
isTerminated() |
OperationsClient.ListOperationsPagedResponse |
listOperations(com.google.longrunning.ListOperationsRequest request)
Lists operations that match the specified filter in the request.
|
OperationsClient.ListOperationsPagedResponse |
listOperations(String name,
String filter)
Lists operations that match the specified filter in the request.
|
com.google.api.gax.rpc.UnaryCallable<com.google.longrunning.ListOperationsRequest,com.google.longrunning.ListOperationsResponse> |
listOperationsCallable()
Lists operations that match the specified filter in the request.
|
com.google.api.gax.rpc.UnaryCallable<com.google.longrunning.ListOperationsRequest,OperationsClient.ListOperationsPagedResponse> |
listOperationsPagedCallable()
Lists operations that match the specified filter in the request.
|
void |
shutdown() |
void |
shutdownNow() |
protected OperationsClient(OperationsSettings settings) throws IOException
IOException@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") protected OperationsClient(OperationsStub stub)
public static final OperationsClient create() throws IOException
IOExceptionpublic static final OperationsClient create(OperationsSettings settings) throws IOException
IOExceptionpublic static final OperationsClient create(OperationsStub stub)
public static final OperationsClient create(com.google.api.gax.core.BackgroundResource stub)
public final OperationsSettings getSettings()
@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") public OperationsStub getStub()
public final OperationsClient.ListOperationsPagedResponse listOperations(String name, String filter)
NOTE: the `name` binding allows API services to override the binding to use different resource name schemes, such as `users/*/operations`. To override the binding, API services can add a binding such as `"/v1/{name=users/*}/operations"` to their service configuration. For backwards compatibility, the default name includes the operations collection id, however overriding users must ensure the name binding is the parent resource, without the operations collection id.
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) {
String name = "name3373707";
String filter = "filter-1274492040";
for (Operation element : operationsClient.listOperations(name, filter).iterateAll()) {
// doThingsWith(element);
}
}
name - The name of the operation's parent resource.filter - The standard list filter.com.google.api.gax.rpc.ApiException - if the remote call failspublic final OperationsClient.ListOperationsPagedResponse listOperations(com.google.longrunning.ListOperationsRequest request)
NOTE: the `name` binding allows API services to override the binding to use different resource name schemes, such as `users/*/operations`. To override the binding, API services can add a binding such as `"/v1/{name=users/*}/operations"` to their service configuration. For backwards compatibility, the default name includes the operations collection id, however overriding users must ensure the name binding is the parent resource, without the operations collection id.
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) {
ListOperationsRequest request =
ListOperationsRequest.newBuilder()
.setName("name3373707")
.setFilter("filter-1274492040")
.setPageSize(883849137)
.setPageToken("pageToken873572522")
.build();
for (Operation element : operationsClient.listOperations(request).iterateAll()) {
// doThingsWith(element);
}
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<com.google.longrunning.ListOperationsRequest,OperationsClient.ListOperationsPagedResponse> listOperationsPagedCallable()
NOTE: the `name` binding allows API services to override the binding to use different resource name schemes, such as `users/*/operations`. To override the binding, API services can add a binding such as `"/v1/{name=users/*}/operations"` to their service configuration. For backwards compatibility, the default name includes the operations collection id, however overriding users must ensure the name binding is the parent resource, without the operations collection id.
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) {
ListOperationsRequest request =
ListOperationsRequest.newBuilder()
.setName("name3373707")
.setFilter("filter-1274492040")
.setPageSize(883849137)
.setPageToken("pageToken873572522")
.build();
ApiFuture<Operation> future =
operationsClient.listOperationsPagedCallable().futureCall(request);
// Do something.
for (Operation element : future.get().iterateAll()) {
// doThingsWith(element);
}
}
public final com.google.api.gax.rpc.UnaryCallable<com.google.longrunning.ListOperationsRequest,com.google.longrunning.ListOperationsResponse> listOperationsCallable()
NOTE: the `name` binding allows API services to override the binding to use different resource name schemes, such as `users/*/operations`. To override the binding, API services can add a binding such as `"/v1/{name=users/*}/operations"` to their service configuration. For backwards compatibility, the default name includes the operations collection id, however overriding users must ensure the name binding is the parent resource, without the operations collection id.
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) {
ListOperationsRequest request =
ListOperationsRequest.newBuilder()
.setName("name3373707")
.setFilter("filter-1274492040")
.setPageSize(883849137)
.setPageToken("pageToken873572522")
.build();
while (true) {
ListOperationsResponse response = operationsClient.listOperationsCallable().call(request);
for (Operation element : response.getResponsesList()) {
// doThingsWith(element);
}
String nextPageToken = response.getNextPageToken();
if (!Strings.isNullOrEmpty(nextPageToken)) {
request = request.toBuilder().setPageToken(nextPageToken).build();
} else {
break;
}
}
}
public final com.google.longrunning.Operation getOperation(String name)
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) {
String name = "name3373707";
Operation response = operationsClient.getOperation(name);
}
name - The name of the operation resource.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.longrunning.Operation getOperation(com.google.longrunning.GetOperationRequest request)
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) {
GetOperationRequest request = GetOperationRequest.newBuilder().setName("name3373707").build();
Operation response = operationsClient.getOperation(request);
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<com.google.longrunning.GetOperationRequest,com.google.longrunning.Operation> getOperationCallable()
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) {
GetOperationRequest request = GetOperationRequest.newBuilder().setName("name3373707").build();
ApiFuture<Operation> future = operationsClient.getOperationCallable().futureCall(request);
// Do something.
Operation response = future.get();
}
public final void deleteOperation(String name)
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) {
String name = "name3373707";
operationsClient.deleteOperation(name);
}
name - The name of the operation resource to be deleted.com.google.api.gax.rpc.ApiException - if the remote call failspublic final void deleteOperation(com.google.longrunning.DeleteOperationRequest request)
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) {
DeleteOperationRequest request =
DeleteOperationRequest.newBuilder().setName("name3373707").build();
operationsClient.deleteOperation(request);
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<com.google.longrunning.DeleteOperationRequest,com.google.protobuf.Empty> deleteOperationCallable()
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) {
DeleteOperationRequest request =
DeleteOperationRequest.newBuilder().setName("name3373707").build();
ApiFuture<Empty> future = operationsClient.deleteOperationCallable().futureCall(request);
// Do something.
future.get();
}
public final void cancelOperation(String name)
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) {
String name = "name3373707";
operationsClient.cancelOperation(name);
}
name - The name of the operation resource to be cancelled.com.google.api.gax.rpc.ApiException - if the remote call failspublic final void cancelOperation(com.google.longrunning.CancelOperationRequest request)
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) {
CancelOperationRequest request =
CancelOperationRequest.newBuilder().setName("name3373707").build();
operationsClient.cancelOperation(request);
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<com.google.longrunning.CancelOperationRequest,com.google.protobuf.Empty> cancelOperationCallable()
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) {
CancelOperationRequest request =
CancelOperationRequest.newBuilder().setName("name3373707").build();
ApiFuture<Empty> future = operationsClient.cancelOperationCallable().futureCall(request);
// Do something.
future.get();
}
public final void close()
close in interface AutoCloseablepublic void shutdown()
shutdown in interface com.google.api.gax.core.BackgroundResourcepublic boolean isShutdown()
isShutdown in interface com.google.api.gax.core.BackgroundResourcepublic boolean isTerminated()
isTerminated in interface com.google.api.gax.core.BackgroundResourcepublic void shutdownNow()
shutdownNow in interface com.google.api.gax.core.BackgroundResourcepublic boolean awaitTermination(long duration,
TimeUnit unit)
throws InterruptedException
awaitTermination in interface com.google.api.gax.core.BackgroundResourceInterruptedException