Async workflow invocation and long polling for result

If you want to wait for workflow to complete don’t use start, but run the workflow method synchronously. Behind the scenes, it is going to perform long polls waiting for its completion. So it is pretty efficient.

For long poll GetWorkflowExecutionHistory is used. When GetWorkflowExecutionHistoryRequest.WaitNewEvent property is set to true the request becomes a long poll request not returning for a minute if there a no new events since the last page. This is used by UI and CLI to stream history as it happens.

When GetWorkflowExecutionHistoryRequest.HistoryEventFilterType is set to CLOSE_EVENT then only the workflow completion event is returned from the API.

So setting both WaitNewEvent to true and HistoryEventFilterType to CLOSE_EVENT makes GetWorkflowExecutionHistory a long poll request that can be used to wait for workflow completion.

Here is the actual implementation of that logic in JavaSDK.

Another way to wait for any workflow completion giving its ID is using untyped workflow stub:

WorkflowStub workflowStub = workflowClient.newUntypedWorkflowStub(execution, Optional.empty());
<ResultType> result = workflowStub.getResult(<ResultType>.class);
1 Like