Options

External Tasks

Internal Service tasks: Synchronous invocation of code deployed along with a process application

External tasks: Providing a unit of work in a list that can be polled by workers

The first option is used when code is implemented as Delegation Code or as a Script. By contrast, external (service) tasks work in a way that the process engine publishes a unit of work to a worker to fetch and complete. We refer to this as the external task pattern.

Note that the above distinction does not say whether the actual “business logic” is implemented locally or as a remote service. The Java Delegate invoked by an internal service task may either implement the business logic itself or it may call out to a web/rest service, send a message to another system and so forth. The same is true for an external worker. The worker can implement the business logic directly or again delegate to a remote system.

The External Task Pattern

The flow of executing external tasks can be conceptually separated into three steps, as depicted in the following image:

Process Engine: Creation of an external task instance

External Worker: Fetch and lock external tasks

External Worker & Process Engine: Complete external task instance

When the process engine encounters a service task that is configured to be externally handled, it creates an external task instance and adds it to a list of external tasks (step 1). The task instance receives a topic that identifies the nature of the work to be performed. At a time in the future, an external worker may fetch and lock tasks for a specific set of topics (step 2). To prevent one task being fetched by multiple workers at the same time, a task has a timestamp-based lock that is set when the task is acquired. Only when the lock expires, another worker can fetch the task again. When an external worker has completed the desired work, it can signal the process engine to continue process execution after the service task (step 3).

The User Task Analogy

External tasks are conceptually very similar to user tasks. When first trying to understand the external task pattern, it can be helpful to think about it in analogy to user tasks:
User tasks are created by the process engine and added to a task list. The process engine then waits for a human user to query the list, claim a task and then complete it. External tasks are similar: An external task is created and then added to a topic. An external application then queries the topic and locks the task. After the task is locked, the application can work on it and complete it.

The essence of this pattern is that the entities performing the actual work are independent of the process engine and receive work items by polling the process engine’s API. This has the following benefits:

Crossing System Boundaries: An external worker does not need to run in the same Java process, on the same machine, in the same cluster or even on the same continent as the process engine. All that is required is that it can access the process engine’s API (via REST or Java). Due to the polling pattern, the worker does not need to expose any interface for the process engine to access.

Crossing Technology Boundaries: An external worker does not need to be implemented in Java. Instead, any technology can be used that is most suitable to perform a work item and that can be used to access the process engine’s API (via REST or Java).

Specialized Workers: An external worker does not need to be a general purpose application. Each external task instance receives a topic name identifying the nature of the task to perform. Workers can poll tasks for only those topics that they can work on.

Fine-Grained Scaling: If there is high load concentrated on service task processing, the number of external workers for the respective topics can be scaled out independently of the process engine.

Independent Maintenance: Workers can be maintained independently of the process engine without breaking operations. For example, if a worker for a specific topic has a downtime (e.g., due to an update), there is no immediate impact on the process engine. Execution of external tasks for such workers degrades gracefully: They are stored in the external task list until the external worker resumes operation.

Working with External Tasks

To work with external tasks they have to be declared in the BPMN XML. At runtime, external task instances can be accessed via Java and REST API. The following explains the API concepts and focuses on the Java API. Often the REST API is more suitable in this context, especially when implementing workers running in different environments with different technologies.

BPMN

In the BPMN XML of a process definition, a service task can be declared to be performed by an external worker by using the attributes camunda:type and camunda:topic. For example, a service task Validate Address can be configured to provide an external task instance for the topic AddressValidation as follows:

In addition, other service-task-like elements such as send tasks, business rule tasks, and throwing message events can be implemented with the external task pattern. See the BPMN 2.0 implementation reference for details.

The following sections address the different interactions with the ExternalTaskService in greater detail.

Fetching Tasks

In order to implement a polling worker, a fetching operation can be executed by using the method ExternalTaskService#fetchAndLock. This method returns a fluent builder that allows to define a set of topics to fetch tasks for. Consider the following code snippet:

This code fetches at most 10 tasks of the topics AddressValidation and ShipmentScheduling. The result tasks are locked exclusively for the worker with id externalWorkerId. Locking means that the task is reserved for this worker for a certain duration beginning with the time of fetching and prevents that another worker can fetch the same task while the lock is valid. If the lock expires and the task has not been completed meanwhile, a different worker can fetch it such that silently failing workers do not block execution indefinitely. The exact duration is given in the single topic fetch instructions: Tasks for AddressValidation are locked for 60 seconds (60L * 1000L milliseconds) while tasks for ShipmentScheduling are locked for 120 seconds (120L * 1000L milliseconds). The lock expiration duration should not be shorter than than the expected execution time. It should also not be too high if that implies a too long timeout until the task is retried in case the worker fails silently.

Variables that are required to perform a task can be fetched along with the task. For example, assume that the AddressValidation task requires an address variable. Fetching tasks with this variable could look like:

The resulting tasks then contain the current values of the requested variable. Note that the variable values are the values that are visible in the scope hierarchy from the external task’s execution. See the chapter on Variable Scopes and Variable Visibility for details.

In order to fetch all variables, call to variables() method should be omitted

