Implementing @WorkflowInterface

  1. can a single class implement multiple workflow types ? ( multiple @WorkflowInterface )
  2. What will happen if there are multiple classes implementing the same @WorkflowInterface
  3. 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 )
  4. Are there any requirements on the constructor of the implementing class ? ( Like there should be a default constructor )
  5. is it OK for the implementing class to have static block with code inside it ? ( java )
  6. is it OK for the implementing class to have instance initializer block ? ( java )
  1. 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
  2. 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.
  3. Temporal provides isolation of workflow executions, so each execution has its own workflow thread.
  4. 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.
  5. 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.
  6. 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.

  1. I was asking this question because I thought worker.registerWorkflowImplementationTypes(MyWfImplClass.class ) will throw an exception if MyWfImpl.class implements multiple WorkflowInterface interfaces

  2. Does Temporal allow Java ThreadLocal objects to be attached inside the workflow ? Will it be recreated if the Worker crashes and is restarted ?

  1. 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);
  1. If you use the WorkflowLocal and WorkflowThreadLocal provided by the SDK your workflows will be deterministic and replayable