A User Task is used to model work that needs to be done by a human actor. When the process execution arrives at such a User Task, a new task is created in the task list of the user(s) or group(s) assigned to that task.

User Task - 图1

A User Task is defined in XML as follows. The id attribute is required, while the name attribute is optional.

  1. <userTask id="theTask" name="Important task" />

Description

A User Task can also have a description. In fact, any BPMN 2.0 element can have a description. A description is defined by adding the documentation element.

  1. <userTask id="theTask" name="Schedule meeting" >
  2. <documentation>
  3. Schedule an engineering meeting for next week with the new hire.
  4. </documentation>

The description text can be retrieved from the task in the standard Java way:

  1. task.getDescription();

Properties

Due Date

Each task has a field indicating the due date of that task. The Query API can be used to query for tasks that are due on, before or after a certain date.

There is an activity extension which allows you to specify an expression in your task-definition to set the initial due date of a task when it is created. The expression should always resolve to a java.util.Date, java.util.String (ISO8601 formatted) or null. When using ISO8601 formatted Strings, you may either specify an exact point in time or a time period relative to the time the task is created. For example, you could use a date that was entered in a previous form in the process or calculated in a previous Service Task.

  1. <userTask id="theTask" name="Important task" camunda:dueDate="${dateVariable}"/>

The due date of a task can also be altered using the TaskService or in TaskListeners using the passed DelegateTask.

Follow Up Date

Each task has a field indicating the follow up date of that task. The Query API can be used to query for tasks that need to be followed up on, before or after a certain date.

There is an activity extension which allows you to specify an expression in your task-definition to set the initial follow up date of a task when it is created. The expression should always resolve to a java.util.Date, java.util.String (ISO8601 formatted) or null. When using ISO8601 formatted Strings, you may either specify an exact point in time or a time period relative to the time the task is created. For example, you could use a date that was entered in a previous form in the process or calculated in a previous Service Task.

  1. <userTask id="theTask" name="Important task" camunda:followUpDate="${dateVariable}"/>

User Assignment

A User Task can be directly assigned to a single user, a list of users or a list of groups.

Assignment using BPMN Resource Assignments

BPMN defines some native assignment concepts which can be used in camunda.As a more powerful alternative, Camunda also defines a set of custom extension elements (see below).

Human Performer

This is done by defining a humanPerformer sub element. Such a humanPerformer definition needs a resourceAssignmentExpression that actually defines the user. Currently, only formalExpressions are supported.

  1. <process ... >
  2. ...
  3. <userTask id='theTask' name='important task' >
  4. <humanPerformer>
  5. <resourceAssignmentExpression>
  6. <formalExpression>kermit</formalExpression>
  7. </resourceAssignmentExpression>
  8. </humanPerformer>
  9. </userTask>

Only one user can be assigned to the task as a human performer. In the engine terminology, this user is called the assignee. Tasks that have an assignee are not visible in the task lists of other users and can be found in the so-called personal task list of the assignee instead.

Tasks directly assigned to users can be retrieved through the TaskService as follows:

  1. List<Task> tasks = taskService.createTaskQuery().taskAssignee("kermit").list();

Potential Owner

Tasks can also be put in the so-called candidate task list of people. In that case, the potentialOwner construct must be used. The usage is similar to the humanPerformer construct. Please note that for each element in the formal expression it is required to specifically define if it is a user or a group (the engine cannot guess this).

  1. <process ... >
  2. ...
  3. <userTask id='theTask' name='important task' >
  4. <potentialOwner>
  5. <resourceAssignmentExpression>
  6. <formalExpression>user(kermit), group(management)</formalExpression>
  7. </resourceAssignmentExpression>
  8. </potentialOwner>
  9. </userTask>

Tasks defined with the potential owner construct can be retrieved as follows (or a similar TaskQuery, such as for tasks which have an assignee, can be used):

  1. List<Task> tasks = taskService.createTaskQuery().taskCandidateUser("kermit");

This will retrieve all tasks where kermit is a candidate user, i.e., the formal expression contains the user kermit. This will also retrieve all tasks that are assigned to a group of which kermit is a member (e.g., group(management), if kermit is a member of that group and the identity component is used). The groups of a user are resolved at runtime and these can be managed through the IdentityService.

If no specifics are given whether the given text string is a user or a group, the engine defaults to group. So the following two alternatives lead to the same result:

  1. <formalExpression>accountancy</formalExpression>
  2. <formalExpression>group(accountancy)</formalExpression>

User Assignment using Camunda Extensions

It is clear that user and group assignments are quite cumbersome for use cases where the assignment is more complicated. To avoid these complexities, custom extensions on the User Task are possible.

Assignee

The assignee attribute: this custom extension allows direct assignment of a User Task to a given user.

  1. <userTask id="theTask" name="my task" camunda:assignee="kermit" />

This is exactly the same as using a humanPerformer construct as defined above.

Candidate Users

The candidateUsers attribute: this custom extension allows you to make a user a candidate for a task.

  1. <userTask id="theTask" name="my task" camunda:candidateUsers="kermit, gonzo" />

This is exactly the same as using a potentialOwner construct as defined above. Note that it is not required to use the user(kermit) declaration as is the case with the potential owner construct, since this attribute can only be used for users.

Candidate Groups

The candidateGroups attribute: this custom extension allows you to make a group a candidate for a task.

  1. <userTask id="theTask" name="my task" camunda:candidateGroups="management, accountancy" />

This is exactly the same as using a potentialOwner construct as defined above. Note that it is not required to use the group(management) declaration as is the case with the potential owner construct, since this attribute can only be used for groups.

