Resume / retry activity upon receiving signal in a workflow


For my use case, I need to have both status polling and callbacks. But when the call back is received irrespective of waiting for next activity time, activity should be retried. Is it possible ??

workflow A            Internal Service            Third Party service
      |                          |                                    |
      |    poll for status       |                                    |
      | ---------- t = 0 ----->  |                                    |
      | ---------- t = 10 ---->  |                                    |
      | ---------- t = 20 ---->  |                                    |
      | <--------------------------- t = 25 ------------------------  | (This call back is not guaranteed. it might come
      |                          |                                    | sometimes and can be missed so polling is mandatory).
      |                          |                                    |

draft code of current implementation

public void startWorkflow() {

     workflowStatus = getFinalActivityStatus();

     // signal workflow status to parent workflow

public WorkflowStatus getFinalActivityStatus() {
         WorkflowStatus workflowStatus = statusPollActivity.getStatusFromInternalService();
        return workflowStatus;

currently my workflow depends only on activity to poll status timely for 10 mins. Because of this it is taking extra time to close. I want to have a callback handling such that, when the signal is received get the final status from internal service and close the workflow. How to achieve this.


I think you can start your activity async and wait until either the activity completes or you get a callback,

  Promise<String> promiseActivity = Async.function(activities::polling, "Hello");
  Workflow.await(()-> signaled || promiseActivity.isCompleted());

From doc isCompleted

Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true.

So maybe you need a different expression here to evaluate activity completions.

Let me know if it helps,

sure, thanks. Will try this and let you know if i face any issues

Is polling done via activity retries?
You could just cancel activity when your callback signal comes in and return the result of the last call.

yes @tihomir currently polling is by throwing retryable error and activity retries.

In call back we just get a signal that the process finished, whatever is present the activity requires to be done and then proceed further. Please let me know how to do this activity at that instant and proceed and cancel the activity.

If I understand the question correctly, when signal comes in you want to allow the last poll to finish and then return result from activity. If so look at samples-java/core/src/main/java/io/temporal/samples/hello/ at main · temporalio/samples-java · GitHub
Your activity should heartbeat to receive cancelation request.
Your signal handler can cancel its scope - samples-java/core/src/main/java/io/temporal/samples/hello/ at main · temporalio/samples-java · GitHub

and then in activity when you get ActivityCompletionException maybe do last poll and then return result instead of rethrowing the exception. also set samples-java/core/src/main/java/io/temporal/samples/hello/ at main · temporalio/samples-java · GitHub to wait for activity on canceling its scope

After 3 retries if i receive signal, i just want the activity to retry for 4th time and continue next activities to progress normally. Instead of cancelling the workflow.

In other terms, while status poll activity is retrying after particular time intervals, when the signal is received, instead of waiting for next time frame, retry at that instant itself

Ok, i think canceling activity and doing the last poll and returning result in ActivityCompletionException block in activity code could be the way to go