How to use @ZeebeWorker outside of @SpringApplication class?

Hello, this might be a silly question but I haven’t been able to find an answer to it anywhere.
I am using Camunda 8 Cloud with my Spring Boot Application and need to write several @ZeebeWorker functions for the service tasks in these diagrams. All the examples I have seen whether it be on YouTube or GitHub show the ZeebeWorker functions being written within the main application class (the one tagged with @SpringApplication) like so:

package com.mycompany.camunda;

// dependencies

@SpringBootApplication
@EnableZeebeClient
@Slf4j
public class CamundaApplication {

    public static void main(String[] args) {
        SpringApplication.run(CamundaApplication.class, args);
    }

    @ZeebeWorker(type="job-a")
    public void handleJobA(final JobClient client, final ActivatedJob job) {
        log.info("Job A is executing");

        client.newCompleteCommand(job.getKey())
                .variables("{\"JSONKey\": \"JSONValue\"}")
                .send()
                .exceptionally(throwable -> { throw new RuntimeException("Could not complete job " + job, throwable); });
    }
}

However, I do not want to write all of my all of my ZeebeWorker in this class. I want to be able to group related Worker functions classes in varying modules like this:

package com.mycompany.camunda.worker;

// Dependencies

@Slf4j
@EnableZeebeClient
public class CallActivity {

    @ZeebeWorker(type="job-b")
    public void handleJobB(final JobClient client, final ActivatedJob job) {
        log.info("Job A is executing");

        client.newCompleteCommand(job.getKey())
                .variables("{\"JSONKey\": \"JSONValue\"}")
                .send()
                .exceptionally(throwable -> { throw new RuntimeException("Could not complete job " + job, throwable); });
    }
}

When I do this, they don’t appear to be found by the ZeebeWorkerAnnotationProcessor.
So, how do I write ZeebeWorker functions outside of the @SpringApplication class?

Hello @thomasjazz ,

the @EnableZeebeClient annotation may still be present on the main class of your project. However, every class containing @ZeebeWorker methods needs to be registered to the bean context, using @Component on the class for example.

I hope this helps

Jonathan

2 Likes

Thank you, this solved the problem!

1 Like

A note that the most recent version of the Spring client deprecates @ZeebeWorker in favor of @JobWorker. See here: Spring Zeebe 8.1 is out - and we renamed some annotations

Josh

1 Like