How to call a Child Workflow from a separate microservice & namespace?

Hello, Is there a way to call a separate workflow from another microservice & namespace as a child workflow? can you provide samples for java?

eg:

ChildWorkflowOptions childWorkflowOptions =  ChildWorkflowOptions.newBuilder().setNamespace("separate-namespace").setTaskQueue("sample-task-queue").setWorkflowId(UUID.randomUUID().toString()).build();

SampleWorkflow sampleWorkflow = Workflow.newChildWorkflowStub(SampleWorkflow .class, childWorkflowOptions);

sampleWorkflow .executeWorkflow();

in the example given above you can see we passed in SampleWorkflow.class but this class doesn’t reside on the same microservice, how we can then call a workflow from another microservice?

You can invoke a child workflow (same with activity) typed or untyped way.

In your case you are using typed by using SampleWorkflow interface.
If your service A runs the parent workflow and wants to invoke child of service B,
A only needs to have the interface, not the impl, so if you can share the interfaces between services this would be ok.
If you cannot share the interfaces then can use untyped approach where you need to know the workflow type of the child workflow you are trying to invoke, something like:

Workflow.newUntypedChildWorkflowStub("SampleWorkflow", childWorkflowOptions);

Note that we don’t encourage cross-namespace child workflow invocations however. This is also not allowed on Temporal Cloud. If you need to do this would be better to start it as normal workflow from an activity using client apis.

Also in your child workflow options:

setWorkflowId(UUID.randomUUID().toString)

don’t think you need this, if you don’t define a workflow id service would assign it a unique one.

Thanks @tihomir , “Note that we don’t encourage cross-namespace child workflow invocations however. This is also not allowed on Temporal Cloud. If you need to do this would be better to start it as normal workflow from an activity using client apis.” - we structured our namespace per microservice, if cross namespace is not a good practice, what would you suggest on the usage of namespace since namespace are unit of isolation ?

There is a project in the works that will make x-namespace communications much easier in the future, no ETA at this time that can share.

Until then I think either use activities to invoke workflows on different namespaces, or consider having a task-queue-per-service approach on single namespace.