com.google.longrunning.stub.GrpcOperationsStub Maven / Gradle / Ivy
Show all versions of gax-grpc Show documentation
/*
* Copyright 2018 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.longrunning.stub;
import static com.google.longrunning.OperationsClient.ListOperationsPagedResponse;
import com.google.api.gax.core.BackgroundResource;
import com.google.api.gax.core.BackgroundResourceAggregation;
import com.google.api.gax.grpc.GrpcCallSettings;
import com.google.api.gax.grpc.GrpcStubCallableFactory;
import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.RequestParamsExtractor;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.common.collect.ImmutableMap;
import com.google.longrunning.CancelOperationRequest;
import com.google.longrunning.DeleteOperationRequest;
import com.google.longrunning.GetOperationRequest;
import com.google.longrunning.ListOperationsRequest;
import com.google.longrunning.ListOperationsResponse;
import com.google.longrunning.Operation;
import com.google.longrunning.WaitOperationRequest;
import com.google.protobuf.Empty;
import io.grpc.MethodDescriptor;
import io.grpc.protobuf.ProtoUtils;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* gRPC stub implementation for Google Long Running Operations API.
*
* This class is for advanced usage and reflects the underlying API directly.
*/
public class GrpcOperationsStub extends OperationsStub {
private static final MethodDescriptor
getOperationMethodDescriptor =
MethodDescriptor.newBuilder()
.setType(MethodDescriptor.MethodType.UNARY)
.setFullMethodName("google.longrunning.Operations/GetOperation")
.setRequestMarshaller(ProtoUtils.marshaller(GetOperationRequest.getDefaultInstance()))
.setResponseMarshaller(ProtoUtils.marshaller(Operation.getDefaultInstance()))
.build();
private static final MethodDescriptor
listOperationsMethodDescriptor =
MethodDescriptor.newBuilder()
.setType(MethodDescriptor.MethodType.UNARY)
.setFullMethodName("google.longrunning.Operations/ListOperations")
.setRequestMarshaller(
ProtoUtils.marshaller(ListOperationsRequest.getDefaultInstance()))
.setResponseMarshaller(
ProtoUtils.marshaller(ListOperationsResponse.getDefaultInstance()))
.build();
private static final MethodDescriptor
cancelOperationMethodDescriptor =
MethodDescriptor.newBuilder()
.setType(MethodDescriptor.MethodType.UNARY)
.setFullMethodName("google.longrunning.Operations/CancelOperation")
.setRequestMarshaller(
ProtoUtils.marshaller(CancelOperationRequest.getDefaultInstance()))
.setResponseMarshaller(ProtoUtils.marshaller(Empty.getDefaultInstance()))
.build();
private static final MethodDescriptor
deleteOperationMethodDescriptor =
MethodDescriptor.newBuilder()
.setType(MethodDescriptor.MethodType.UNARY)
.setFullMethodName("google.longrunning.Operations/DeleteOperation")
.setRequestMarshaller(
ProtoUtils.marshaller(DeleteOperationRequest.getDefaultInstance()))
.setResponseMarshaller(ProtoUtils.marshaller(Empty.getDefaultInstance()))
.build();
private static final MethodDescriptor
waitOperationMethodDescriptor =
MethodDescriptor.newBuilder()
.setType(MethodDescriptor.MethodType.UNARY)
.setFullMethodName("google.longrunning.Operations/WaitOperation")
.setRequestMarshaller(
ProtoUtils.marshaller(WaitOperationRequest.getDefaultInstance()))
.setResponseMarshaller(ProtoUtils.marshaller(Operation.getDefaultInstance()))
.build();
private final BackgroundResource backgroundResources;
private final UnaryCallable getOperationCallable;
private final UnaryCallable listOperationsCallable;
private final UnaryCallable
listOperationsPagedCallable;
private final UnaryCallable cancelOperationCallable;
private final UnaryCallable deleteOperationCallable;
private final UnaryCallable waitOperationCallable;
private final GrpcStubCallableFactory callableFactory;
public static final GrpcOperationsStub create(OperationsStubSettings settings)
throws IOException {
return new GrpcOperationsStub(settings, ClientContext.create(settings));
}
public static final GrpcOperationsStub create(ClientContext clientContext) throws IOException {
return new GrpcOperationsStub(OperationsStubSettings.newBuilder().build(), clientContext);
}
public static final GrpcOperationsStub create(
ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException {
return new GrpcOperationsStub(
OperationsStubSettings.newBuilder().build(), clientContext, callableFactory);
}
/**
* Constructs an instance of GrpcOperationsStub, 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.
*/
protected GrpcOperationsStub(OperationsStubSettings settings, ClientContext clientContext)
throws IOException {
this(settings, clientContext, new GrpcOperationsCallableFactory());
}
/**
* Constructs an instance of GrpcOperationsStub, 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.
*/
protected GrpcOperationsStub(
OperationsStubSettings settings,
ClientContext clientContext,
GrpcStubCallableFactory callableFactory)
throws IOException {
this.callableFactory = callableFactory;
GrpcCallSettings getOperationTransportSettings =
GrpcCallSettings.newBuilder()
.setMethodDescriptor(getOperationMethodDescriptor)
.setParamsExtractor(
new RequestParamsExtractor() {
@Override
public Map extract(GetOperationRequest request) {
ImmutableMap.Builder params = ImmutableMap.builder();
params.put("name", String.valueOf(request.getName()));
return params.build();
}
})
.build();
GrpcCallSettings
listOperationsTransportSettings =
GrpcCallSettings.newBuilder()
.setMethodDescriptor(listOperationsMethodDescriptor)
.setParamsExtractor(
new RequestParamsExtractor() {
@Override
public Map extract(ListOperationsRequest request) {
ImmutableMap.Builder params = ImmutableMap.builder();
params.put("name", String.valueOf(request.getName()));
return params.build();
}
})
.build();
GrpcCallSettings cancelOperationTransportSettings =
GrpcCallSettings.newBuilder()
.setMethodDescriptor(cancelOperationMethodDescriptor)
.setParamsExtractor(
new RequestParamsExtractor() {
@Override
public Map extract(CancelOperationRequest request) {
ImmutableMap.Builder params = ImmutableMap.builder();
params.put("name", String.valueOf(request.getName()));
return params.build();
}
})
.build();
GrpcCallSettings deleteOperationTransportSettings =
GrpcCallSettings.newBuilder()
.setMethodDescriptor(deleteOperationMethodDescriptor)
.setParamsExtractor(
new RequestParamsExtractor() {
@Override
public Map extract(DeleteOperationRequest request) {
ImmutableMap.Builder params = ImmutableMap.builder();
params.put("name", String.valueOf(request.getName()));
return params.build();
}
})
.build();
GrpcCallSettings waitOperationTransportSettings =
GrpcCallSettings.newBuilder()
.setMethodDescriptor(waitOperationMethodDescriptor)
.setParamsExtractor(
new RequestParamsExtractor() {
@Override
public Map extract(WaitOperationRequest request) {
ImmutableMap.Builder params = ImmutableMap.builder();
params.put("name", String.valueOf(request.getName()));
return params.build();
}
})
.build();
this.getOperationCallable =
callableFactory.createUnaryCallable(
getOperationTransportSettings, settings.getOperationSettings(), clientContext);
this.listOperationsCallable =
callableFactory.createUnaryCallable(
listOperationsTransportSettings, settings.listOperationsSettings(), clientContext);
this.listOperationsPagedCallable =
callableFactory.createPagedCallable(
listOperationsTransportSettings, settings.listOperationsSettings(), clientContext);
this.cancelOperationCallable =
callableFactory.createUnaryCallable(
cancelOperationTransportSettings, settings.cancelOperationSettings(), clientContext);
this.deleteOperationCallable =
callableFactory.createUnaryCallable(
deleteOperationTransportSettings, settings.deleteOperationSettings(), clientContext);
this.waitOperationCallable =
callableFactory.createUnaryCallable(
waitOperationTransportSettings, settings.waitOperationSettings(), clientContext);
backgroundResources = new BackgroundResourceAggregation(clientContext.getBackgroundResources());
}
@Override
public UnaryCallable getOperationCallable() {
return getOperationCallable;
}
@Override
public UnaryCallable
listOperationsPagedCallable() {
return listOperationsPagedCallable;
}
@Override
public UnaryCallable listOperationsCallable() {
return listOperationsCallable;
}
@Override
public UnaryCallable cancelOperationCallable() {
return cancelOperationCallable;
}
@Override
public UnaryCallable deleteOperationCallable() {
return deleteOperationCallable;
}
@Override
public UnaryCallable waitOperationCallable() {
return waitOperationCallable;
}
@Override
public final void close() {
shutdown();
}
@Override
public void shutdown() {
backgroundResources.shutdown();
}
@Override
public boolean isShutdown() {
return backgroundResources.isShutdown();
}
@Override
public boolean isTerminated() {
return backgroundResources.isTerminated();
}
@Override
public void shutdownNow() {
backgroundResources.shutdownNow();
}
@Override
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
return backgroundResources.awaitTermination(duration, unit);
}
}