Search in sources :

Example 1 with ActivatedJob

use of io.camunda.zeebe.client.api.response.ActivatedJob in project zeebe-test-container by camunda-community-hub.

the class TriggerTimerCatchEventTest method shouldTriggerTimerStartEvent.

@Test
@Timeout(value = 5, unit = TimeUnit.MINUTES)
void shouldTriggerTimerStartEvent() {
    // given
    final ZeebeClock clock = ZeebeClock.newDefaultClock(zeebeContainer);
    final BpmnModelInstance process = Bpmn.createExecutableProcess("process").startEvent().intermediateCatchEvent().timerWithDate(TIMER_DATE.toString()).serviceTask("task", b -> b.zeebeJobType(JOB_TYPE)).endEvent().done();
    final List<ActivatedJob> activatedJobs = new CopyOnWriteArrayList<>();
    final Instant brokerTime;
    // when
    final JobHandler handler = (client, job) -> activatedJobs.add(job);
    try (final ZeebeClient client = newZeebeClient(zeebeContainer);
        final JobWorker worker = newJobWorker(handler, client)) {
        client.newDeployCommand().addProcessModel(process, "process.bpmn").send().join();
        client.newCreateInstanceCommand().bpmnProcessId("process").latestVersion().send().join();
        brokerTime = clock.addTime(TIME_OFFSET);
        Awaitility.await("until a job has been activated by the worker").untilAsserted(() -> Assertions.assertThat(activatedJobs).hasSize(1));
    }
    // then
    Assertions.assertThat(activatedJobs).as("the timer event was triggered and a job is now available").hasSize(1);
    Assertions.assertThat(brokerTime).as("the modified time is at least equal to one day from now").isAfterOrEqualTo(TIMER_DATE);
}
Also used : ActivatedJob(io.camunda.zeebe.client.api.response.ActivatedJob) JobHandler(io.camunda.zeebe.client.api.worker.JobHandler) Testcontainers(org.testcontainers.junit.jupiter.Testcontainers) ZeebeContainer(io.zeebe.containers.ZeebeContainer) Bpmn(io.camunda.zeebe.model.bpmn.Bpmn) Instant(java.time.Instant) ZeebeClock(io.zeebe.containers.clock.ZeebeClock) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Duration(java.time.Duration) ZeebeClient(io.camunda.zeebe.client.ZeebeClient) ActivatedJob(io.camunda.zeebe.client.api.response.ActivatedJob) JobHandler(io.camunda.zeebe.client.api.worker.JobHandler) BpmnModelInstance(io.camunda.zeebe.model.bpmn.BpmnModelInstance) Assertions(org.assertj.core.api.Assertions) JobWorker(io.camunda.zeebe.client.api.worker.JobWorker) Awaitility(org.awaitility.Awaitility) Timeout(org.junit.jupiter.api.Timeout) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Container(org.testcontainers.junit.jupiter.Container) ZeebeClock(io.zeebe.containers.clock.ZeebeClock) Instant(java.time.Instant) ZeebeClient(io.camunda.zeebe.client.ZeebeClient) BpmnModelInstance(io.camunda.zeebe.model.bpmn.BpmnModelInstance) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) JobWorker(io.camunda.zeebe.client.api.worker.JobWorker) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 2 with ActivatedJob

use of io.camunda.zeebe.client.api.response.ActivatedJob in project spring-zeebe by camunda-community-hub.

the class JobHandlerInvokingSpringBeans method createParameters.

