Class JobWorkerBuilderImpl
- java.lang.Object
-
- io.camunda.zeebe.client.impl.worker.JobWorkerBuilderImpl
-
- All Implemented Interfaces:
JobWorkerBuilderStep1
,JobWorkerBuilderStep1.JobWorkerBuilderStep2
,JobWorkerBuilderStep1.JobWorkerBuilderStep3
public final class JobWorkerBuilderImpl extends Object implements JobWorkerBuilderStep1, JobWorkerBuilderStep1.JobWorkerBuilderStep2, JobWorkerBuilderStep1.JobWorkerBuilderStep3
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.camunda.zeebe.client.api.worker.JobWorkerBuilderStep1
JobWorkerBuilderStep1.JobWorkerBuilderStep2, JobWorkerBuilderStep1.JobWorkerBuilderStep3
-
-
Field Summary
Fields Modifier and Type Field Description static BackoffSupplier
DEFAULT_BACKOFF_SUPPLIER
-
Constructor Summary
Constructors Constructor Description JobWorkerBuilderImpl(ZeebeClientConfiguration configuration, GatewayGrpc.GatewayStub gatewayStub, JobClient jobClient, JsonMapper jsonMapper, ScheduledExecutorService executorService, List<Closeable> closeables, Predicate<Throwable> retryPredicate)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JobWorkerBuilderStep1.JobWorkerBuilderStep3
backoffSupplier(BackoffSupplier backoffSupplier)
Sets the backoff supplier.JobWorkerBuilderStep1.JobWorkerBuilderStep3
fetchVariables(String... fetchVariables)
Set a list of variable names which should be fetch on job activation.JobWorkerBuilderStep1.JobWorkerBuilderStep3
fetchVariables(List<String> fetchVariables)
Set a list of variable names which should be fetch on job activation.JobWorkerBuilderStep1.JobWorkerBuilderStep3
handler(JobHandler handler)
Set the handler to process the jobs.JobWorkerBuilderStep1.JobWorkerBuilderStep2
jobType(String type)
Set the type of jobs to work on.JobWorkerBuilderStep1.JobWorkerBuilderStep3
maxJobsActive(int maxJobsActive)
Set the maximum number of jobs which will be exclusively activated for this worker at the same time.JobWorkerBuilderStep1.JobWorkerBuilderStep3
name(String workerName)
Set the name of the worker owner.JobWorker
open()
Open the worker and start to work on available tasks.JobWorkerBuilderStep1.JobWorkerBuilderStep3
pollInterval(Duration pollInterval)
Set the maximal interval between polling for new jobs.JobWorkerBuilderStep1.JobWorkerBuilderStep3
requestTimeout(Duration requestTimeout)
Set the request timeout for activate job request used to poll for new job.JobWorkerBuilderStep1.JobWorkerBuilderStep3
timeout(long timeout)
Set the time for how long a job is exclusively assigned for this worker.JobWorkerBuilderStep1.JobWorkerBuilderStep3
timeout(Duration timeout)
Set the time for how long a job is exclusively assigned for this worker.
-
-
-
Field Detail
-
DEFAULT_BACKOFF_SUPPLIER
public static final BackoffSupplier DEFAULT_BACKOFF_SUPPLIER
-
-
Constructor Detail
-
JobWorkerBuilderImpl
public JobWorkerBuilderImpl(ZeebeClientConfiguration configuration, GatewayGrpc.GatewayStub gatewayStub, JobClient jobClient, JsonMapper jsonMapper, ScheduledExecutorService executorService, List<Closeable> closeables, Predicate<Throwable> retryPredicate)
-
-
Method Detail
-
jobType
public JobWorkerBuilderStep1.JobWorkerBuilderStep2 jobType(String type)
Description copied from interface:JobWorkerBuilderStep1
Set the type of jobs to work on.- Specified by:
jobType
in interfaceJobWorkerBuilderStep1
- Parameters:
type
- the type of jobs (e.g. "payment")- Returns:
- the builder for this worker
-
handler
public JobWorkerBuilderStep1.JobWorkerBuilderStep3 handler(JobHandler handler)
Description copied from interface:JobWorkerBuilderStep1.JobWorkerBuilderStep2
Set the handler to process the jobs. At the end of the processing, the handler should complete the job or mark it as failed;Example JobHandler implementation:
public final class PaymentHandler implements JobHandler { @Override public void handle(JobClient client, JobEvent jobEvent) { String json = jobEvent.getVariables(); // modify variables client .newCompleteCommand(jobEvent.getKey()) .variables(json) .send(); } };
The handler must be thread-safe.- Specified by:
handler
in interfaceJobWorkerBuilderStep1.JobWorkerBuilderStep2
- Parameters:
handler
- the handle to process the jobs- Returns:
- the builder for this worker
-
timeout
public JobWorkerBuilderStep1.JobWorkerBuilderStep3 timeout(long timeout)
Description copied from interface:JobWorkerBuilderStep1.JobWorkerBuilderStep3
Set the time for how long a job is exclusively assigned for this worker.In this time, the job can not be assigned by other workers to ensure that only one worker work on the job. When the time is over then the job can be assigned again by this or other worker if it's not completed yet.
If no timeout is set, then the default is used from the configuration.
- Specified by:
timeout
in interfaceJobWorkerBuilderStep1.JobWorkerBuilderStep3
- Parameters:
timeout
- the time in milliseconds- Returns:
- the builder for this worker
-
timeout
public JobWorkerBuilderStep1.JobWorkerBuilderStep3 timeout(Duration timeout)
Description copied from interface:JobWorkerBuilderStep1.JobWorkerBuilderStep3
Set the time for how long a job is exclusively assigned for this worker.In this time, the job can not be assigned by other workers to ensure that only one worker work on the job. When the time is over then the job can be assigned again by this or other worker if it's not completed yet.
If no time is set then the default is used from the configuration.
- Specified by:
timeout
in interfaceJobWorkerBuilderStep1.JobWorkerBuilderStep3
- Parameters:
timeout
- the time as duration (e.g. "Duration.ofMinutes(5)")- Returns:
- the builder for this worker
-
name
public JobWorkerBuilderStep1.JobWorkerBuilderStep3 name(String workerName)
Description copied from interface:JobWorkerBuilderStep1.JobWorkerBuilderStep3
Set the name of the worker owner.This name is used to identify the worker to which a job is exclusively assigned to.
If no name is set then the default is used from the configuration.
- Specified by:
name
in interfaceJobWorkerBuilderStep1.JobWorkerBuilderStep3
- Parameters:
workerName
- the name of the worker (e.g. "payment-service")- Returns:
- the builder for this worker
-
maxJobsActive
public JobWorkerBuilderStep1.JobWorkerBuilderStep3 maxJobsActive(int maxJobsActive)
Description copied from interface:JobWorkerBuilderStep1.JobWorkerBuilderStep3
Set the maximum number of jobs which will be exclusively activated for this worker at the same time.This is used to control the backpressure of the worker. When the maximum is reached then the worker will stop activating new jobs in order to not overwhelm the client and give other workers the chance to work on the jobs. The worker will try to activate new jobs again when jobs are completed (or marked as failed).
If no maximum is set then the default, from the
ZeebeClientConfiguration
, is used.Considerations:
- A greater value can avoid situations in which the client waits idle for the broker to provide more jobs. This can improve the worker's throughput.
- The memory used by the worker is linear with respect to this value.
- The job's timeout starts to run down as soon as the broker pushes the job. Keep in mind
that the following must hold to ensure fluent job handling:
time spent in queue + time job handler needs until job completion < job timeout
- Specified by:
maxJobsActive
in interfaceJobWorkerBuilderStep1.JobWorkerBuilderStep3
- Parameters:
maxJobsActive
- the maximum jobs active by this worker- Returns:
- the builder for this worker
-
pollInterval
public JobWorkerBuilderStep1.JobWorkerBuilderStep3 pollInterval(Duration pollInterval)
Description copied from interface:JobWorkerBuilderStep1.JobWorkerBuilderStep3
Set the maximal interval between polling for new jobs.A job worker will automatically try to always activate new jobs after completing jobs. If no jobs can be activated after completing the worker will periodically poll for new jobs.
If no poll interval is set then the default is used from the
ZeebeClientConfiguration
- Specified by:
pollInterval
in interfaceJobWorkerBuilderStep1.JobWorkerBuilderStep3
- Parameters:
pollInterval
- the maximal interval to check for new jobs- Returns:
- the builder for this worker
-
requestTimeout
public JobWorkerBuilderStep1.JobWorkerBuilderStep3 requestTimeout(Duration requestTimeout)
Description copied from interface:JobWorkerBuilderStep1.JobWorkerBuilderStep3
Set the request timeout for activate job request used to poll for new job.If no request timeout is set then the default is used from the
ZeebeClientConfiguration
- Specified by:
requestTimeout
in interfaceJobWorkerBuilderStep1.JobWorkerBuilderStep3
- Parameters:
requestTimeout
- the request timeout for activate jobs request- Returns:
- the builder for this worker
-
fetchVariables
public JobWorkerBuilderStep1.JobWorkerBuilderStep3 fetchVariables(List<String> fetchVariables)
Description copied from interface:JobWorkerBuilderStep1.JobWorkerBuilderStep3
Set a list of variable names which should be fetch on job activation.The jobs which are activated by this worker will only contain variables from this list.
This can be used to limit the number of variables of the activated jobs.
- Specified by:
fetchVariables
in interfaceJobWorkerBuilderStep1.JobWorkerBuilderStep3
- Parameters:
fetchVariables
- list of variables names to fetch on activation- Returns:
- the builder for this worker
-
fetchVariables
public JobWorkerBuilderStep1.JobWorkerBuilderStep3 fetchVariables(String... fetchVariables)
Description copied from interface:JobWorkerBuilderStep1.JobWorkerBuilderStep3
Set a list of variable names which should be fetch on job activation.The jobs which are activated by this worker will only contain variables from this list.
This can be used to limit the number of variables of the activated jobs.
- Specified by:
fetchVariables
in interfaceJobWorkerBuilderStep1.JobWorkerBuilderStep3
- Parameters:
fetchVariables
- list of variables names to fetch on activation- Returns:
- the builder for this worker
-
backoffSupplier
public JobWorkerBuilderStep1.JobWorkerBuilderStep3 backoffSupplier(BackoffSupplier backoffSupplier)
Description copied from interface:JobWorkerBuilderStep1.JobWorkerBuilderStep3
Sets the backoff supplier. The supplier is called to determine the retry delay after each failed request; the worker then waits until the returned delay has elapsed before sending the next request. Note that this is used only for the polling mechanism - failures in theJobHandler
should be handled there, and retried there if need be.By default, the supplier uses exponential back off, with an upper bound of 5 seconds. The exponential backoff can be easily configured using
BackoffSupplier.newBackoffBuilder()
.- Specified by:
backoffSupplier
in interfaceJobWorkerBuilderStep1.JobWorkerBuilderStep3
- Parameters:
backoffSupplier
- supplies the retry delay after a failed request- Returns:
- the builder for this worker
-
open
public JobWorker open()
Description copied from interface:JobWorkerBuilderStep1.JobWorkerBuilderStep3
Open the worker and start to work on available tasks.- Specified by:
open
in interfaceJobWorkerBuilderStep1.JobWorkerBuilderStep3
- Returns:
- the worker
-
-