How to get the target for new workflow client

To connect to an already running workflow in Java from a Workflow, I do

CreateDatasetWorkflow workflow = Workflow.newExternalWorkflowStub(
  CreateDatasetWorkflow.class, workflowIdToNotify);
workflow.pendingWorkflowFinished(workflowId); // signal call

If I don’t know if it’s running or not, I have to do a signalWithStart, which is only available in the WorkflowClient, which needs a WorkflowServiceStubs, which needs a target or will default to 127.0.0.1

WorkflowServiceStubsOptions workflowServiceStubsOptions = WorkflowServiceStubsOptions.newBuilder()
  .setTarget(temporalTarget) // *** THIS LINE
  .build();
WorkflowServiceStubs workflowServiceStubs = WorkflowServiceStubs.newInstance(workflowServiceStubsOptions);
WorkflowClient workflowClient = WorkflowClient.newInstance(workflowServiceStubs);

CreateFeatureWorkflow workflow = workflowClient.newWorkflowStub(CreateFeatureWorkflow.class, WorkflowOptions.newBuilder()
  .setTaskQueue(Constants.FEATURES_QUEUE_NAME)
  .setWorkflowId(featureGroupWorkflowId)
  .build());

BatchRequest batchRequest = workflowClient.newSignalWithStartRequest();
batchRequest.add(workflow::createFeatureGroup, featureGroup);
batchRequest.add(workflow::notifyWhenDone, Workflow.getInfo().getWorkflowId());

return workflowClient.signalWithStart(batchRequest);

My question is… How can we copy the behaviour of the External Workflow to not have to specify a target at all and still finding the correct one?

I am having to pass the temporal target for the workflowclient to build pointing to the correct temporal server.

void workflowMethod(String temporalTarget, Object realParamenter) {
  // store the temporal target for later use, would like to not have to pass it as a parameter
}

WorkflowClient internally makes gRPC calls to the Temporal service. So it cannot be used directly in the workflow code as such calls break determinism.

You have to move all the signalWithStart logic into an activity. Then inject the WorkflowClient into the activity when instantiating it before registering with a worker.

1 Like

OK, thanks for the quick response! I will test this out.