In order to enable the deserialization of serialized variables values (typically variables that store custom Java objects), it is necessary to call enableCustomObjectDeserialization(). Otherwise an exception, that the object is not deserialized, is thrown once the serialized variable is retrieved from the variables map.

External Task Prioritization

External task prioritization is similar to job prioritization. The same problem exists with starvation which should be considered.
For further details, see the section on Job Prioritization.

Configure the Process Engine for External Task Priorities

This section explains how to enable and disable external task priorities in the configuration. There are two relevant configuration properties which can be set on the process engine configuration:

producePrioritizedExternalTasks: Controls whether the process engine assigns priorities to external tasks. The default value is true.
If priorities are not needed, the process engine configuration property producePrioritizedExternalTasks can be set to false. In this case, all external tasks receive a priority of 0.
For details on how to specify external task priorities and how the process engine assigns them, see the following section on Specifying External Task Priorities.

Specify External Task Priorities

External task priorities can be specified in the BPMN model as well as overridden at runtime via API.

Priorities in BPMN XML

External task priorities can be assigned at the process or the activity level. To achieve this, the Camunda extension attribute camunda:taskPriority can be used.

For specifying the priority, both constant values and expressions are supported.
When using a constant value, the same priority is assigned to all instances of the process or activity.
Expressions, on the other hand, allow assigning a different priority to each instance of the process or activity. Expression must evaluate to a number in the Java long range.
The concrete value can be the result of a complex calculation and be based on user-provided data (resulting from a task form or other sources).

Priorities at the Process Level

When configuring external task priorities at the process instance level, the camunda:taskPriority attribute needs to be applied to the bpmn <process ...> element:

The effect is that all external tasks inside the process inherit the same priority (unless it is overridden locally).
The above example shows how a constant value can be used for setting the priority. This way the same priority is applied to all instances of the process.
If different process instances need to be executed with different priorities, an expression can be used:

In the above example the priority is determined based on the property priority of the variable order.

Priorities at the Service Task Level

When configuring external task priorities at the service task level, the camunda:taskPriority attribute needs to be applied to the bpmn <serviceTask ...> element.
The service task must be an external task with the attribute camunda:type="external".

The effect is that the priority is set for the defined external task (overrides the process taskPriority).
The above example shows how a constant value can be used for setting the priority. This way the same priority is applied to the external task in different instances of the process.
If different process instances need to be executed with different external task priorities, an expression can be used:

In the above example the priority is determined based on the property priority of the variable order.

Fetch External Task with Priority

To fetch external tasks based on their priority, the overloaded method ExternalTaskService#fetchAndLock with the parameter usePriority can be used.
The method without the boolean parameter returns the external tasks arbitrarily. If the parameter is given, the returned external tasks are ordered descendingly.
See the following example which regards the priority of the external tasks:

Completing Tasks

After fetching and performing the requested work, a worker can complete an external task by calling the ExternalTaskService#complete method. A worker can only complete tasks that it fetched and locked before. If the task has been locked by a different worker in the meantime, an exception is raised.

Reporting Task Failure

A worker may not always be able to complete a task successfully. In this case it can report a failure to the process engine by using ExternalTaskService#handleFailure. Like #complete, #handleFailure can only be invoked by the worker possessing the most recent lock for a task. The #handleFailure method takes four additional arguments: errorMessage,errorDetails, retries, retryTimeout. The error message can contain a description of the nature of the problem and is limited to 666 characters. It can be accessed when the task is fetched again or is queried for. The errorDetails are containing full error description and are unlimited in legth, error details are accessible through the separate method, based on task id. With retries and retryTimout, workers can specify a retry strategy. When setting retries to a value > 0, the task can be fetched again after retryTimeout expires. When setting retries to 0, a task can no longer be fetched and an incident is created for this task.

A failure is reported for the locked task such that it can be retried once more after 10 minutes. The process engine does not decrement retries itself. Instead, such a behavior can be implemented by setting the retries to task.getRetries() - 1 when reporting a failure.

At the moment when error details are required, they are queried from the service using separate method.

Reporting BPMN Error

For some reason a business error can appear during execution. In this case, the worker can report a BPMN error to the process engine by using ExternalTaskService#handleBpmnError.
Like #complete or #handleFailure, it can only be invoked by the worker possessing the most recent lock for a task.
The #handleBpmnError method takes one additional argument: errorCode.
The error code identifies a predefined error. If the given errorCode does not exist or there is no boundary event defined,
the current activity instance simply ends and the error is not handled.

A BPMN error with the error code bpmn-error is propagated. If a boundary event with this error code exists, the BPMN error will be caught and handled.

Querying Tasks

A query for external tasks can be made via ExternalTaskService#createExternalTaskQuery. Contrary to #fetchAndLock, this is a reading query that does not set any locks.

Managing Operations

Additional management operations are ExternalTaskService#unlock, ExternalTaskService#setRetries and ExternalTaskService#setPriority to clear the current lock, to set the retries and to set the priority of an external task.
Setting the retries is useful when a task has 0 retries left and must be resumed manually. With the last method the priority can
be set to a higher value for more important or to a lower value for less important external tasks.