WorkflowServiceStubs thread Safety and other confusions

  1. Why is it called WorkflowServiceStubs ( plural )
    ( is this like a Factory which can be used to create the actual Stubs ? ( like WorkflowServiceBlocking
  2. Does WorkflowServiceStubs hold the actual http/TCP connection object ?
  3. is WorkflowServiceStubs thread safe ? ( Can multiple threads use WorkflowServiceStubs or objects created from it - like WorkflowServiceBlockingStub )
  4. Are we supposed to call shutdown() after using it ? ( Noticed it is not Java Closeable )
    ( If we do not call shutdown(0, will it leak the underlying HTTP connection ? )
  5. is there a way to cache the underlying http/TCP network connection, so that we will not need to connect/disconnect against each request ? ( Maybe using some kind of Object pooling )

–sony

  1. Why is it called WorkflowServiceStub s ( plural )
    ( is this like a Factory which can be used to create the actual Stubs ? ( like WorkflowServiceBlocking

Yes, it can be used to get access to both blocking and future gRPC stubs.

  1. Does WorkflowServiceStubs hold the actual http/TCP connection object ?

Yes, indirectly through gRPC library.

  1. is WorkflowServiceStubs thread safe ? ( Can multiple threads use WorkflowServiceStubs or objects created from it - like WorkflowServiceBlockingStub )

Yes, it is thread safe.

  1. Are we supposed to call shutdown() after using it ? ( Noticed it is not Java Closeable )
    ( If we do not call shutdown(0, will it leak the underlying HTTP connection ? )

You are supposed to create it once per process lifetime. Calling shutdown helps to cleanly close the TCP connections gRPC library keeps.

  1. is there a way to cache the underlying http/TCP network connection, so that we will not need to connect/disconnect against each request ? ( Maybe using some kind of Object pooling )

It is already done assuming that you use a single instance of WorkflowServiceStubs for all your communication to Temporal service.

( Thank You Maxim - You are always very helpful )

So the following class is correct and thread safe ? ( Is it Ok for runQuery() method to be called from multiple threads simultaneously? )

public class MySingletonQueryClass {
    static WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
    static WorkflowClient client = WorkflowClient.newInstance(service);

    String runQuery( String runId, String query ) {
        WorkflowExecution workflowExecution =  WorkflowExecution.newBuilder().setWorkflowId("MyFixedWorkflowID").setRunId(runId).build();
        WorkflowStub workflow = client.newUntypedWorkflowStub(workflowExecution, Optional.empty());
        return workflow.query(query,String.class ) ;
    }
}

Yes, this code looks fine to me.