How to set run id manually instead of an auto generated one in Java

I prefer to set my own run id over the autogenerated. Is there a way to set the run id in Java ?

Hi @Vikas_NS, can you explain the use case for doing this?

Workflow id is a domain specific id and is assigned in your client code. By default there can be only one workflow run for a given id (per all workflow types per namespace). You can change the default id reuse policy via WorkflowOptions.setWorkflowIdReusePolicy.

Workflow run id is a serve-assigned UUID (cannot be set via client). A workflow execution can be uniquely identified via workflow id, run id, and namespace.

Thanks tihomir.

In this video (Temporal with Ryland (EP02) - Debugging and the Web UI - YouTube) Rayland says we can provide our own run id?

@ryland was incorrect in that particular case. RunId is not possible to provide externally as it is always generated by the service.

@Vikas_NS do you have any specific use case in mind?

Maxim,

In our usecase we reuse workflow ids for similar things.
So Run Id becomes important to distinguish two workflows with the same workflow id.

When endsystems invoke my application, I am supposed to create a worklfow and return the run Id. The endsystem will use the run Id to track the status.

My understanding is , I can get hold of run id only from within the workflow using Workflow.getInfo().getRunId();. Two ways to access run id from outside is

  1. expose query method and return run id from it.
  2. return the runId once the workflow method completes through java return.

First option doesn’t work because my usecase invovles reusing workflow ids. So even if I know the workflow id, there will be multiple workflows with the same id. So i won’t be able to find the right workflow to query.

Second option is not fesible as its going to take a long time for the workflow to complete and the endsystem can’t wait so long.

So I thought if I can set the run Id by myself, i can escape the work of fetching the run Id from Temporal.

Any suggestions?

Which SDK are you using? With Java SDK you can get the run id when you start the workflow, for example:

WorkflowExecution execution = WorkflowClient.start(workflow::start);
    String runId = execution.getRunId();

where “start” is the workflow method.

You can do similar with Go SDK:

we, err := c.ExecuteWorkflow(...)
...
runid := we.GetRunID()

Does that help?

In our usecase we reuse workflow ids for similar things.

I think this is the gist of the issue. Don’t reuse the same workflowID for similar things. Use a different one for different use cases. If you need to keep them separate use a prefix something like “UseCase1-[userId]” and “UseCase2-[userId]”.

I’m on Java SDK. Tried your solution and it works.
So .start method spins a new thread to trigger the workflow letting the current thread to continue?

And will the new thread be used only to trigger the workflow or will it also be used to run the workflow method code as well?

WorkflowClient.start starts your workflow async, meaning it does not wait for the workflow to complete (does not block your class thread that does the invocation until workflow completes).

will the new thread be used only to trigger the workflow or will it also be used to run the workflow method code as well

It should not, it sends a command to the server to schedule workflow execution.