Difference between workflowClient.newWorkflowStub and Workflow.newExternalWorkflowStub

Hi Team,

I see there are lots of ways to create a client stub like using Workflow.newExternalWorkflowStub, workflowClient.newWorkflowStub etc.,

Can you please suggest when should we use what and if possible it would be great if you can help with any documentation around that topic.

1 Like

The general pattern is that methods that belong to Workflow class can be called only from workflow code. Methods that belong to WorkflowClient class can be called only by non workflow code.

new…WorkflowStub methods have two variants. The first one takes options and creates stub that can be used to start workflow execution. The second takes workflowId and optional runId and can be used to signal and query an existing workflow.

How can one find the exact runId to send a signal?

How can one find the exact runId to send a signal?

When a workflow is started a runId is returned to the caller.

    WorkflowExecution execution = WorkflowClient.start(transferWorkflow::transfer, from, to, reference, amountCents);
    System.out.println(execution.getRunId());

runId is optional when calling SignalWorkflowExecution. If you don’t specify it the client is going to signal whatever run is currently running with the given WorkflowId.

@maxim
I’m trying to send a signal to Go workflow from a java activity. But I get

“trace”: "java.lang.Error: Called from non workflow or workflow callback thread\n\tat io.temporal.internal.sync.DeterministicRunnerImpl.currentThreadInternal(DeterministicRunnerImpl.java...

I am able to send a signal from Go to Java, but I’m not able to do the reverse.

I tried this

ExternalWorkflowStub externalWorkflowStub = Workflow.newUntypedExternalWorkflowStub("simple_workflow-go");

I’m not sure this is correct, because I think I’d have to pass runId of the workflow in Go which is waiting for the signal

I’ve created a topic here:

Any API that belongs to the Workflow class must be called only from the workflow code. You are calling this API from an activity that is not allowed.

Why are you using an activity to call this API? If you really need to do it from activity use WorkflowClient APIs.

@maxim but a workflow client’s method needs the class name of the workflow to which it has to send a signal, right? The workflow I want to send a signal to is a Go workflow. How can I do this?

GreetingWorkflow workflow = client.newWorkflowStub(GreetingWorkflow.class, workflowOptions);//need class, suppose GreetingWorkFlow is in Go

@Abhilash_M you can use Workflow.newUntypedExternalWorkflowStub, see here:

Hope this helps!