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,

Hi, Itsnuyen.

I also figured out this solution, but it brings in a new issue. If there are multiple jobs are waiting to be either completed or failed, how could I complete one of them and put back all the others?

For example, my BPMN workflow uses a Parallel gateway to start 3 jobs at waiting stage. The zeebe.newActivateJobsCommand().jobType(jobType).maxJobsToActivate(count) activates those three jobs.

I just want to complete one job by its element id, and put the other two jobs back to the engine. I do not want to complete or fail the other two jobs.

Then I will assert the process instance.

Then I want to run the zeebe.newActivateJobsCommand().jobType(jobType).maxJobsToActivate(count) again to active the remaining two jobs. I will complete one of the two jobs, and put the last one back to engine.

But there is no command to put back an active job. As a result, the second execution of zeebe.newActivateJobsCommand().jobType(jobType) will active zero jobs.

Hi @Kent_Huang
Simply you can do nothing so job becomes available for activation once it times out or you can update timeout based on your needs

zeebeClient
  .newUpdateTimeoutCommand(jobKey)
  .timeout(100)
  .send();

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.