- can a single class implement multiple workflow types ? ( multiple @WorkflowInterface )
- What will happen if there are multiple classes implementing the same @WorkflowInterface
- Will the implementing class get instantiated for each new workflow ? ( or it Temporal going to call the @WorkflowMethod on the same instance whenever a new workflow is created )
- Are there any requirements on the constructor of the implementing class ? ( Like there should be a default constructor )
- is it OK for the implementing class to have static block with code inside it ? ( java )
- is it OK for the implementing class to have instance initializer block ? ( java )
- Technically yes, but I don’t think this is very useful as you have to pick a single interface when creating a WorkflowStub. Workflow interface inheritance is probably more useful: Workflows in Java | Temporal documentation
- Not sure what you mean with what will happen? You cannot register the same workflow type more than once with the same worker. You could register however each of the implementations of the same workflow interface with different workers listening to different task queues.
- Temporal provides isolation of workflow executions, so each execution has its own workflow thread.
- Yes, public no-arg constructor is needed. You should not really define a constructor on your implementations anyways. If you need to pass data to a workflow on invocation, you pass it to its @WorkflowMethod.
- You can have non-static fields without worrying about isolation issues. For static fields you should use WorkflowLocal or WorkflowThreadLocal depending on your use case. Workflow code has to be deterministic and you can read about workflow implementation constraints here.
- You can but the workflow stub is created for the workflow interface and not the implementation, so no need to use them.
Thank you Tihomir.
-
I was asking this question because I thought worker.registerWorkflowImplementationTypes(MyWfImplClass.class ) will throw an exception if MyWfImpl.class implements multiple WorkflowInterface interfaces
-
Does Temporal allow Java ThreadLocal objects to be attached inside the workflow ? Will it be recreated if the Worker crashes and is restarted ?
- It should not throw an error. You can test it yourself as well. You can have for example:
class MyWorkflowImpl implements WorkflowOneInterface, WorkflowTwoInterface { ...}
and then later:
WorkflowOne workflow1 = client.newWorkflowStub(WorkflowOneInterface.class, parentWorkflowOptions);
WorkflowTwo workflow2 = client.newWorkflowStub(WorkflowTwoInterface.class, parentWorkflowOptions);
- If you use the WorkflowLocal and WorkflowThreadLocal provided by the SDK your workflows will be deterministic and replayable