Combining Candidate Users and Groups

candidateUsers and candidateGroups can both be defined for the same User Task.

Assignment based on Data and Service Logic

In the above examples, constant values such as kermit or management are used. But what if the exact name of an assignee or a candidate group is not known at design time? And what if the assignee is not a constant value but depends on data such as “The person who started the process”? Maybe the assigment logic is also more complex and needs to access an external data source such as LDAP to implement a lookup such as “The manager of the employee who started the process”.

Such things can be implemented using assignment expressions or task listeners.

Assignment Expressions

Assignment expressions allow accessing process variables or calling out to beans and services.

Using Process Variables

Process variables are useful for assignments based on data which has been collected or calculated up front.

The following example shows how to assign a User Task to the person who started the process:

  1. <startEvent id="startEvent" camunda:initiator="starter" />
  2. ...
  3. <userTask id="task" name="Clarify Invoice" camunda:assignee="${ starter }"/>
  4. ...

First, the camunda:initiator extension is used to bind the user id of the person who started (“initiated”) the process to the variable starter. Then the expression ${ starter } retrieves that value and uses it as assignee for the task.

It is possible to use all process variables visible from the User Task in the expression.

Invoking a Service / Bean

When using Spring or CDI, it is possible to delegate to a bean or service implementation. This way it is possible to call out to complex assignment logic without modeling it as an explicit service task in the process which would then produce a variable used in the assignment.

In the following example, the assignee will be set by calling the findManagerOfEmployee() on the ldapService Spring/CDI bean. The emp parameter that is passed is a process variable.

  1. <userTask id="task" name="My Task" camunda:assignee="${ldapService.findManagerForEmployee(emp)}"/>

This also works in a similar way for candidate users and groups:

  1. <userTask id="task" name="My Task" camunda:candidateUsers="${ldapService.findAllSales()}"/>

Note that this will only work if the return type of the invoked methods is String or Collection (for candidate users and groups):

  1. public class FakeLdapService {
  2. public String findManagerForEmployee(String employee) {
  3. return "Kermit The Frog";
  4. }
  5. public List<String> findAllSales() {
  6. return Arrays.asList("kermit", "gonzo", "fozzie");
  7. }
  8. }

Assignments in Listeners

It is also possible to use task listeners for handling assignments. The following example demonstrates a task listener on the create event:

  1. <userTask id="task1" name="My task" >
  2. <extensionElements>
  3. <camunda:taskListener event="create" class="org.camunda.bpm.MyAssignmentHandler" />
  4. </extensionElements>
  5. </userTask>

The DelegateTask that is passed to the TaskListener implementation allows you to set the assignee and candidate-users/groups:

  1. public class MyAssignmentHandler implements TaskListener {
  2. public void notify(DelegateTask delegateTask) {
  3. // Execute custom identity lookups here
  4. // and then for example call following methods:
  5. delegateTask.setAssignee("kermit");
  6. delegateTask.addCandidateUser("fozzie");
  7. delegateTask.addCandidateGroup("management");
  8. ...
  9. }
  10. }

Assignments and Identity Service

Although the Camunda engine provides an identity management component, which is exposed through the IdentityService, it does not check whether a provided user is known by the identity component. This allows integration of the engine with existing identity management solutions when it is embedded into an application.

However, note that you can use the identity service in a service / bean or listener to query your user repository if this is useful to you.

Forms

It is possible to provide information to render a User Task form by using the camunda:formKeyattribute:

  1. <userTask id="someTask" camunda:formKey="someForm.html">
  2. ...
  3. </userTask>

The form key is a symbolic value which can be set in the BPMN XML file by using the extension attributeformKey and retrieved at runtime using the process engine API.

If the User Task form is displayed inside the Camunda Tasklist, the format of the formKey must followspecial rules. See the corresponding section in the user guide for details.

In custom applications, the value of the form key attribute can be interpreted freely. Based on the specific UI technology used,it can reference the name of an HTML file, a JSF / Facelets template, a Vaadin / GWT view, …

Retrieving the Form Key using the Form Service.

  1. String formKey = formService.getTaskFormData(someTaskId).getFormKey();

Retrieving the Form using the Task Service

When performing a task query, it is possible to retrieve the form key as well. This is most usefulif the form keys need to be retrieved for a complete list of tasks:

  1. List<Task> tasks = TaskService.createTaskQuery()
  2. .assignee("jonny")
  3. .initializeFormKeys() // must be invoked
  4. .list();
  5. for(Task task : tasks) {
  6. String formKey = task.getFormKey();
  7. }

Note that it is required to call the .initializeFormKeys() method on the TaskQuery object tomake sure the form keys are initialized.

Camunda Extensions

Attributes
camunda:assignee, camunda:asyncBefore, camunda:asyncAfter, camunda:candidateGroups, camunda:candidateUsers, camunda:dueDate, camunda:exclusive, camunda:formHandlerClass, camunda:formKey, camunda:jobPriority, camunda:priority
Extension Elements
——-
camunda:formData, camunda:formProperty, camunda:taskListener, camunda:failedJobRetryTimeCycle, camunda:inputOutput
Constraints
——-
The attribute camunda:assignee cannot be used simultaneously with the humanPerformer element
Only one camunda:formData extension element is allowed
The camunda:exclusive attribute is only evaluated if the attribute camunda:asyncBefore or camunda:asyncAfter is set to true

原文: https://docs.camunda.org/manual/7.9/reference/bpmn20/tasks/user-task/