Exception handling with Async.procedure calls

Hi team,

I have two activities being executed through async calls like this:

private CompletablePromise<Void> uploaded = Workflow.newPromise();            
uploaded.completeFrom(Async.procedure(activities::uploadSignature, id, signature));
uploaded.completeFrom(Async.procedure(activities::submitSignature, id));

// Activities
public String uploadSignature(String id, String signature) { 
...
}

public String submitSignature(String id) {
...
}

This code works fine for the current scenario. My problem is that I cannot have uploadSignature and submitSignature throw Exceptions because Async.procedure call gives compile errors. But then I have no way to allow the two activities to automatically retry execution in case there are Exceptions or unsuccessful calls.

I wanted to check if there is a way I can call them asynchronously like I do right now while being able to let the activities retry themselves if they don’t succeed right away. Any kind of help is much appreciated, thanks in advance!

You can throw exceptions from activities. You cannot specify checked exceptions in the activity signatures. So your options are to throw unchecked exceptions or wrap checked exceptions using Activity .wrap.

try {
  ...
} catch (IOException e) {
  throw Activity.wrap(e);
}

Also, you cannot call completeFrom multiple times on the same Promise. Use Promise.allOf or Promise.anyOf to listen on multiple Promises.

1 Like

Thanks @maxim for the Activity.wrap, and pointing out the completeFrom error. Now I’ve changed my code to look like this

List<Promise<Void>> acts = new ArrayList<>();
acts.add(Async.procedure(activities::uploadSignature, id, signature));
acts.add(Async.procedure(activities::submitSignature, id));

private CompletablePromise<Void> uploaded = Workflow.newPromise();
uploaded.completeFrom(Promise.allOf(acts));

I hope this looks better, and just wanted to confirm, would this ensure that uploadSignature is always called before submitSignature, because the workflow would fail if it tries to submitSignature before it is uploaded. If not, is there any way for me to ensure the order?