From d5b646ef14d440708fb2c72d59e74c0095d1e217 Mon Sep 17 00:00:00 2001 From: Shijie Sheng Date: Thu, 17 Apr 2025 09:27:10 -0700 Subject: [PATCH] change IWorkflowServiceV4 to have both future and blocking methods to simplify usage --- .../serviceclient/IWorkflowServiceV4.java | 205 ++-- .../serviceclient/WorkflowServiceGrpc.java | 880 +++++++++++++----- 2 files changed, 804 insertions(+), 281 deletions(-) diff --git a/src/main/java/com/uber/cadence/serviceclient/IWorkflowServiceV4.java b/src/main/java/com/uber/cadence/serviceclient/IWorkflowServiceV4.java index 9d2401930..e142eaa10 100644 --- a/src/main/java/com/uber/cadence/serviceclient/IWorkflowServiceV4.java +++ b/src/main/java/com/uber/cadence/serviceclient/IWorkflowServiceV4.java @@ -22,82 +22,181 @@ import javax.annotation.Nullable; public interface IWorkflowServiceV4 { - CompletableFuture startWorkflowExecution( - StartWorkflowExecutionRequest request, @Nullable CallMetaData meta); + Blocking blockingStub(); + Future futureStub(); + ClientOptions getOptions(); + CompletableFuture isHealthy(); - CompletableFuture startWorkflowExecutionAsync( - StartWorkflowExecutionAsyncRequest request, @Nullable CallMetaData meta); + interface Blocking { + StartWorkflowExecutionResponse startWorkflowExecution( + StartWorkflowExecutionRequest request, @Nullable CallMetaData meta); - CompletableFuture signalWorkflowExecution( - SignalWorkflowExecutionRequest request, @Nullable CallMetaData meta); + StartWorkflowExecutionAsyncResponse startWorkflowExecutionAsync( + StartWorkflowExecutionAsyncRequest request, @Nullable CallMetaData meta); - CompletableFuture signalWithStartWorkflowExecution( - SignalWithStartWorkflowExecutionRequest request, @Nullable CallMetaData meta); + SignalWorkflowExecutionResponse signalWorkflowExecution( + SignalWorkflowExecutionRequest request, @Nullable CallMetaData meta); - CompletableFuture - signalWithStartWorkflowExecutionAsync( - SignalWithStartWorkflowExecutionAsyncRequest request, @Nullable CallMetaData meta); + SignalWithStartWorkflowExecutionResponse signalWithStartWorkflowExecution( + SignalWithStartWorkflowExecutionRequest request, @Nullable CallMetaData meta); - CompletableFuture getWorkflowExecutionHistory( - GetWorkflowExecutionHistoryRequest request, @Nullable CallMetaData meta); + SignalWithStartWorkflowExecutionAsyncResponse signalWithStartWorkflowExecutionAsync( + SignalWithStartWorkflowExecutionAsyncRequest request, @Nullable CallMetaData meta); - CompletableFuture queryWorkflow( - QueryWorkflowRequest request, @Nullable CallMetaData meta); + GetWorkflowExecutionHistoryResponse getWorkflowExecutionHistory( + GetWorkflowExecutionHistoryRequest request, @Nullable CallMetaData meta); - CompletableFuture requestCancelWorkflowExecution( - RequestCancelWorkflowExecutionRequest request, @Nullable CallMetaData meta); + QueryWorkflowResponse queryWorkflow(QueryWorkflowRequest request, @Nullable CallMetaData meta); - CompletableFuture terminateWorkflowExecution( - TerminateWorkflowExecutionRequest request, @Nullable CallMetaData meta); + RequestCancelWorkflowExecutionResponse requestCancelWorkflowExecution( + RequestCancelWorkflowExecutionRequest request, @Nullable CallMetaData meta); - CompletableFuture restartWorkflowExecution( - RestartWorkflowExecutionRequest request, @Nullable CallMetaData meta); + TerminateWorkflowExecutionResponse terminateWorkflowExecution( + TerminateWorkflowExecutionRequest request, @Nullable CallMetaData meta); - CompletableFuture listWorkflowExecutions( - ListWorkflowExecutionsRequest request, @Nullable CallMetaData meta); + RestartWorkflowExecutionResponse restartWorkflowExecution( + RestartWorkflowExecutionRequest request, @Nullable CallMetaData meta); - CompletableFuture scanWorkflowExecutions( - ScanWorkflowExecutionsRequest request, @Nullable CallMetaData meta); + ListWorkflowExecutionsResponse listWorkflowExecutions( + ListWorkflowExecutionsRequest request, @Nullable CallMetaData meta); - CompletableFuture listOpenWorkflowExecutions( - ListOpenWorkflowExecutionsRequest request, @Nullable CallMetaData meta); + ListArchivedWorkflowExecutionsResponse listArchivedWorkflowExecutions( + ListArchivedWorkflowExecutionsRequest listRequest, @Nullable CallMetaData meta); - CompletableFuture listClosedWorkflowExecutions( - ListClosedWorkflowExecutionsRequest request, @Nullable CallMetaData meta); + ScanWorkflowExecutionsResponse scanWorkflowExecutions( + ScanWorkflowExecutionsRequest request, @Nullable CallMetaData meta); - CompletableFuture countWorkflowExecutions( - CountWorkflowExecutionsRequest request, @Nullable CallMetaData meta); + ListOpenWorkflowExecutionsResponse listOpenWorkflowExecutions( + ListOpenWorkflowExecutionsRequest request, @Nullable CallMetaData meta); - CompletableFuture pollForActivityTask( - PollForActivityTaskRequest request, @Nullable CallMetaData meta); + ListClosedWorkflowExecutionsResponse listClosedWorkflowExecutions( + ListClosedWorkflowExecutionsRequest request, @Nullable CallMetaData meta); - CompletableFuture recordActivityTaskHeartbeat( - RecordActivityTaskHeartbeatRequest request, @Nullable CallMetaData meta); + CountWorkflowExecutionsResponse countWorkflowExecutions( + CountWorkflowExecutionsRequest request, @Nullable CallMetaData meta); - CompletableFuture respondActivityTaskCanceled( - RespondActivityTaskCanceledRequest request, @Nullable CallMetaData meta); + PollForActivityTaskResponse pollForActivityTask( + PollForActivityTaskRequest request, @Nullable CallMetaData meta); - CompletableFuture respondActivityTaskCanceledByID( - RespondActivityTaskCanceledByIDRequest request, @Nullable CallMetaData meta); + RecordActivityTaskHeartbeatResponse recordActivityTaskHeartbeat( + RecordActivityTaskHeartbeatRequest request, @Nullable CallMetaData meta); - CompletableFuture respondActivityTaskFailed( - RespondActivityTaskFailedRequest request, @Nullable CallMetaData meta); + RespondActivityTaskCanceledResponse respondActivityTaskCanceled( + RespondActivityTaskCanceledRequest request, @Nullable CallMetaData meta); - CompletableFuture respondActivityTaskFailedByID( - RespondActivityTaskFailedByIDRequest request, @Nullable CallMetaData meta); + RespondActivityTaskCanceledByIDResponse respondActivityTaskCanceledByID( + RespondActivityTaskCanceledByIDRequest request, @Nullable CallMetaData meta); - CompletableFuture respondActivityTaskCompleted( - RespondActivityTaskCompletedRequest request, @Nullable CallMetaData meta); + RespondActivityTaskFailedResponse respondActivityTaskFailed( + RespondActivityTaskFailedRequest request, @Nullable CallMetaData meta); - CompletableFuture respondActivityTaskCompletedByID( - RespondActivityTaskCompletedByIDRequest request, @Nullable CallMetaData meta); + RespondActivityTaskFailedByIDResponse respondActivityTaskFailedByID( + RespondActivityTaskFailedByIDRequest request, @Nullable CallMetaData meta); - CompletableFuture pollForDecisionTask( - PollForDecisionTaskRequest request, @Nullable CallMetaData meta); + RespondActivityTaskCompletedResponse respondActivityTaskCompleted( + RespondActivityTaskCompletedRequest request, @Nullable CallMetaData meta); - CompletableFuture respondDecisionTaskFailed( - RespondDecisionTaskFailedRequest request, @Nullable CallMetaData meta); + RespondActivityTaskCompletedByIDResponse respondActivityTaskCompletedByID( + RespondActivityTaskCompletedByIDRequest request, @Nullable CallMetaData meta); - CompletableFuture respondDecisionTaskCompleted( - RespondDecisionTaskCompletedRequest request, @Nullable CallMetaData meta); + PollForDecisionTaskResponse pollForDecisionTask( + PollForDecisionTaskRequest request, @Nullable CallMetaData meta); + + RespondDecisionTaskFailedResponse respondDecisionTaskFailed( + RespondDecisionTaskFailedRequest request, @Nullable CallMetaData meta); + + RespondDecisionTaskCompletedResponse respondDecisionTaskCompleted( + RespondDecisionTaskCompletedRequest request, @Nullable CallMetaData meta); + + RefreshWorkflowTasksResponse refreshWorkflowTasks( + RefreshWorkflowTasksRequest request, @Nullable CallMetaData meta); + } + + interface Future { + + CompletableFuture startWorkflowExecution( + StartWorkflowExecutionRequest request, @Nullable CallMetaData meta); + + CompletableFuture startWorkflowExecutionAsync( + StartWorkflowExecutionAsyncRequest request, @Nullable CallMetaData meta); + + CompletableFuture signalWorkflowExecution( + SignalWorkflowExecutionRequest request, @Nullable CallMetaData meta); + + CompletableFuture signalWithStartWorkflowExecution( + SignalWithStartWorkflowExecutionRequest request, @Nullable CallMetaData meta); + + CompletableFuture + signalWithStartWorkflowExecutionAsync( + SignalWithStartWorkflowExecutionAsyncRequest request, @Nullable CallMetaData meta); + + CompletableFuture getWorkflowExecutionHistory( + GetWorkflowExecutionHistoryRequest request, @Nullable CallMetaData meta); + + CompletableFuture queryWorkflow( + QueryWorkflowRequest request, @Nullable CallMetaData meta); + + CompletableFuture requestCancelWorkflowExecution( + RequestCancelWorkflowExecutionRequest request, @Nullable CallMetaData meta); + + CompletableFuture terminateWorkflowExecution( + TerminateWorkflowExecutionRequest request, @Nullable CallMetaData meta); + + CompletableFuture restartWorkflowExecution( + RestartWorkflowExecutionRequest request, @Nullable CallMetaData meta); + + CompletableFuture listWorkflowExecutions( + ListWorkflowExecutionsRequest request, @Nullable CallMetaData meta); + + CompletableFuture scanWorkflowExecutions( + ScanWorkflowExecutionsRequest request, @Nullable CallMetaData meta); + + CompletableFuture listOpenWorkflowExecutions( + ListOpenWorkflowExecutionsRequest request, @Nullable CallMetaData meta); + + CompletableFuture listClosedWorkflowExecutions( + ListClosedWorkflowExecutionsRequest request, @Nullable CallMetaData meta); + + CompletableFuture listArchivedWorkflowExecutions( + ListArchivedWorkflowExecutionsRequest listRequest, @Nullable CallMetaData meta); + + CompletableFuture countWorkflowExecutions( + CountWorkflowExecutionsRequest request, @Nullable CallMetaData meta); + + CompletableFuture pollForActivityTask( + PollForActivityTaskRequest request, @Nullable CallMetaData meta); + + CompletableFuture recordActivityTaskHeartbeat( + RecordActivityTaskHeartbeatRequest request, @Nullable CallMetaData meta); + + CompletableFuture respondActivityTaskCanceled( + RespondActivityTaskCanceledRequest request, @Nullable CallMetaData meta); + + CompletableFuture respondActivityTaskCanceledByID( + RespondActivityTaskCanceledByIDRequest request, @Nullable CallMetaData meta); + + CompletableFuture respondActivityTaskFailed( + RespondActivityTaskFailedRequest request, @Nullable CallMetaData meta); + + CompletableFuture respondActivityTaskFailedByID( + RespondActivityTaskFailedByIDRequest request, @Nullable CallMetaData meta); + + CompletableFuture respondActivityTaskCompleted( + RespondActivityTaskCompletedRequest request, @Nullable CallMetaData meta); + + CompletableFuture respondActivityTaskCompletedByID( + RespondActivityTaskCompletedByIDRequest request, @Nullable CallMetaData meta); + + CompletableFuture pollForDecisionTask( + PollForDecisionTaskRequest request, @Nullable CallMetaData meta); + + CompletableFuture respondDecisionTaskFailed( + RespondDecisionTaskFailedRequest request, @Nullable CallMetaData meta); + + CompletableFuture respondDecisionTaskCompleted( + RespondDecisionTaskCompletedRequest request, @Nullable CallMetaData meta); + + CompletableFuture refreshWorkflowTasks( + RefreshWorkflowTasksRequest request, @Nullable CallMetaData meta); + } } diff --git a/src/main/java/com/uber/cadence/serviceclient/WorkflowServiceGrpc.java b/src/main/java/com/uber/cadence/serviceclient/WorkflowServiceGrpc.java index 8520cbab6..122a7dbb7 100644 --- a/src/main/java/com/uber/cadence/serviceclient/WorkflowServiceGrpc.java +++ b/src/main/java/com/uber/cadence/serviceclient/WorkflowServiceGrpc.java @@ -22,10 +22,14 @@ import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.uber.cadence.api.v1.*; +import com.uber.cadence.internal.compatibility.proto.ErrorMapper; import com.uber.cadence.internal.compatibility.proto.serviceclient.IGrpcServiceStubs; +import com.uber.cadence.serviceclient.exceptions.ServiceClientException; +import com.uber.cadence.workflow.Functions; import io.grpc.*; import java.time.Duration; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; @@ -38,266 +42,676 @@ public class WorkflowServiceGrpc implements IWorkflowServiceV4 { } @Override - public CompletableFuture startWorkflowExecution( - StartWorkflowExecutionRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workflowFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .startWorkflowExecution(request)); + public ClientOptions getOptions() { + return grpcServiceStubs.getOptions(); } @Override - public CompletableFuture startWorkflowExecutionAsync( - StartWorkflowExecutionAsyncRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workflowFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .startWorkflowExecutionAsync(request)); - } + public CompletableFuture isHealthy() { - @Override - public CompletableFuture signalWorkflowExecution( - SignalWorkflowExecutionRequest request, @Nullable CallMetaData meta) { return toCompletableFuture( grpcServiceStubs - .workflowFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .signalWorkflowExecution(request)); + .metaFutureStub() + .health(HealthRequest.getDefaultInstance())) + .thenApply(HealthResponse::getOk) + .exceptionally( throwable -> { + throw toServiceClientException(throwable); + }); } @Override - public CompletableFuture - signalWithStartWorkflowExecution( - SignalWithStartWorkflowExecutionRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workflowFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .signalWithStartWorkflowExecution(request)); - } + public Blocking blockingStub() { + return new Blocking() { + @Override + public StartWorkflowExecutionResponse startWorkflowExecution( + StartWorkflowExecutionRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workflowBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .startWorkflowExecution(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture - signalWithStartWorkflowExecutionAsync( - SignalWithStartWorkflowExecutionAsyncRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workflowFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .signalWithStartWorkflowExecutionAsync(request)); - } + @Override + public StartWorkflowExecutionAsyncResponse startWorkflowExecutionAsync( + StartWorkflowExecutionAsyncRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workflowBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .startWorkflowExecutionAsync(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture getWorkflowExecutionHistory( - GetWorkflowExecutionHistoryRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workflowFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .getWorkflowExecutionHistory(request)); - } + @Override + public SignalWorkflowExecutionResponse signalWorkflowExecution( + SignalWorkflowExecutionRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workflowBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .signalWorkflowExecution(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture queryWorkflow( - QueryWorkflowRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workflowFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .queryWorkflow(request)); - } + @Override + public SignalWithStartWorkflowExecutionResponse signalWithStartWorkflowExecution( + SignalWithStartWorkflowExecutionRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workflowBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .signalWithStartWorkflowExecution(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture requestCancelWorkflowExecution( - RequestCancelWorkflowExecutionRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workflowFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .requestCancelWorkflowExecution(request)); - } + @Override + public SignalWithStartWorkflowExecutionAsyncResponse signalWithStartWorkflowExecutionAsync( + SignalWithStartWorkflowExecutionAsyncRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workflowBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .signalWithStartWorkflowExecutionAsync(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture terminateWorkflowExecution( - TerminateWorkflowExecutionRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workflowFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .terminateWorkflowExecution(request)); - } + @Override + public GetWorkflowExecutionHistoryResponse getWorkflowExecutionHistory( + GetWorkflowExecutionHistoryRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workflowBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .getWorkflowExecutionHistory(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture restartWorkflowExecution( - RestartWorkflowExecutionRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workflowFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .restartWorkflowExecution(request)); - } + @Override + public QueryWorkflowResponse queryWorkflow( + QueryWorkflowRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workflowBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .queryWorkflow(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture listWorkflowExecutions( - ListWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .visibilityFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .listWorkflowExecutions(request)); - } + @Override + public RequestCancelWorkflowExecutionResponse requestCancelWorkflowExecution( + RequestCancelWorkflowExecutionRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workflowBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .requestCancelWorkflowExecution(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture scanWorkflowExecutions( - ScanWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .visibilityFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .scanWorkflowExecutions(request)); - } + @Override + public TerminateWorkflowExecutionResponse terminateWorkflowExecution( + TerminateWorkflowExecutionRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workflowBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .terminateWorkflowExecution(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture listOpenWorkflowExecutions( - ListOpenWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .visibilityFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .listOpenWorkflowExecutions(request)); - } + @Override + public RestartWorkflowExecutionResponse restartWorkflowExecution( + RestartWorkflowExecutionRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workflowBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .restartWorkflowExecution(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture listClosedWorkflowExecutions( - ListClosedWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .visibilityFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .listClosedWorkflowExecutions(request)); - } + @Override + public ListWorkflowExecutionsResponse listWorkflowExecutions( + ListWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .visibilityBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .listWorkflowExecutions(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture countWorkflowExecutions( - CountWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .visibilityFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .countWorkflowExecutions(request)); - } + @Override + public ListArchivedWorkflowExecutionsResponse listArchivedWorkflowExecutions(ListArchivedWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .visibilityBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .listArchivedWorkflowExecutions(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture pollForActivityTask( - PollForActivityTaskRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workerFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .pollForActivityTask(request)); - } + @Override + public ScanWorkflowExecutionsResponse scanWorkflowExecutions( + ScanWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .visibilityBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .scanWorkflowExecutions(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture recordActivityTaskHeartbeat( - RecordActivityTaskHeartbeatRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workerFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .recordActivityTaskHeartbeat(request)); - } + @Override + public ListOpenWorkflowExecutionsResponse listOpenWorkflowExecutions( + ListOpenWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .visibilityBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .listOpenWorkflowExecutions(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture respondActivityTaskCanceled( - RespondActivityTaskCanceledRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workerFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .respondActivityTaskCanceled(request)); - } + @Override + public ListClosedWorkflowExecutionsResponse listClosedWorkflowExecutions( + ListClosedWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .visibilityBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .listClosedWorkflowExecutions(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture respondActivityTaskCanceledByID( - RespondActivityTaskCanceledByIDRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workerFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .respondActivityTaskCanceledByID(request)); - } + @Override + public CountWorkflowExecutionsResponse countWorkflowExecutions( + CountWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .visibilityBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .countWorkflowExecutions(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture respondActivityTaskFailed( - RespondActivityTaskFailedRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workerFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .respondActivityTaskFailed(request)); - } + @Override + public PollForActivityTaskResponse pollForActivityTask( + PollForActivityTaskRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workerBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .pollForActivityTask(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture respondActivityTaskFailedByID( - RespondActivityTaskFailedByIDRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workerFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .respondActivityTaskFailedByID(request)); - } + @Override + public RecordActivityTaskHeartbeatResponse recordActivityTaskHeartbeat( + RecordActivityTaskHeartbeatRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workerBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .recordActivityTaskHeartbeat(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture respondActivityTaskCompleted( - RespondActivityTaskCompletedRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workerFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .respondActivityTaskCompleted(request)); - } + @Override + public RespondActivityTaskCanceledResponse respondActivityTaskCanceled( + RespondActivityTaskCanceledRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workerBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondActivityTaskCanceled(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture - respondActivityTaskCompletedByID( - RespondActivityTaskCompletedByIDRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workerFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .respondActivityTaskCompletedByID(request)); - } + @Override + public RespondActivityTaskCanceledByIDResponse respondActivityTaskCanceledByID( + RespondActivityTaskCanceledByIDRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workerBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondActivityTaskCanceledByID(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture pollForDecisionTask( - PollForDecisionTaskRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workerFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .pollForDecisionTask(request)); - } + @Override + public RespondActivityTaskFailedResponse respondActivityTaskFailed( + RespondActivityTaskFailedRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workerBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondActivityTaskFailed(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } - @Override - public CompletableFuture respondDecisionTaskFailed( - RespondDecisionTaskFailedRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workerFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .respondDecisionTaskFailed(request)); + @Override + public RespondActivityTaskFailedByIDResponse respondActivityTaskFailedByID( + RespondActivityTaskFailedByIDRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workerBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondActivityTaskFailedByID(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } + + @Override + public RespondActivityTaskCompletedResponse respondActivityTaskCompleted( + RespondActivityTaskCompletedRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workerBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondActivityTaskCompleted(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } + + @Override + public RespondActivityTaskCompletedByIDResponse respondActivityTaskCompletedByID( + RespondActivityTaskCompletedByIDRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workerBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondActivityTaskCompletedByID(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } + + @Override + public PollForDecisionTaskResponse pollForDecisionTask( + PollForDecisionTaskRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workerBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .pollForDecisionTask(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } + + @Override + public RespondDecisionTaskFailedResponse respondDecisionTaskFailed( + RespondDecisionTaskFailedRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workerBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondDecisionTaskFailed(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } + + @Override + public RespondDecisionTaskCompletedResponse respondDecisionTaskCompleted( + RespondDecisionTaskCompletedRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workerBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondDecisionTaskCompleted(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } + + @Override + public RefreshWorkflowTasksResponse refreshWorkflowTasks(RefreshWorkflowTasksRequest request, @Nullable CallMetaData meta) { + try { + return grpcServiceStubs + .workflowBlockingStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .refreshWorkflowTasks(request); + } catch (Exception e) { + throw toServiceClientException(e); + } + } + }; } @Override - public CompletableFuture respondDecisionTaskCompleted( - RespondDecisionTaskCompletedRequest request, @Nullable CallMetaData meta) { - return toCompletableFuture( - grpcServiceStubs - .workerFutureStub() - .withInterceptors(new CallMetadataClientInterceptor(meta)) - .respondDecisionTaskCompleted(request)); + public Future futureStub() { + return new Future() { + @Override + public CompletableFuture startWorkflowExecution( + StartWorkflowExecutionRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workflowFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .startWorkflowExecution(request)); + } + + @Override + public CompletableFuture startWorkflowExecutionAsync( + StartWorkflowExecutionAsyncRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workflowFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .startWorkflowExecutionAsync(request)); + } + + @Override + public CompletableFuture signalWorkflowExecution( + SignalWorkflowExecutionRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workflowFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .signalWorkflowExecution(request)); + } + + @Override + public CompletableFuture + signalWithStartWorkflowExecution( + SignalWithStartWorkflowExecutionRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workflowFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .signalWithStartWorkflowExecution(request)); + } + + @Override + public CompletableFuture + signalWithStartWorkflowExecutionAsync( + SignalWithStartWorkflowExecutionAsyncRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workflowFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .signalWithStartWorkflowExecutionAsync(request)); + } + + @Override + public CompletableFuture getWorkflowExecutionHistory( + GetWorkflowExecutionHistoryRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workflowFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .getWorkflowExecutionHistory(request)); + } + + @Override + public CompletableFuture queryWorkflow( + QueryWorkflowRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workflowFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .queryWorkflow(request)); + } + + @Override + public CompletableFuture + requestCancelWorkflowExecution( + RequestCancelWorkflowExecutionRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workflowFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .requestCancelWorkflowExecution(request)); + } + + @Override + public CompletableFuture terminateWorkflowExecution( + TerminateWorkflowExecutionRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workflowFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .terminateWorkflowExecution(request)); + } + + @Override + public CompletableFuture restartWorkflowExecution( + RestartWorkflowExecutionRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workflowFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .restartWorkflowExecution(request)); + } + + @Override + public CompletableFuture listWorkflowExecutions( + ListWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .visibilityFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .listWorkflowExecutions(request)); + } + + @Override + public CompletableFuture scanWorkflowExecutions( + ScanWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .visibilityFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .scanWorkflowExecutions(request)); + } + + @Override + public CompletableFuture listOpenWorkflowExecutions( + ListOpenWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .visibilityFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .listOpenWorkflowExecutions(request)); + } + + @Override + public CompletableFuture listClosedWorkflowExecutions( + ListClosedWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .visibilityFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .listClosedWorkflowExecutions(request)); + } + + @Override + public CompletableFuture listArchivedWorkflowExecutions( + ListArchivedWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .visibilityFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .listArchivedWorkflowExecutions(request)); + } + + @Override + public CompletableFuture countWorkflowExecutions( + CountWorkflowExecutionsRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .visibilityFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .countWorkflowExecutions(request)); + } + + @Override + public CompletableFuture pollForActivityTask( + PollForActivityTaskRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workerFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .pollForActivityTask(request)); + } + + @Override + public CompletableFuture recordActivityTaskHeartbeat( + RecordActivityTaskHeartbeatRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workerFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .recordActivityTaskHeartbeat(request)); + } + + @Override + public CompletableFuture respondActivityTaskCanceled( + RespondActivityTaskCanceledRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workerFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondActivityTaskCanceled(request)); + } + + @Override + public CompletableFuture respondActivityTaskCanceledByID( + RespondActivityTaskCanceledByIDRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workerFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondActivityTaskCanceledByID(request)); + } + + @Override + public CompletableFuture respondActivityTaskFailed( + RespondActivityTaskFailedRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workerFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondActivityTaskFailed(request)); + } + + @Override + public CompletableFuture respondActivityTaskFailedByID( + RespondActivityTaskFailedByIDRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workerFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondActivityTaskFailedByID(request)); + } + + @Override + public CompletableFuture respondActivityTaskCompleted( + RespondActivityTaskCompletedRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workerFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondActivityTaskCompleted(request)); + } + + @Override + public CompletableFuture respondActivityTaskCompletedByID( + RespondActivityTaskCompletedByIDRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workerFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondActivityTaskCompletedByID(request)); + } + + @Override + public CompletableFuture pollForDecisionTask( + PollForDecisionTaskRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workerFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .pollForDecisionTask(request)); + } + + @Override + public CompletableFuture respondDecisionTaskFailed( + RespondDecisionTaskFailedRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workerFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondDecisionTaskFailed(request)); + } + + @Override + public CompletableFuture respondDecisionTaskCompleted( + RespondDecisionTaskCompletedRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workerFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .respondDecisionTaskCompleted(request)); + } + + @Override + public CompletableFuture refreshWorkflowTasks( + RefreshWorkflowTasksRequest request, @Nullable CallMetaData meta) { + return toCompletableFuture( + grpcServiceStubs + .workflowFutureStub() + .withInterceptors(new CallMetadataClientInterceptor(meta)) + .refreshWorkflowTasks(request)); + } + }; } private static class CallMetadataClientInterceptor implements ClientInterceptor { @@ -330,10 +744,20 @@ public void onSuccess(T t) { @Override public void onFailure(Throwable throwable) { - completableFuture.completeExceptionally(throwable); + completableFuture.completeExceptionally(toServiceClientException(throwable)); } }, MoreExecutors.directExecutor()); return completableFuture; } + + private ServiceClientException toServiceClientException(Throwable t) { + if (t instanceof ServiceClientException) { + return (ServiceClientException) t; + } else if (t instanceof StatusRuntimeException) { + return ErrorMapper.Error((StatusRuntimeException) t); + } else { + return new ServiceClientException(t); + } + } }