Activiti User Guide -- Activit 用户指南 Part07

时间:2024-03-23 22:14:02

Now we can start a new process instance using the id we defined in the process definition (see first line of the XML). Note that this id in Activiti terminology is called the key.

现在我们就可以使用id来创建一个新的流程实例了(id值在XML定义的第一行中)。注意此处的idActiviti属于中称之为key

ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("financialReport");

 

This will create a runtime execution that will go first through the start event. After the start event, it follows all the outgoing sequence flow (only one in this case) and the first task ('write monthly financial report') is reached. The Activiti engine will now store a task in the persistent datastore. At this point, the user or group assignments attached to the task are resolved and also stored in the datastore.

这将创建一个流程执行,该执行将首先调用start event。之后将经由所有外出顺序流(这里只有一个)并到达第一个任务(“write monthly financial report”)Activiti引擎将把一个任务信息存储到持久化数据库中。这里分配给任务的人员或小组也会被解析,并也存储到数据库中。

 

After the task is created, the startProcessInstanceByKey will return since the user task activity is a so-called 'wait state', which means that the engine will continue the process instance only when some external trigger is given. In this case, the task is assigned to a group, which means that the every member of the group is a candidate to perform the task.

当任务创建后,startProcessInstanceByKey 也就返回了,因此user task activity也称之为’wait state’,也就是说引擎只有在接收到外部一个触发器之后才继续执行流程实例。在这里,任务赋给了一个小组,因此小组中的每一个成员都是执行任务可选人员。

Task lists

任务列表

We can now retrieve this newly created task through the taskService.

下面我们就可以taskService通过获取我们刚刚创建的任务。

List<Task> tasks = taskService.findUnassignedTasks("fozzie");
 . Note that the user we pass to this operation needs to be a member of the accountancy group, since that was declared in the process definition:

注意:执行该方法的人员必须是accountancy 小组中的一员,因为在流程定义中我们是这么定义的:

<potentialOwner>
  <resourceAssignmentExpression>
    <formalExpression>accountancy</formalExpression>
  </resourceAssignmentExpression>
</potentialOwner>
 We could also use the task query API to get the same results:

我们也可以通过使用任务查询 API 获取同样的结果:

List<Task> tasks = taskService.createTaskQuery().candidateUser("fozzie").list();
 or

List<Task> tasks = taskService.createTaskQuery().candidateGroup("accountancy").list();
 The business process described here is also deployed as an example to the demo setup database. After running the demo setup, log into the Activiti Explorer as fozzie (he's an accountant), and select in the drop down menu on the right the 'Monthly financial report' process.

这里所描述的业务流程已经部署在示例数据库中。运行完demo setup,你可以在Activiti Explorer中以fozzie(他属于accountan小组),并在右边的下拉菜单中选择‘Monthly financial report’流程。


Activiti User Guide -- Activit 用户指南 Part07
  

As explained, the process will execute up to the first user task. Since we're logged in as fozzie, we can see that there is a new candidate task available for him after we've started the process instance. Note that even if the process was started by someone else, the task would still be visible as a candidate task to everyone in the accountancy group.

正如我们上面解释的,流程将创建第一个用户任务。当我们启动流程实例后,使用fozzie登录,在界面中就可以看到一个新的待确认的任务。注意,即使该流程是别人启动的,该任务仍然是一个待确认任务,可以被accountancy小组的所有成员看到。


Activiti User Guide -- Activit 用户指南 Part07
 Claiming the task

确认任务

An accountant now needs to claim the task. By claiming the task, the specific user will become the assignee of the task and the task will disappear from every task list of the other members of the accountancy group. Claiming a task is programmatically done as follows:

现在需要accountant小组的成员确认任务。通过确认任务,任务将被赋给一个特定人员作为任务的处理者,同时任务将从accountancy小组的其他成员任务列表中消失。确认任务则是需要通过下面的程序进行:

taskService.claim(task.getId(), "fozzie");
 The task is now in the personal task list of the one that claimed the task.

此时任务就会加入确认者个人任务列表中。

List<Task> tasks = taskService.findAssignedTasks("fozzie");
 In the Activiti Explorer UI, clicking the claim button will call the same operation. The task will now move to the personal task list of the logged on user.

Activiti Explorer界面中,点击“claim”按钮也会执行相同的操作。任务就会移到登录者的个人任务列表中。


Activiti User Guide -- Activit 用户指南 Part07
 Completing the task

完成任务

The accountant can now start working on the financial report. Once the report is finished, he can complete the task, which means that all work for that task is done.

会计人员此时就可以开始编制财务报表。一但报表完成,就可以完成任务了,意味着本任务中的所有工作都做完了。

taskService.complete(task.getId());
 For the Activiti engine, this is an external signal that the process instance execution must be continued. The single outgoing transition out of the task is followed, bringing the execution in the second task ('verification of the report'). The same mechanism as described for the first task will now happen, with the small difference that the task will be assigned to the management group.

对于Activiti引擎,这是一外部触发器迫使流程实例继续执行。接下来只有一个外出流向,该流向将使流程执行第二个任务(“verification of the report”)。接下来所发生的和第一个任务的处理机制一样,只不过稍有不同的就是任务将赋给management 小组。

 

In the demo setup, completing the task is done by clicking the complete button in the task list. Since Fozzie isn't an accountant, we need to log out of the Activiti Explorer and login in as kermit (which is a manager). The second task is now visible in the unassigned task lists.

在示例中,可以通过点击“complete”按钮来完成任务。因为Fozzie不是manager小组的成员,我们需要登出并且使用kemit(他是manager小组的成员)登录。此时第二个任务就会显示在待确认的任务列表中。


Activiti User Guide -- Activit 用户指南 Part07
 Ending the process

结束流程

The verification task can be retrieved and claimed in exactly the same way as before. Completing this second task, will bring process execution at the end event, which finishes the process instance. The process instance and all related runtime execution data is removed from the datastore.

该任务可以像上面一样获取和确认。完成该任务将使流程到达end event,这将结束整个流程。此时流程实例以及相关所有的运行数据都会从数据库中移除。

 

When you log into Activiti Probe you can verify this, since no records will be found in the table where the process executions are stored.

你可以通过登录Acitivit Probe来验证,登录后你可以发现在存储流程执行数据的表中没有任何记录。


Activiti User Guide -- Activit 用户指南 Part07
 Future enhancements

后续改进

It's easy to see that this business process is too simple to be usable in reality. However, as you are going through the BPMN 2.0 constructs available in Activiti, you will be able to enhance the business process by

显然,对于实际应用来说这个流程是太简单了。不过,当你学习完Activiti中所提供BPMN2.0结构,就可以对业务流程进行以下改进:

  • defining a timer start event that automatically starts a process instance at the end of every month.
  • 定义一个timer start event在每个月定时启动一个流程实例
  • defining gateways that act as decisions. This way, a manager could reject the financial report which would recreate the task for the accountant.
  • 定义gateway用来进行判断。此时经理就可以驳回财务报表重新为会计人员创建一个任务
  • declaring and using variables, such that we can store or reference the report such that it can be visualized while verifying it.
  • 声明和使用variable,这样就可以存储相应的报表,在验证的时候就可以查看了
  • defining a service task at the end of the process that will send the report to every shareholder.
  • 在流程结束的时候定义一个service task,自动的将报表发送给每一个股东
  • etc.