Search in sources :

Example 1 with WorkerCfg

use of io.camunda.zeebe.config.WorkerCfg in project zeebe by camunda-cloud.

the class Worker method run.

@Override
public void run() {
    final WorkerCfg workerCfg = appCfg.getWorker();
    final String jobType = workerCfg.getJobType();
    final long completionDelay = workerCfg.getCompletionDelay().toMillis();
    final var variables = readVariables(workerCfg.getPayloadPath());
    final BlockingQueue<Future<?>> requestFutures = new ArrayBlockingQueue<>(10_000);
    final BlockingDeque<DelayedCommand> delayedCommands = new LinkedBlockingDeque<>(10_000);
    final ZeebeClient client = createZeebeClient();
    printTopology(client);
    final JobWorker worker = client.newWorker().jobType(jobType).handler((jobClient, job) -> {
        final var command = jobClient.newCompleteCommand(job.getKey()).variables(variables);
        if (workerCfg.isCompleteJobsAsync()) {
            delayedCommands.addLast(new DelayedCommand(Instant.now().plusMillis(completionDelay), command));
        } else {
            try {
                Thread.sleep(completionDelay);
            } catch (Exception e) {
                e.printStackTrace();
            }
            requestFutures.add(command.send());
        }
    }).open();
    final ResponseChecker responseChecker = new ResponseChecker(requestFutures);
    responseChecker.start();
    final var asyncJobCompleter = new DelayedCommandSender(delayedCommands, requestFutures);
    if (workerCfg.isCompleteJobsAsync()) {
        asyncJobCompleter.start();
    }
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        worker.close();
        client.close();
        asyncJobCompleter.close();
        responseChecker.close();
    }));
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Future(java.util.concurrent.Future) AppCfg(io.camunda.zeebe.config.AppCfg) BlockingDeque(java.util.concurrent.BlockingDeque) FinalCommandStep(io.camunda.zeebe.client.api.command.FinalCommandStep) ZeebeClient(io.camunda.zeebe.client.ZeebeClient) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) WorkerCfg(io.camunda.zeebe.config.WorkerCfg) BlockingQueue(java.util.concurrent.BlockingQueue) JobWorker(io.camunda.zeebe.client.api.worker.JobWorker) Instant(java.time.Instant) ZeebeClientBuilder(io.camunda.zeebe.client.ZeebeClientBuilder) WorkerCfg(io.camunda.zeebe.config.WorkerCfg) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ZeebeClient(io.camunda.zeebe.client.ZeebeClient) JobWorker(io.camunda.zeebe.client.api.worker.JobWorker) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Future(java.util.concurrent.Future)

Example 2 with WorkerCfg

use of io.camunda.zeebe.config.WorkerCfg in project zeebe by zeebe-io.

the class Worker method createZeebeClient.

private ZeebeClient createZeebeClient() {
    final WorkerCfg workerCfg = appCfg.getWorker();
    final ZeebeClientBuilder builder = ZeebeClient.newClientBuilder().gatewayAddress(appCfg.getBrokerUrl()).numJobWorkerExecutionThreads(workerCfg.getThreads()).defaultJobWorkerName(workerCfg.getWorkerName()).defaultJobTimeout(workerCfg.getCompletionDelay().multipliedBy(6)).defaultJobWorkerMaxJobsActive(workerCfg.getCapacity()).defaultJobPollInterval(workerCfg.getPollingDelay()).withProperties(System.getProperties()).withInterceptors(monitoringInterceptor);
    if (!appCfg.isTls()) {
        builder.usePlaintext();
    }
    return builder.build();
}
Also used : WorkerCfg(io.camunda.zeebe.config.WorkerCfg) ZeebeClientBuilder(io.camunda.zeebe.client.ZeebeClientBuilder)

Example 3 with WorkerCfg

use of io.camunda.zeebe.config.WorkerCfg in project zeebe by zeebe-io.

the class Worker method run.

