temporalio / sdk-java

Temporal Java SDK
https://temporal.io
Apache License 2.0
219 stars 146 forks source link

SpringBoot - add @Primary to temporalWorkflowClient WorkflowClient Bean #1811

Open tsurdilo opened 1 year ago

tsurdilo commented 1 year ago

Springboot autoconfig defines the primary WorkflowClient bean thats generated from config as:

  @Bean(name = "temporalWorkflowClient")
  public WorkflowClient client(ClientTemplate clientTemplate) {
    return clientTemplate.getWorkflowClient();
  }

For client app use case sometimes we need to create additional WorkflowClient beans (for example one for a different namespace thats part of the use case). Its not possible to do this via config so to do this currently once a custom WorkflowClient bean is created, for example:

   @Bean(name = "customWorkflowClient")
   public WorkflowClient secondClient() {
     WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs();
     return WorkflowClient.newInstance(
    service, WorkflowClientOptions.newBuilder().setNamespace("xyz").build());
  }

There are two issues with this:

  1. you cannot autowire the default WorkflowServiceStubs here because of cyclic dependency on the default template. user has to create a new WorkflowServiceStubs which seems unnecessary.
  2. Once you define this you no longer can resolve autowiring default WorkflowClient generated from config unless you use its name Qualifier, for example you have to do:

    @Autowired
    @Qualifier("temporalWorkflowClient") // gotta do this even if you don't autowire the custom one
    WorkflowClient client;
    
    @Autowired
    @Qualifier("customWorkflowClient")
    WorkflowClient customClient;

I think it would help if we annotate the default WorkflowClient bean with @Primary in order to allow autowiring the default one without having to use Qualifier directly everywhere in your code. Another idea could be to allow config of multiple clients from config directly, but not sure this is best approach maybe.

Wdyt?

cretz commented 1 year ago

For client app use case sometimes we need to create additional WorkflowClient beans

May need multiple clients for for worker apps too not just client apps.

This is similar to #1799 but more generic. There was internal discussion with myself and @Quinn-With-Two-Ns about how to possibly support multiple clients on multiple workers.

annotate the default WorkflowClient bean with @primary in order to allow autowiring the default one without having to use Qualifier directly everywhere in your code.

Concur on @Primary for the default, though we might need @Qualifier to support multiple clients on multiple workers, but maybe that's a separate issue.

robzienert commented 1 year ago

FWIW, this would work so @Qualifier is unnecessary; perhaps already known:

@Autowired
WorkflowClient temporalWorkflowClient; // property named after the bean name