How to find a job by its elementId

Hi, all.

I am building java unit tests for my workflow. I try to complete a job by

JobClient.newCompleteCommand(ActivatedJob job)

How could I find the target job by its element ID?

My workflow is designed within Camunda 8. The JobClient used by my unit test is from

        <dependency>
            <groupId>io.camunda</groupId>
            <artifactId>zeebe-client-java</artifactId>
            <version>8.5.6</version>
        </dependency>

Hi Kent,

how did you create your tests? Do you start a process instance by yourself or do you want to start a process instance through a abstraction layer?

in general for your tests you can use the zeebe-process-test-extension package

<dependency>
  <groupId>io.camunda</groupId>
  <artifactId>zeebe-process-test-extension</artifactId>
  <version>8.5.7</version>
  <scope>test</scope>
</dependency>

You can inject the ZeebeClient and ZeebeTestEngine into your tests and listen to them.

With the ZeebeClient you can search for the current running job by the jobType you defined. The jobType should be the ServiceTask ID you defined in your BPMN
Example: completeServiceTasks Example

Source: Zeebe Process Test

Thanks, @itsnuyen

Our current solution match your suggestion and the completeServiceTasks Example.

The problem is multiple jobs are the same type. For example, I have two service task:

type = "io.camunda:http-json:1"
element id = "ServiceTask_CreateBrazeProfile"
element instance key = 2251799813685258

and

type = "io.camunda:http-json:1"
element id = "ServiceTask_CreateSalesforceProfile"
element instance key = 2251799813685249

We feel these two service tasks are the same type: sending a REST request to an external API endpoint. The only differences are “REST API endpoint” and request body. Therefore, they share the same type.

But we give them different element ID.

As a result, the completeServiceTasks Example does not help. It cannot active a job by its element id.

Hi @Kent_Huang,

would something like this be feasible for you?

final var activateJobsResponse =
                zeebe.newActivateJobsCommand()
                        .jobType(jobType)
                        .maxJobsToActivate(count)
                        .send()
                        .join();
for (int i = 0; i < count; i++) {
            final var job = activateJobsResponse.getJobs().get(i);
            if (job.getElementId().equals("ServiceTask_CreateBrazeProfile")) {
                zeebe.newCompleteCommand(job.getKey())
                        .variables(Collections.singletonMap("foo", "bar"))
                        .send()
                        .join();
            } else {
                zeebe.newCompleteCommand(job.getKey()).send().join();
            }
        }

This is still the completeServiceTasks Example but I adapt a little bit of the code.
It is going to loop over the current jobs and based on the elementId it would execute different complete commands.

Regards,