# WorkflowServiceStubs thread Safety and other confusions

**URL:** <https://community.temporal.io/t/workflowservicestubs-thread-safety-and-other-confusions/2869>\
**Category:** Community Support\
**Tags:** workflow-options\
**Created:** [September 6, 2021, 12:55am UTC](https://community.temporal.io/t/workflowservicestubs-thread-safety-and-other-confusions/2869 "2021-09-06T00:55:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sonyantony](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/sonyantony/32/1209_2.png) [@sonyantony](https://community.temporal.io/u/sonyantony)\
**Post date:** [September 6, 2021, 12:55am UTC](https://community.temporal.io/t/workflowservicestubs-thread-safety-and-other-confusions/2869/1 "2021-09-06T00:55:13Z")

</div>

1. Why is it called WorkflowServiceStub **s** ( 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

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [September 6, 2021, 2:20am UTC](https://community.temporal.io/t/workflowservicestubs-thread-safety-and-other-confusions/2869/2 "2021-09-06T02:20:42Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![sonyantony](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/sonyantony/32/1209_2.png) [@sonyantony](https://community.temporal.io/u/sonyantony)\
**Post date:** [September 6, 2021, 3:16am UTC](https://community.temporal.io/t/workflowservicestubs-thread-safety-and-other-confusions/2869/3 "2021-09-06T03:16:11Z")

</div>

( 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? )

```auto
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 ) ;
    }
}

```

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [September 6, 2021, 3:43am UTC](https://community.temporal.io/t/workflowservicestubs-thread-safety-and-other-confusions/2869/4 "2021-09-06T03:43:24Z")

</div>

Yes, this code looks fine to me.