@Override
public void run() {
    final WorkerCfg workerCfg = appCfg.getWorker();
    final String jobType = workerCfg.getJobType();
    final long completionDelay = workerCfg.getCompletionDelay().toMillis();
    final var variables = readVariables(workerCfg.getPayloadPath());
    final BlockingQueue<Future<?>> requestFutures = new ArrayBlockingQueue<>(10_000);
    final BlockingDeque<DelayedCommand> delayedCommands = new LinkedBlockingDeque<>(10_000);
    final ZeebeClient client = createZeebeClient();
    printTopology(client);
    final JobWorker worker = client.newWorker().jobType(jobType).handler((jobClient, job) -> {
        final var command = jobClient.newCompleteCommand(job.getKey()).variables(variables);
        if (workerCfg.isCompleteJobsAsync()) {
            delayedCommands.addLast(new DelayedCommand(Instant.now().plusMillis(completionDelay), command));
        } else {
            try {
                Thread.sleep(completionDelay);
            } catch (Exception e) {
                e.printStackTrace();
            }
            requestFutures.add(command.send());
        }
    }).open();
    final ResponseChecker responseChecker = new ResponseChecker(requestFutures);
    responseChecker.start();
    final var asyncJobCompleter = new DelayedCommandSender(delayedCommands, requestFutures);
    if (workerCfg.isCompleteJobsAsync()) {
        asyncJobCompleter.start();
    }
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        worker.close();
        client.close();
        asyncJobCompleter.close();
        responseChecker.close();
    }));
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Future(java.util.concurrent.Future) AppCfg(io.camunda.zeebe.config.AppCfg) BlockingDeque(java.util.concurrent.BlockingDeque) FinalCommandStep(io.camunda.zeebe.client.api.command.FinalCommandStep) ZeebeClient(io.camunda.zeebe.client.ZeebeClient) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) WorkerCfg(io.camunda.zeebe.config.WorkerCfg) BlockingQueue(java.util.concurrent.BlockingQueue) JobWorker(io.camunda.zeebe.client.api.worker.JobWorker) Instant(java.time.Instant) ZeebeClientBuilder(io.camunda.zeebe.client.ZeebeClientBuilder) WorkerCfg(io.camunda.zeebe.config.WorkerCfg) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ZeebeClient(io.camunda.zeebe.client.ZeebeClient) JobWorker(io.camunda.zeebe.client.api.worker.JobWorker) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Future(java.util.concurrent.Future)

Example 4 with WorkerCfg

use of io.camunda.zeebe.config.WorkerCfg in project zeebe by camunda-cloud.

the class Worker method createZeebeClient.

private ZeebeClient createZeebeClient() {
    final WorkerCfg workerCfg = appCfg.getWorker();
    final ZeebeClientBuilder builder = ZeebeClient.newClientBuilder().gatewayAddress(appCfg.getBrokerUrl()).numJobWorkerExecutionThreads(workerCfg.getThreads()).defaultJobWorkerName(workerCfg.getWorkerName()).defaultJobTimeout(workerCfg.getCompletionDelay().multipliedBy(6)).defaultJobWorkerMaxJobsActive(workerCfg.getCapacity()).defaultJobPollInterval(workerCfg.getPollingDelay()).withProperties(System.getProperties()).withInterceptors(monitoringInterceptor);
    if (!appCfg.isTls()) {
        builder.usePlaintext();
    }
    return builder.build();
}
Also used : WorkerCfg(io.camunda.zeebe.config.WorkerCfg) ZeebeClientBuilder(io.camunda.zeebe.client.ZeebeClientBuilder)

Example 5 with WorkerCfg

use of io.camunda.zeebe.config.WorkerCfg in project zeebe by camunda.

the class Worker method createZeebeClient.

private ZeebeClient createZeebeClient() {
    final WorkerCfg workerCfg = appCfg.getWorker();
    final ZeebeClientBuilder builder = ZeebeClient.newClientBuilder().gatewayAddress(appCfg.getBrokerUrl()).numJobWorkerExecutionThreads(workerCfg.getThreads()).defaultJobWorkerName(workerCfg.getWorkerName()).defaultJobTimeout(workerCfg.getCompletionDelay().multipliedBy(6)).defaultJobWorkerMaxJobsActive(workerCfg.getCapacity()).defaultJobPollInterval(workerCfg.getPollingDelay()).withProperties(System.getProperties()).withInterceptors(monitoringInterceptor);
    if (!appCfg.isTls()) {
        builder.usePlaintext();
    }
    return builder.build();
}
Also used : WorkerCfg(io.camunda.zeebe.config.WorkerCfg) ZeebeClientBuilder(io.camunda.zeebe.client.ZeebeClientBuilder)

Aggregations

ZeebeClientBuilder (io.camunda.zeebe.client.ZeebeClientBuilder)6 WorkerCfg (io.camunda.zeebe.config.WorkerCfg)6 ZeebeClient (io.camunda.zeebe.client.ZeebeClient)3 FinalCommandStep (io.camunda.zeebe.client.api.command.FinalCommandStep)3 JobWorker (io.camunda.zeebe.client.api.worker.JobWorker)3 AppCfg (io.camunda.zeebe.config.AppCfg)3 Instant (java.time.Instant)3 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)3 BlockingDeque (java.util.concurrent.BlockingDeque)3 BlockingQueue (java.util.concurrent.BlockingQueue)3 Future (java.util.concurrent.Future)3 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)3