private List<Object> createParameters(JobClient jobClient, ActivatedJob job, List<ParameterInfo> parameters) {
    List<Object> args = new ArrayList<>();
    for (ParameterInfo param : parameters) {
        // parameter default null
        Object arg = null;
        Class<?> clazz = param.getParameterInfo().getType();
        if (JobClient.class.isAssignableFrom(clazz)) {
            arg = jobClient;
        } else if (ActivatedJob.class.isAssignableFrom(clazz)) {
            arg = job;
        } else if (param.getParameterInfo().isAnnotationPresent(ZeebeVariable.class)) {
            try {
                // TODO make this work for complex types as well
                arg = clazz.cast(job.getVariablesAsMap().get(param.getParameterName()));
            } catch (ClassCastException ex) {
                throw new RuntimeException("Cannot assign process variable '" + param.getParameterName() + "' to parameter when executing job '" + job.getType() + "', invalid type found: " + ex.getMessage());
            }
        } else if (param.getParameterInfo().isAnnotationPresent(ZeebeVariablesAsType.class)) {
            try {
                arg = job.getVariablesAsType(clazz);
            } catch (RuntimeException e) {
                throw new RuntimeException("Cannot assign process variables to type '" + clazz.getName() + "' when executing job '" + job.getType() + "', cause is: " + e.getMessage(), e);
            }
        } else if (param.getParameterInfo().isAnnotationPresent(ZeebeCustomHeaders.class)) {
            try {
                arg = job.getCustomHeaders();
            } catch (RuntimeException e) {
                throw new RuntimeException("Cannot assign headers '" + param.getParameterName() + "' to parameter when executing job '" + job.getType() + "', cause is: " + e.getMessage(), e);
            }
        }
        args.add(arg);
    }
    return args;
}
Also used : ActivatedJob(io.camunda.zeebe.client.api.response.ActivatedJob) ZeebeVariablesAsType(io.camunda.zeebe.spring.client.annotation.ZeebeVariablesAsType) ArrayList(java.util.ArrayList) ParameterInfo(io.camunda.zeebe.spring.client.bean.ParameterInfo)

Example 3 with ActivatedJob

use of io.camunda.zeebe.client.api.response.ActivatedJob in project micronaut-zeebe-client by camunda-community-hub.

the class ProcessTest method workerShouldProcessWork.

@Test
void workerShouldProcessWork() {
    // Deploy process model
    DeploymentEvent deploymentEvent = client.newDeployCommand().addResourceFromClasspath("bpmn/say_hello.bpmn").send().join();
    BpmnAssert.assertThat(deploymentEvent);
    // Start process instance
    ProcessInstanceEvent event = client.newCreateInstanceCommand().bpmnProcessId("Process_SayHello").latestVersion().send().join();
    engine.waitForIdleState();
    // Verify that process has started
    ProcessInstanceAssert processInstanceAssertions = BpmnAssert.assertThat(event);
    processInstanceAssertions.hasPassedElement("start");
    processInstanceAssertions.isWaitingAtElement("say_hello");
    // Fetch job: say-hello
    ActivateJobsResponse response = client.newActivateJobsCommand().jobType("say-hello").maxJobsToActivate(1).send().join();
    // Complete job: say-hello
    ActivatedJob activatedJob = response.getJobs().get(0);
    client.newCompleteCommand(activatedJob.getKey()).send().join();
    engine.waitForIdleState();
    // Fetch job: say-goodbye
    response = client.newActivateJobsCommand().jobType("say-goodbye").maxJobsToActivate(1).send().join();
    // Complete job: say-goodbye
    activatedJob = response.getJobs().get(0);
    client.newCompleteCommand(activatedJob.getKey()).send().join();
    engine.waitForIdleState();
    // Verify completed
    engine.waitForIdleState();
    processInstanceAssertions.isCompleted();
}
Also used : ActivatedJob(io.camunda.zeebe.client.api.response.ActivatedJob) ProcessInstanceAssert(io.camunda.zeebe.process.test.assertions.ProcessInstanceAssert) ActivateJobsResponse(io.camunda.zeebe.client.api.response.ActivateJobsResponse) ProcessInstanceEvent(io.camunda.zeebe.client.api.response.ProcessInstanceEvent) DeploymentEvent(io.camunda.zeebe.client.api.response.DeploymentEvent) Test(org.junit.jupiter.api.Test) ZeebeProcessTest(io.camunda.zeebe.process.test.extensions.ZeebeProcessTest)

Example 4 with ActivatedJob

use of io.camunda.zeebe.client.api.response.ActivatedJob in project zeebe by camunda.

