I have a question about the sample of how stubs are created. I think the stub should be a shared resource like the client and should be a singleton that is shared by all workflow executions in the worker process. But in all samples I have seen, definition creates the stub inside it which means the stub is frequently created.
Could you help shed more light on the workflow/activity stub?
stub should be a shared resource like the client and should be a singleton that is shared by all workflow executions in the worker process
For activities you register a single activity implementation class with the worker which is shared for all activity type invocations that this worker is processing, so your activity impl should be thread safe.
For activity stubs, activity invocations are remote procedure calls and your worker has to send command service to request invocation of this activity that includes the activity type, task queue, and activity options. The configs (such as timeouts, inputs, etc) can differ between workflow executions so you have to define them there.
For workflow stubs you create in your client code, you create a workflow stub per execution (cannot use same workflow stub for multiple executions you want to start).