How to call @QueryMethod from another Workflow

Hi
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 
io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:262)
io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:243)
io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:156)
io.grpc.health.v1.HealthGrpc$HealthBlockingStub.check(HealthGrpc.java:252)
io.temporal.serviceclient.WorkflowServiceStubsImpl.lambda$checkHealth$2(WorkflowServiceStubsImpl.java:273)
io.temporal.internal.retryer.GrpcSyncRetryer.retry(GrpcSyncRetryer.java:61)
io.temporal.internal.retryer.GrpcRetryer.retryWithResult(GrpcRetryer.java:51)
io.temporal.serviceclient.WorkflowServiceStubsImpl.checkHealth(WorkflowServiceStubsImpl.java:266)
io.temporal.serviceclient.WorkflowServiceStubsImpl.<init>(WorkflowServiceStubsImpl.java:173)
io.temporal.serviceclient.WorkflowServiceStubs.newInstance(WorkflowServiceStubs.java:51)
io.temporal.serviceclient.WorkflowServiceStubs.newInstance(WorkflowServiceStubs.java:41)
io.temporal.serviceclient.WorkflowServiceStubs.newInstance(WorkflowServiceStubs.java:36)

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.