How to increase the workers registered to workflow implementation type

I am using the java sdk of temporal with below maven dependency

    <dependency>
        <groupId>io.temporal</groupId>
        <artifactId>temporal-sdk</artifactId>
        <version>1.8.1</version>
    </dependency>

I noticed that one worker only assigned to the task queue of workflow implementation type, and so I tried to increase the workers registered to workflow implementation type registered to the task queue, but I failed to do that and I got this exception below

example code is :
Worker worker = workerFactory.newWorker(“example_task_queue”);
worker.registerWorkflowImplementationTypes(ExampleWorkflowImpl.class);

exception got :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘ExampleWorkflowImpl’ defined in URL [jar:file:/app.jar!/BOOT-INF/classes!/com/example/workflow/impl/ExampleWorkflowImpl.class]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: ExampleWorkflowImpl workflow type is already registered with the worker
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:610)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:917)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:582)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300)
at com.example.ExampleWorkflowImpl.main(ExampleWorkflowImpl.java:26)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:107)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.lang.IllegalStateException: ExampleWorkflowImpl workflow type is already registered with the worker
at io.temporal.internal.sync.POJOWorkflowImplementationFactory.registerWorkflowImplementationType(POJOWorkflowImplementationFactory.java:196)
at io.temporal.internal.sync.POJOWorkflowImplementationFactory.registerWorkflowImplementationTypes(POJOWorkflowImplementationFactory.java:104)
at io.temporal.internal.sync.SyncWorkflowWorker.registerWorkflowImplementationTypes(SyncWorkflowWorker.java:138)
at io.temporal.worker.Worker.registerWorkflowImplementationTypes(Worker.java:223)
at ai.applica.spring.boot.starter.temporal.processors.WorkflowAnnotationBeanPostProcessor.postProcessAfterInitialization(WorkflowAnnotationBeanPostProcessor.java:128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:437)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)

I think this is expected. If you try to create a second worker within the same process (deployment) from same WorkerFactory that listens on the same task queue, SDK is going to return the existing one and not create second, see here. In your case I believe an existing worker is returned which already had the workflow impl registered, and the exception imo makes sense.

With Temporal to achieve high availability /scaling on your app side you should deploy multiple worker processes so in case one goes down your workflow executions can continue on another worker in different process (so you can deploy your app that contains workers on multiple pods/containers).

1 Like

A follow up on this question.

I noticed the workerFactory.newWorker method may take a 2nd param for options.

I wonder what if the 1st param queue name is same, but 2nd param options is different. Will it still create the same worker?

Hi @RJ88

this How to increase the workers registered to workflow implementation type - #2 by tihomir answer is also valid for your question. It will return the same worker.