/** * 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`. * * <p>Sample code: * * <pre><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(); * } * </code></pre> */ public final UnaryCallable<CancelOperationRequest, Empty> cancelOperationCallable() { return stub.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`. * * <p>Sample code: * * <pre><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(); * } * </code></pre> */ public final UnaryCallable<CancelOperationRequest, Empty> cancelOperationCallable() { return stub.cancelOperationCallable(); }
@Override public UnaryCallable<String, Void> cancelOperationCallable() { return TranslatingUnaryCallable.create( operationsStub.cancelOperationCallable(), new ApiFunction<String, CancelOperationRequest>() { @Override public CancelOperationRequest apply(String request) { return CancelOperationRequest.newBuilder().setName(request).build(); } }, new ApiFunction<Empty, Void>() { @Override public Void apply(Empty empty) { return null; } }); }
@Override public UnaryCallable<String, Void> cancelOperationCallable() { return TranslatingUnaryCallable.create( operationsStub.cancelOperationCallable(), new ApiFunction<String, CancelOperationRequest>() { @Override public CancelOperationRequest apply(String request) { return CancelOperationRequest.newBuilder().setName(request).build(); } }, new ApiFunction<Empty, Void>() { @Override public Void apply(Empty empty) { return null; } }); }
@Test public void cancel() { OperationsStub operationsStub = mock(OperationsStub.class); when(operationsStub.cancelOperationCallable()) .thenReturn( new UnaryCallable<CancelOperationRequest, Empty>() { @Override public ApiFuture<Empty> futureCall( CancelOperationRequest request, ApiCallContext context) { if (!request.getName().equals("poseidon")) { return ApiFutures.immediateFailedFuture( new IllegalStateException("Unexpected request: " + request)); } else { return ApiFutures.immediateFuture(Empty.getDefaultInstance()); } } }); LongRunningClient longRunningClient = new GrpcLongRunningClient(operationsStub); Truth.assertThat(longRunningClient.cancelOperationCallable().call("poseidon")).isNull(); }