How to call @QueryMethod from another Workflow

I’m looking a way to call @QueryMethod of one workflow from another. I’ve note from here Cannot call @QueryMethod from child workflow that I need to querying from Activity and use WorkflowClient to get workflowStub.

  public String someActivityMethod(String workflowId, String queryType) {
    WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
    WorkflowClient client = WorkflowClient.newInstance(service);
    WorkflowStub workflow = client.newUntypedWorkflowStub(workflowId);
    return workflow.query(queryType, String.class);

But in this case I’m getting error

io.grpc.StatusRuntimeException: UNAVAILABLE: io exception 

Help me please, how can i get workflowStub in activity or sugest another way to call @QueryMethod from workflow.

Hi @korad

Regarding the “Unavailable” error message, are you running Temporal server locally? Can you show how you create WorkflowServiceStubs and WorkflowClient in your client code that starts workflow exec?

You don’t have to create client in your activity methods (those are typically heavy-weight operations), you can pass it to your activity instance when you register it with the worker, for example:

worker.registerActivitiesImplementations(new MyActivitiesImpl(client));

(add a constructor to your activities impl that takes in WorkflowClient as input)

Yes, you cannot query using ExternalWorkflowStub (just signal) so you would need to use activity for it.

In your activity code I think you should also catch WorkflowException and then check its type: WorkflowNotFoundException in case workflowId is invalid, and WorkflowQueryException in case the queryType passed in is incorrect, if you need to handle these cases gracefully.

Thanks @tihomir, your answer was helpful!
I use spring to manage Activities and WorkflowClient beans, so it was enough for me to autowire WorkflowClient to Activity and use it there.