Java wait for signal and retry the activity if not received within some seconds

Hi Temporal,

We had such requirement:
Context: we have an activity A send a request to external service.
And this external service will return the response, and a signal will be sent back to the workflow.

If signal not received with in 5 seconds, then that particular activity A should be retried, and maximum retry count should not go beyond 3.

So, for above retry requirement I have developed some code, it could achieve the goal but not sure if this “do while” loop, construction of mutableSideEffect and updateRequestId are the right approaches or any other corner cases missing. Please help to have a look! Thank you!

Pseudo code:

requestId has a field for attemptCount using Workflow.mutableSideEffect to construct.

activityA.initiateScan() → send a request to external system.
signalTimeout → Duration of 5 seconds;
checkReattemptCount → if requestId.getAttemptCount() < 3;
workflowWaitForResult → !Workflow.await(signalTimeout, this::isSignalResultReceived);
isSignalResultReceived → check if signal received
updateRequestId → requestId.setAttemptCount(++attemptCount);

do {
} while (workflowWaitForResult() && checkReattemptCount(requestId));

Best regards,

You could use Workflow.retry but note it’s not meant to be used for a large number of retries which you don’t seem to have, so something like

 try {
      () -> {
        Workflow.await(Duration.ofSeconds(5), () -> gotApproval);
        if (!gotApproval) {
          throw new IllegalArgumentException("did not get approval within 5s");
} catch (Exception e) {
 // did not get approval after all 3 reties