Repeat an activity with time limit

We are using temporal java-sdk 1.0.9 version.
We have a usecase wherein we have to repeat an activity (which polls an external system through API) until we get success or we reach the time limit (say 8 s) from the start of the workflow.
Please suggest how we can achieve this.

Here are some best practices for polling activities impl.
What is the poll frequency you are trying to achieve?

1 Like

Thanks @tihomir, this helps. Our polling frequency is 1 sec, but limited to 8s from start of the workflow.

One more query, how do we get Workflow start time within an activity.

Inside workflow code you can get:

  1. Workflow.getInfo().getRunStartedTimestampMillis() which is the time when workflow run was started (by your client code). Note that this might not be the exact time when your worker starts processing your WorkflowMethod in your workflow code.
  2. Workflow.currentTimeMillis() this is the current workflow time. If you call it as first thing inside your WorkflowMethod it would give you the time when workflow execution actually started.

Don’t think it’s possible to get that inside activity code itself via Activity.getExecutionContext().getInfo() so if you need it inside your activity, get it inside workflow code and can pass it as input to your activity invocation.

1 Like

Just to add, if your maximum time to poll is really short (8 seconds as you mentioned,) using local activity would be another reasonable solution for this (as with regular activity you might experience some time delays to start of execution in case of network latencies for example).

In that case you should bump the default task timeout (via WorkflowOptions->setWorkflowTaskTimeout, default value 10s, max value you can set 60s) to 15 seconds to avoid workflow task heartbeats.

In case the max polling time is larger, regular activity would be recommended solution.

1 Like

thank you :slight_smile: