Activities Retries notified Workflow?

I have my workflow something like below.

boolean isWorkflowCompleted = false;
        try {
                 activity1.doSomthing();
                 activity2.doSomthing();
                 activity3.doSomthing();
            isWorkflowCompleted = true;
        } finally {
            if (!isWorkflowCompleted) {
                finalActivity.doSomething();
            }
        }

What I wanna do is if Workflow gets completed due to any activities retries then I wanna perform one more activity in my finally block, If workflow gets completed normally (without any activity failure in just first attempt) I don’t want to do anything.

How can I achieve that ?? Do Workflows gets notified by temporal of their activities being retried?

Hi @Raj1206

Do Workflows gets notified by temporal of their activities being retried?

For your workflow, an activity invocation is like remote procedure call. If your activity fails and exhausts all its retries, the failure is delivered to your workflow code via ActivityFailure which has ApplicationFailure as it’s cause. You can get the actual exception thrown in your activity code from AppliationFailure, for example let’s say your activity throws a NullPointerException with message “simulated error”, in your workflow code you can do:

try {
    activity.exec();
} catch (ActivityFailure e) {
    ApplicationFailure af = (ApplicationFailure) e.getCause();
    String originalFailureMessage = af.getOriginalMessage();
    String originalFailureType = af.getType();
}

originalFailureMessage should be “simulated error” and originalFailureType should be java.lang.NullPointerException in this case.

Note that in case of activity timeout, you would still catch ActivityFailure but its cause is going to be io.temporal.failure.TimeoutFailure so in the previous sample code you can check if e.getCause() is instance of ApplicationFailure or TimeoutFailure.

Since you are executing your activities sync, you can use the shown error handling to catch the failure and then run your final activity, or you can do compensation as well, see sample here.

Also, just to add, child workflow invocations always throw ChildWorkflowFailure so you can use the same type of approach for child workflows as well.

Thanks for a quick reply.

My main question was around the success scenario, Let me be more clear here.

In the above example all activities are retry-able up to 3 times.

Scenario 1: my workflow gets completed with out any activity failure, then I don’t want anything after it gets completed.

Scenario 2: let say activity 2 failed on first attempt and then Is passes on second attempt and the third activity passes as well and eventually my workflow is completed, so we can say my workflow is completed because of retry, in this case I wanna notify user.

Is there any solution for this scenario ?

The value of activity attempt is accessible from the activity code:

int attempt = Activity.getExecutionContext().getInfo().getAttempt();

then you can return the attempt as part of the activity result.
So your code could look like:

       int attemptCount = 0;
        try {
            attemptCount += activity1.doSomthing();
            attemptCount += activity2.doSomthing();
            attemptCount += activity3.doSomthing();
            isWorkflowCompleted = true;
        } finally {
            if (!isWorkflowCompleted || attemptCount > 3) {
                finalActivity.doSomething();
            }
        }