Use activity result from async completion in workflow

Typically, Activity Function has a return value, which represents the result of the Activity Execution. We can then branch workflow execution based on this result, like:

SampleActivityInterface activityStub = Workflow.newActivityStub(...)
boolean result = activityStub.activityFunction(); 
if (result) {
  // do another activity
}

If the above activity uses Asynchronous Completion and activityFunction has a void return type, what should I do in the workflow to get the result boolean provided by an external service via Async Completion, so that I could branch workflow execution based on the result like above? I’m using Temporal Java SDK.

Thanks for your help.

If the above activity uses Asynchronous Completion and activityFunction has a void return type, what should I do in the workflow to get the result boolean provided by an external service via Async Completion

First of all, I think you’ll want your activityFunction to return boolean if the intention is to pass the boolean back from the external service.

It may look like this:

SampleActivityInterface activityStub = Workflow.newActivityStub(...)
Promise<Boolean> result = Async.function(activityStub::activityFunction);
if (result.get()) {
  // do another activity
}

You may also find these two examples helpful:

How an activity is invoked is entirely unrelated to how it is implemented. So you still can invoke an activity synchronously even if it is implemented using async activity completion.

In the same way, you can invoke an activity asynchronously even if its implementation is synchronous.

Thanks for the above replies.

So you still can invoke an activity synchronously even if it is implemented using async activity completion.

Indeed, I am invoking the activity synchronously with async activity completion. Let me provide a more detailed example to illustrate my question better:

Activity code

    public void myActivity() {
        ActivityExecutionContext context = Activity.getExecutionContext();
        ManualActivityCompletionClient manualActivityCompletionClient = context.useLocalManualCompletion();

        // sends a http request to an external service using non-blocking IO
        externalHttpReqNioUsingReactor()
            .doOnError(manualActivityCompletionClient::fail)
            // complete the activity when a response is received
            .doOnNext(response -> manualActivityCompletionClient.complete(response))
            .subscribe();
    }

Workflow code:

MyActivity externalActivity = Workflow.newActivityStub(MyActivity.class, activityOptions);
// invoke the activity synchronously with async activity completion
externalActivity.myActivity();
// Question: How should I access the activity result, that is the "response" value in myActivity(), here in the workflow?

I have this question because with async activity completion, the document says “the return value of Activity Function is ignored” (so I made the Activity Function return void).

The external service’s response is reported to the Temporal cluster as Activity Result, but I’m not sure how to use that in the workflow. Should I try to return a Promise<Boolean> (assuming the “response” is a boolean) from the Activity Function implemented this way? Should I try to grab that result from event history?

Thanks.

You have to change myActivity signature to return the response type that matches the response. Then the invocation is going to return that value.

Thanks so much for the clarification!