Class OperationsClient
- java.lang.Object
-
- com.google.longrunning.OperationsClient
-
- All Implemented Interfaces:
BackgroundResource
,java.lang.AutoCloseable
public class OperationsClient extends java.lang.Object implements BackgroundResource
Service Description: Manages long-running operations with an API service.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 = ""; 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);
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
OperationsClient.ListOperationsFixedSizeCollection
static class
OperationsClient.ListOperationsPage
static class
OperationsClient.ListOperationsPagedResponse
-
Constructor Summary
Constructors Modifier Constructor Description protected
OperationsClient(OperationsSettings settings)
Constructs an instance of OperationsClient, using the given settings.protected
OperationsClient(OperationsStub stub)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
awaitTermination(long duration, java.util.concurrent.TimeUnit unit)
Blocks until all work has completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.void
cancelOperation(java.lang.String name)
Starts asynchronous cancellation on a long-running operation.UnaryCallable<com.google.longrunning.CancelOperationRequest,com.google.protobuf.Empty>
cancelOperationCallable()
Starts asynchronous cancellation on a long-running operation.void
close()
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(java.lang.String name)
Deletes a long-running operation.UnaryCallable<com.google.longrunning.DeleteOperationRequest,com.google.protobuf.Empty>
deleteOperationCallable()
Deletes a long-running operation.com.google.longrunning.Operation
getOperation(java.lang.String name)
Gets the latest state of a long-running operation.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()
Returns true if this background resource has been shut down.boolean
isTerminated()
Returns true if all work has completed following shut down.OperationsClient.ListOperationsPagedResponse
listOperations(com.google.longrunning.ListOperationsRequest request)
Lists operations that match the specified filter in the request.OperationsClient.ListOperationsPagedResponse
listOperations(java.lang.String name, java.lang.String filter)
Lists operations that match the specified filter in the request.UnaryCallable<com.google.longrunning.ListOperationsRequest,com.google.longrunning.ListOperationsResponse>
listOperationsCallable()
Lists operations that match the specified filter in the request.UnaryCallable<com.google.longrunning.ListOperationsRequest,OperationsClient.ListOperationsPagedResponse>
listOperationsPagedCallable()
Lists operations that match the specified filter in the request.void
shutdown()
Initiates an orderly shutdown in which previously submitted work is finished, but no new work will be accepted.void
shutdownNow()
Attempts to stop all actively executing work and halts the processing of waiting work.com.google.longrunning.Operation
waitOperation(com.google.longrunning.WaitOperationRequest request)
Waits until the specified long-running operation is done or reaches at most a specified timeout, returning the latest state.UnaryCallable<com.google.longrunning.WaitOperationRequest,com.google.longrunning.Operation>
waitOperationCallable()
Waits until the specified long-running operation is done or reaches at most a specified timeout, returning the latest state.
-
-
-
Constructor Detail
-
OperationsClient
protected OperationsClient(OperationsSettings settings) throws java.io.IOException
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:
java.io.IOException
-
OperationsClient
protected OperationsClient(OperationsStub stub)
-
-
Method Detail
-
create
public static final OperationsClient create(OperationsSettings settings) throws java.io.IOException
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:
java.io.IOException
-
create
public static final OperationsClient create(OperationsStub stub)
Constructs an instance of OperationsClient, using the given stub for making calls. This is for advanced usage - prefer to use OperationsSettings}.
-
getSettings
public final OperationsSettings getSettings()
-
getStub
public OperationsStub getStub()
-
getOperation
public final com.google.longrunning.Operation getOperation(java.lang.String name)
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 = ""; Operation response = operationsClient.getOperation(name); }
- Parameters:
name
- The name of the operation resource.- Throws:
ApiException
- if the remote call fails
-
getOperationCallable
public final UnaryCallable<com.google.longrunning.GetOperationRequest,com.google.longrunning.Operation> 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()) { String name = ""; GetOperationRequest request = GetOperationRequest.newBuilder() .setName(name) .build(); ApiFuture<Operation> future = operationsClient.getOperationCallable().futureCall(request); // Do something Operation response = future.get(); }
-
listOperations
public final OperationsClient.ListOperationsPagedResponse listOperations(java.lang.String name, java.lang.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 below allows API services to override the binding to use different resource name schemes, such as `users/*/operations`.
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) { String name = ""; String filter = ""; for (Operation element : operationsClient.listOperations(name, filter).iterateAll()) { // doThingsWith(element); } }
- Parameters:
name
- The name of the operation collection.filter
- The standard list filter.- Throws:
ApiException
- if the remote call fails
-
listOperations
public final OperationsClient.ListOperationsPagedResponse listOperations(com.google.longrunning.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 below allows API services to override the binding to use different resource name schemes, such as `users/*/operations`.
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) { String name = ""; String filter = ""; ListOperationsRequest request = ListOperationsRequest.newBuilder() .setName(name) .setFilter(filter) .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<com.google.longrunning.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 below allows API services to override the binding to use different resource name schemes, such as `users/*/operations`.
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) { String name = ""; String filter = ""; ListOperationsRequest request = ListOperationsRequest.newBuilder() .setName(name) .setFilter(filter) .build(); ApiFuture<ListOperationsPagedResponse> future = operationsClient.listOperationsPagedCallable().futureCall(request); // Do something for (Operation element : future.get().iterateAll()) { // doThingsWith(element); } }
-
listOperationsCallable
public final UnaryCallable<com.google.longrunning.ListOperationsRequest,com.google.longrunning.ListOperationsResponse> 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 below allows API services to override the binding to use different resource name schemes, such as `users/*/operations`.
Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) { String name = ""; String filter = ""; ListOperationsRequest request = ListOperationsRequest.newBuilder() .setName(name) .setFilter(filter) .build(); while (true) { ListOperationsResponse response = operationsClient.listOperationsCallable().call(request); for (Operation element : response.getOperationsList()) { // doThingsWith(element); } String nextPageToken = response.getNextPageToken(); if (!Strings.isNullOrEmpty(nextPageToken)) { request = request.toBuilder().setPageToken(nextPageToken).build(); } else { break; } } }
-
cancelOperation
public final void cancelOperation(java.lang.String name)
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 = ""; operationsClient.cancelOperation(name); }
- Parameters:
name
- The name of the operation resource to be cancelled.- Throws:
ApiException
- if the remote call fails
-
cancelOperationCallable
public final UnaryCallable<com.google.longrunning.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()) { String name = ""; CancelOperationRequest request = CancelOperationRequest.newBuilder() .setName(name) .build(); ApiFuture<Void> future = operationsClient.cancelOperationCallable().futureCall(request); // Do something future.get(); }
-
deleteOperation
public final void deleteOperation(java.lang.String name)
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 = ""; operationsClient.deleteOperation(name); }
- Parameters:
name
- The name of the operation resource to be deleted.- Throws:
ApiException
- if the remote call fails
-
deleteOperationCallable
public final UnaryCallable<com.google.longrunning.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()) { String name = ""; DeleteOperationRequest request = DeleteOperationRequest.newBuilder() .setName(name) .build(); ApiFuture<Void> future = operationsClient.deleteOperationCallable().futureCall(request); // Do something future.get(); }
-
waitOperation
public final com.google.longrunning.Operation waitOperation(com.google.longrunning.WaitOperationRequest request)
Waits until the specified long-running operation is done or reaches at most a specified timeout, returning the latest state. If the operation is already done, the latest state is immediately returned. If the timeout specified is greater than the default HTTP/RPC timeout, the HTTP/RPC timeout is used. If the server does not support this method, it returns `google.rpc.Code.UNIMPLEMENTED`. Note that this method is on a best-effort basis. It may return the latest state before the specified timeout (including immediately), meaning even an immediate response is no guarantee that the operation is done.Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) { String name = ""; WaitOperationRequest request = WaitOperationRequest.newBuilder() .setName(name) .setTimeout(Duration.ofMillis(100)) .build(); Operation response = operationsClient.waitOperation(request); }
- Parameters:
request
- the request object containing all of the parameters for the API call.- Throws:
ApiException
- if the remote call fails
-
waitOperationCallable
public final UnaryCallable<com.google.longrunning.WaitOperationRequest,com.google.longrunning.Operation> waitOperationCallable()
Waits until the specified long-running operation is done or reaches at most a specified timeout, returning the latest state. If the operation is already done, the latest state is immediately returned. If the timeout specified is greater than the default HTTP/RPC timeout, the HTTP/RPC timeout is used. If the server does not support this method, it returns `google.rpc.Code.UNIMPLEMENTED`. Note that this method is on a best-effort basis. It may return the latest state before the specified timeout (including immediately), meaning even an immediate response is no guarantee that the operation is done.Sample code:
try (OperationsClient operationsClient = OperationsClient.create()) { String name = ""; WaitOperationRequest request = WaitOperationRequest.newBuilder() .setName(name) .setTimeout(Duration.ofMillis(100)) .build(); ApiFuture<Operation> future = operationsClient.waitOperationCallable().futureCall(request); }
-
close
public final void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
-
shutdown
public void shutdown()
Description copied from interface:BackgroundResource
Initiates an orderly shutdown in which previously submitted work is finished, but no new work will be accepted. Invocation has no additional effect if already shut down.This method does not wait for previously submitted work to complete execution. Use awaitTermination to do that.
- Specified by:
shutdown
in interfaceBackgroundResource
-
isShutdown
public boolean isShutdown()
Description copied from interface:BackgroundResource
Returns true if this background resource has been shut down.- Specified by:
isShutdown
in interfaceBackgroundResource
-
isTerminated
public boolean isTerminated()
Description copied from interface:BackgroundResource
Returns true if all work has completed following shut down. Note that isTerminated is never true unless either shutdown or shutdownNow was called first.- Specified by:
isTerminated
in interfaceBackgroundResource
-
shutdownNow
public void shutdownNow()
Description copied from interface:BackgroundResource
Attempts to stop all actively executing work and halts the processing of waiting work.This method does not wait for actively executing work to terminate. Use awaitTermination to do that.
There are no guarantees beyond best-effort attempts to stop processing actively executing work. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.
- Specified by:
shutdownNow
in interfaceBackgroundResource
-
awaitTermination
public boolean awaitTermination(long duration, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
Description copied from interface:BackgroundResource
Blocks until all work has completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.- Specified by:
awaitTermination
in interfaceBackgroundResource
- Throws:
java.lang.InterruptedException
-
-