the class JobWorkerWithMultiplePartitionsTest method shouldReceiveJobsFromMultiplePartitions.

@Test
public void shouldReceiveJobsFromMultiplePartitions() {
    // given
    final String jobType = helper.getJobType();
    final RecordingJobHandler handler = new RecordingJobHandler();
    IntStream.range(0, PARTITION_COUNT).forEach(p -> CLIENT_RULE.createSingleJob(jobType));
    // when
    CLIENT_RULE.getClient().newWorker().jobType(jobType).handler(handler).name("test").open();
    // then
    waitUntil(() -> handler.getHandledJobs().size() >= PARTITION_COUNT);
    final List<Integer> receivedPartitionIds = handler.getHandledJobs().stream().map(ActivatedJob::getKey).map(Protocol::decodePartitionId).collect(Collectors.toList());
    assertThat(receivedPartitionIds).containsExactlyInAnyOrderElementsOf(CLIENT_RULE.getPartitions());
}
Also used : RecordingJobHandler(io.camunda.zeebe.it.util.RecordingJobHandler) ActivatedJob(io.camunda.zeebe.client.api.response.ActivatedJob) Test(org.junit.Test)

Example 5 with ActivatedJob

use of io.camunda.zeebe.client.api.response.ActivatedJob in project zeebe by camunda.

the class AvailabilityTest method shouldActivateJobsRoundRobinWhenOnePartitionDown.

@Test
public void shouldActivateJobsRoundRobinWhenOnePartitionDown() {
    // given
    final int numInstances = 2 * partitionCount;
    final BrokerInfo leaderForPartition = clusteringRule.getLeaderForPartition(partitionCount);
    clusteringRule.stopBroker(leaderForPartition.getNodeId());
    for (int i = 0; i < numInstances; i++) {
        clientRule.createProcessInstance(processDefinitionKey);
    }
    // when
    Awaitility.await().until(() -> RecordingExporter.jobRecords(JobIntent.CREATED).limit(numInstances).count() == numInstances);
    final Set<Long> activatedJobsKey = new HashSet<>();
    final List<ActivatedJob> jobs = clientRule.getClient().newActivateJobsCommand().jobType(JOBTYPE).maxJobsToActivate(2 * numInstances).timeout(Duration.ofMinutes(5)).send().join().getJobs();
    jobs.forEach(job -> activatedJobsKey.add(job.getKey()));
    // then
    assertThat(activatedJobsKey).hasSize(numInstances);
}
Also used : ActivatedJob(io.camunda.zeebe.client.api.response.ActivatedJob) BrokerInfo(io.camunda.zeebe.client.api.response.BrokerInfo) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ActivatedJob (io.camunda.zeebe.client.api.response.ActivatedJob)46 Test (org.junit.Test)27 Bpmn (io.camunda.zeebe.model.bpmn.Bpmn)16 List (java.util.List)16 Test (org.junit.jupiter.api.Test)16 BrokerInfo (io.camunda.zeebe.client.api.response.BrokerInfo)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)14 ZeebeClient (io.camunda.zeebe.client.ZeebeClient)13 Duration (java.time.Duration)13 Awaitility (org.awaitility.Awaitility)13 ProcessInstanceEvent (io.camunda.zeebe.client.api.response.ProcessInstanceEvent)12 Map (java.util.Map)11 ActivateJobsResponse (io.camunda.zeebe.client.api.response.ActivateJobsResponse)9 DeploymentEvent (io.camunda.zeebe.client.api.response.DeploymentEvent)9 ZeebeFuture (io.camunda.zeebe.client.api.ZeebeFuture)8 ClientException (io.camunda.zeebe.client.api.command.ClientException)8 PartitionBrokerHealth (io.camunda.zeebe.client.api.response.PartitionBrokerHealth)8 PartitionBrokerRole (io.camunda.zeebe.client.api.response.PartitionBrokerRole)8 PartitionInfo (io.camunda.zeebe.client.api.response.PartitionInfo)8 Process (io.camunda.zeebe.client.api.response.Process)8