Class OperationsClient
- All Implemented Interfaces:
BackgroundResource,AutoCloseable
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:
- A "flattened" method. With this type of method, the fields of the request type have been converted into function parameters. It may be the case that not all fields are available as parameters, and not every API method will have a flattened method entry point.
- A "request object" method. This type of method only takes one parameter, a request object, which must be constructed before the call. Not every API method will have a request object method.
- A "callable" method. This type of method takes no parameters and returns an immutable API callable object, which can be used to initiate calls to the service.
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classstatic class -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedOperationsClient(OperationsSettings settings) Constructs an instance of OperationsClient, using the given settings.protected -
Method Summary
Modifier and TypeMethodDescriptionbooleanawaitTermination(long duration, TimeUnit unit) final voidcancelOperation(CancelOperationRequest request) Starts asynchronous cancellation on a long-running operation.final voidcancelOperation(String name) Starts asynchronous cancellation on a long-running operation.final UnaryCallable<CancelOperationRequest,com.google.protobuf.Empty> Starts asynchronous cancellation on a long-running operation.final voidclose()static final OperationsClientcreate()Constructs an instance of OperationsClient with default settings.static final OperationsClientcreate(BackgroundResource stub) Constructs an instance of OperationsClient, using the given stub for making calls.static final OperationsClientcreate(OperationsSettings settings) Constructs an instance of OperationsClient, using the given settings.static final OperationsClientcreate(OperationsStub stub) Constructs an instance of OperationsClient, using the given stub for making calls.final voiddeleteOperation(DeleteOperationRequest request) Deletes a long-running operation.final voiddeleteOperation(String name) Deletes a long-running operation.final UnaryCallable<DeleteOperationRequest,com.google.protobuf.Empty> Deletes a long-running operation.final OperationgetOperation(GetOperationRequest request) Gets the latest state of a long-running operation.final OperationgetOperation(String name) Gets the latest state of a long-running operation.Gets the latest state of a long-running operation.final OperationsSettingsgetStub()booleanbooleanlistOperations(ListOperationsRequest request) Lists operations that match the specified filter in the request.listOperations(String name, String filter) Lists operations that match the specified filter in the request.Lists operations that match the specified filter in the request.Lists operations that match the specified filter in the request.voidshutdown()void
-
Constructor Details
-
OperationsClient
Constructs an instance of OperationsClient, using the given settings. This is protected so that it is easy to make a subclass, but otherwise, the static factory methods should be preferred.- Throws:
IOException
-
OperationsClient
-
-
Method Details
-
create
Constructs an instance of OperationsClient with default settings.- Throws:
IOException
-
create
Constructs an instance of OperationsClient, using the given settings. The channels are created based on the settings passed in, or defaults for any settings that are not set.- Throws:
IOException
-
create
Constructs an instance of OperationsClient, using the given stub for making calls. This is for advanced usage - prefer using create(OperationsSettings). -
create
Constructs an instance of OperationsClient, using the given stub for making calls. This is for advanced usage - prefer using create(OperationsSettings). -
getSettings
-
getStub
-
listOperations
public final OperationsClient.ListOperationsPagedResponse listOperations(String name, String filter) Lists operations that match the specified filter in the request. If the server doesn't support this method, it returns `UNIMPLEMENTED`.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); } }- Parameters:
name- The name of the operation's parent resource.filter- The standard list filter.- Throws:
ApiException- if the remote call fails
-
listOperations
public final OperationsClient.ListOperationsPagedResponse listOperations(ListOperationsRequest request) Lists operations that match the specified filter in the request. If the server doesn't support this method, it returns `UNIMPLEMENTED`.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); } }- Parameters:
request- The request object containing all of the parameters for the API call.- Throws:
ApiException- if the remote call fails
-
listOperationsPagedCallable
public final UnaryCallable<ListOperationsRequest,OperationsClient.ListOperationsPagedResponse> listOperationsPagedCallable()Lists operations that match the specified filter in the request. If the server doesn't support this method, it returns `UNIMPLEMENTED`.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); } } -
listOperationsCallable
Lists operations that match the specified filter in the request. If the server doesn't support this method, it returns `UNIMPLEMENTED`.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; } } } -
getOperation
Gets the latest state of a long-running operation. Clients can use this method to poll the operation result at intervals as recommended by the API service.Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) { String name = "name3373707"; Operation response = operationsClient.getOperation(name); }- Parameters:
name- The name of the operation resource.- Throws:
ApiException- if the remote call fails
-
getOperation
Gets the latest state of a long-running operation. Clients can use this method to poll the operation result at intervals as recommended by the API service.Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) { GetOperationRequest request = GetOperationRequest.newBuilder().setName("name3373707").build(); Operation response = operationsClient.getOperation(request); }- Parameters:
request- The request object containing all of the parameters for the API call.- Throws:
ApiException- if the remote call fails
-
getOperationCallable
Gets the latest state of a long-running operation. Clients can use this method to poll the operation result at intervals as recommended by the API service.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(); } -
deleteOperation
Deletes a long-running operation. This method indicates that the client is no longer interested in the operation result. It does not cancel the operation. If the server doesn't support this method, it returns `google.rpc.Code.UNIMPLEMENTED`.Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) { String name = "name3373707"; operationsClient.deleteOperation(name); }- Parameters:
name- The name of the operation resource to be deleted.- Throws:
ApiException- if the remote call fails
-
deleteOperation
Deletes a long-running operation. This method indicates that the client is no longer interested in the operation result. It does not cancel the operation. If the server doesn't support this method, it returns `google.rpc.Code.UNIMPLEMENTED`.Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) { DeleteOperationRequest request = DeleteOperationRequest.newBuilder().setName("name3373707").build(); operationsClient.deleteOperation(request); }- Parameters:
request- The request object containing all of the parameters for the API call.- Throws:
ApiException- if the remote call fails
-
deleteOperationCallable
public final UnaryCallable<DeleteOperationRequest,com.google.protobuf.Empty> deleteOperationCallable()Deletes a long-running operation. This method indicates that the client is no longer interested in the operation result. It does not cancel the operation. If the server doesn't support this method, it returns `google.rpc.Code.UNIMPLEMENTED`.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(); } -
cancelOperation
Starts asynchronous cancellation on a long-running operation. The server makes a best effort to cancel the operation, but success is not guaranteed. If the server doesn't support this method, it returns `google.rpc.Code.UNIMPLEMENTED`. Clients can use [Operations.GetOperation][google.longrunning.Operations.GetOperation] or other methods to check whether the cancellation succeeded or whether the operation completed despite cancellation. On successful cancellation, the operation is not deleted; instead, it becomes an operation with an [Operation.error][google.longrunning.Operation.error] value with a [google.rpc.Status.code][google.rpc.Status.code] of 1, corresponding to `Code.CANCELLED`.Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) { String name = "name3373707"; operationsClient.cancelOperation(name); }- Parameters:
name- The name of the operation resource to be cancelled.- Throws:
ApiException- if the remote call fails
-
cancelOperation
Starts asynchronous cancellation on a long-running operation. The server makes a best effort to cancel the operation, but success is not guaranteed. If the server doesn't support this method, it returns `google.rpc.Code.UNIMPLEMENTED`. Clients can use [Operations.GetOperation][google.longrunning.Operations.GetOperation] or other methods to check whether the cancellation succeeded or whether the operation completed despite cancellation. On successful cancellation, the operation is not deleted; instead, it becomes an operation with an [Operation.error][google.longrunning.Operation.error] value with a [google.rpc.Status.code][google.rpc.Status.code] of 1, corresponding to `Code.CANCELLED`.Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) { CancelOperationRequest request = CancelOperationRequest.newBuilder().setName("name3373707").build(); operationsClient.cancelOperation(request); }- Parameters:
request- The request object containing all of the parameters for the API call.- Throws:
ApiException- if the remote call fails
-
cancelOperationCallable
public final UnaryCallable<CancelOperationRequest,com.google.protobuf.Empty> cancelOperationCallable()Starts asynchronous cancellation on a long-running operation. The server makes a best effort to cancel the operation, but success is not guaranteed. If the server doesn't support this method, it returns `google.rpc.Code.UNIMPLEMENTED`. Clients can use [Operations.GetOperation][google.longrunning.Operations.GetOperation] or other methods to check whether the cancellation succeeded or whether the operation completed despite cancellation. On successful cancellation, the operation is not deleted; instead, it becomes an operation with an [Operation.error][google.longrunning.Operation.error] value with a [google.rpc.Status.code][google.rpc.Status.code] of 1, corresponding to `Code.CANCELLED`.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(); } -
close
public final void close()- Specified by:
closein interfaceAutoCloseable
-
shutdown
public void shutdown()- Specified by:
shutdownin interfaceBackgroundResource
-
isShutdown
public boolean isShutdown()- Specified by:
isShutdownin interfaceBackgroundResource
-
isTerminated
public boolean isTerminated()- Specified by:
isTerminatedin interfaceBackgroundResource
-
shutdownNow
public void shutdownNow()- Specified by:
shutdownNowin interfaceBackgroundResource
-
awaitTermination
- Specified by:
awaitTerminationin interfaceBackgroundResource- Throws:
InterruptedException
-