Activiti User Guide -- Activit 用户指南 Part07

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’流程。



  

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小组的所有成员看到。



 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”按钮也会执行相同的操作。任务就会移到登录者的个人任务列表中。



 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小组的成员)登录。此时第二个任务就会显示在待确认的任务列表中。



 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来验证,登录后你可以发现在存储流程执行数据的表中没有任何记录。



 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,Activity是Android系统中的四大组件之一,可以用于显示View。Activity是一个与用记交互的系统模块,几乎所有的Activity都是和用户进行交互的,但是如果这样就能说Activity主要是用来显示View就不太正确了。 在深入了解Activity之前,我们先要知道一下MVC设计模式,在JAVAEE 中MVC设计模式已经很经典了,而且分的也比较清晰了,但是在Android中,好多人对MVC在Android开发中的应用不是很清楚,下面我就先来介绍一下MVC在Android开发中的应用: M(Model 模型):Model是应用程序的主体部分,所有的业务逻辑都应该写在这里,在Android中Model层与JavaEE中的变化不大,如:对数据库的操作,对网络等的操作都放在该层(但不是说它们都放在同一个包中,可以分开放,但它们统称为Model层)。 V(View 视图):是应用程序中负责生成用户界面的部分,也是在整个MVC架构中用户唯一可以看到的一层,接收用户输入,显示处理结果;在Android应用中一般采用XML文件里德界面的描述,使用的时候可以非常方便的引入,当然也可以使用JavaScript+Html等方式作为View。 C(Controller控制层)android的控制层的重任就要落在众多的activity的肩上了,所以在这里就要建议大家不要在activity中写太多的代码,尽量能过activity交割Model业务逻辑层处理。 好了,在介绍过Android应用开发中的MVC架构后,我们就可以很明确的知道,在Android中Activity主要是用来做控制的,它可以选择要显示的View,也可以从View中获取数据然后把数据传给Model层进行处理,最后再来显示出处理结果。 介绍过Activity的主要作用后,那么我们就要详细说一下Activity了。 Activity生命周期图 Activity 的生命周期是被以下的函数控制的。 public class Activity extends ApplicationContext { protected void onCreate(Bundle icicle); protected void onStart(); protected void onRestart(); protected void onResume(); protected void onFreeze(Bundle outIcicle); protected void onPause(); protected void onStop(); protected void onDestroy(); } onCreate(Bundle) 函数是你进行初始化的地方,这个也是执行 setContentView(View)函数的地方,setContentView(View)函数可以传入一个由XML 编制的UI界面,可以使UI和具体实现完全分离。 onPause()函数是处理用户离开当前 Activity 的地方。更重要的是,任何在当前 Activity中的任何改变都要在这个函数中提交。 Activity有四种状态: 活动状态,当Activity处于Stack(栈)顶时,就是手机当前的现实屏幕,这是 Activity就 处于activity或者运行状态。 运行但是失去焦点,当Activity还处于运行状态时,但是屏幕是有另外一个Activity 处于文档处于焦点状态,当前的Activity就处于pause。 停止,当Activity被另一个Activity完全覆盖的时候,就被停止了,其实就是虽然在 运行,但是用户却看不见。 结束,当Activity处于pause或者stop时,系统可以结束 Activity,回收资源,这 是Activity就是处于结束状态了。 处于结束状态的是Activity,如果要使用户可见,只要重启才可以。 Activity的响应时间 当前Activity所在的线程为主线程,它的响应时间为5秒,如果在当前运行的Activity中进行耗时的操作且响应时间起过5秒,那么程序就会报ANR错误。所以,这也是不建议在Activity中写太多复杂代码的原因之一。 当然,有些代码只能写在Activity中,不然就运行不了(它们不是生命周期方法),比如你想要获得android系统或者硬件一的些信息,就必须在Activity中写出来,如果单独写一个工具类获得不了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值