Activiti工作流学习-----基于5.19.0版本(3)

前面关于eventType的属性值的配置简单的说了一下,activiti支持的值如下表所示:这是我摘抄的activiti官网的

Event 的名字 描述 Event的类名

ENGINE_CREATED


The process-engine this listener is attached to, has been created and is ready for API-calls.


org.activiti…?ActivitiEvent


ENGINE_CLOSED


The process-engine this listener is attached to, has been closed. API-calls to the engine are no longer possible.


org.activiti…?ActivitiEvent


ENTITY_CREATED


A new entity is created. The new entity is contained in the event.


org.activiti…?ActivitiEntityEvent


ENTITY_INITIALIZED


A new entity has been created and is fully initialized. If any children are created as part of the creation of an entity, this event will be fired AFTER the create/initialisation of the child entities as opposed to the ENTITY_CREATE event.


org.activiti…?ActivitiEntityEvent


ENTITY_UPDATED


An existing is updated. The updated entity is contained in the event.


org.activiti…?ActivitiEntityEvent


ENTITY_DELETED


An existing entity is deleted. The deleted entity is contained in the event.


org.activiti…?ActivitiEntityEvent


ENTITY_SUSPENDED


An existing entity is suspended. The suspended entity is contained in the event. Will be dispatched for ProcessDefinitions, ProcessInstances and Tasks.


org.activiti…?ActivitiEntityEvent


ENTITY_ACTIVATED


An existing entity is activated. The activated entity is contained in the event. Will be dispatched for ProcessDefinitions, ProcessInstances and Tasks.


org.activiti…?ActivitiEntityEvent


JOB_EXECUTION_SUCCESS


A job has been executed successfully. The event contains the job that was executed.


org.activiti…?ActivitiEntityEvent


JOB_EXECUTION_FAILURE


The execution of a job has failed. The event contains the job that was executed and the exception.


org.activiti…?ActivitiEntityEvent and org.activiti…?ActivitiExceptionEvent


JOB_RETRIES_DECREMENTED


The number of job retries have been decremented due to a failed job. The event contains the job that was updated.


org.activiti…?ActivitiEntityEvent


TIMER_FIRED


A timer has been fired. The event contains the job that was executed?


org.activiti…?ActivitiEntityEvent


JOB_CANCELED


A job has been canceled. The event contains the job that was canceled. Job can be canceled by API call, task was completed and associated boundary timer was canceled, on the new process definition deployment.


org.activiti…?ActivitiEntityEvent


ACTIVITY_STARTED


An activity is starting to execute


org.activiti…?ActivitiActivityEvent


ACTIVITY_COMPLETED


An activity is completed successfully


org.activiti…?ActivitiActivityEvent


ACTIVITY_CANCELLED


An activity is going to be cancelled. There can be three reasons for activity cancellation (MessageEventSubscriptionEntity, SignalEventSubscriptionEntity, TimerEntity).


org.activiti…?ActivitiActivityCancelledEvent


ACTIVITY_SIGNALED


An activity received a signal


org.activiti…?ActivitiSignalEvent


ACTIVITY_MESSAGE_RECEIVED


An activity received a message. Dispatched before the activity receives the message. When received, a ACTIVITY_SIGNAL or ACTIVITY_STARTED will be dispatched for this activity, depending on the type (boundary-event or event-subprocess start-event)


org.activiti…?ActivitiMessageEvent


ACTIVITY_ERROR_RECEIVED


An activity has received an error event. Dispatched before the actual error has been handled by the activity. The event’s activityId contains a reference to the error-handling activity. This event will be either followed by a ACTIVITY_SIGNALLED event or ACTIVITY_COMPLETE for the involved activity, if the error was delivered successfully.


org.activiti…?ActivitiErrorEvent


UNCAUGHT_BPMN_ERROR


An uncaught BPMN error has been thrown. The process did not have any handlers for that specific error. The event’s activityId will be empty.


org.activiti…?ActivitiErrorEvent


ACTIVITY_COMPENSATE


An activity is about to be compensated. The event contains the id of the activity that is will be executed for compensation.


org.activiti…?ActivitiActivityEvent


VARIABLE_CREATED


A variable has been created. The event contains the variable name, value and related execution and task (if any).


org.activiti…?ActivitiVariableEvent


VARIABLE_UPDATED


An existing variable has been updated. The event contains the variable name, updated value and related execution and task (if any).


org.activiti…?ActivitiVariableEvent


VARIABLE_DELETED


An existing variable has been deleted. The event contains the variable name, last known value and related execution and task (if any).


org.activiti…?ActivitiVariableEvent


TASK_ASSIGNED


A task has been assigned to a user. The event contains the task


org.activiti…?ActivitiEntityEvent


TASK_CREATED


A task has been created. This is dispatched after the ENTITY_CREATE event. In case the task is part of a process, this event will be fired before the task listeners are executed.


org.activiti…?ActivitiEntityEvent


TASK_COMPLETED


A task has been completed. This is dispatched before the ENTITY_DELETE event. In case the task is part of a process, this event will be fired before the process has moved on and will be followed by a ACTIVITY_COMPLETE event, targeting the activity that represents the completed task.


org.activiti…?ActivitiEntityEvent


PROCESS_COMPLETED


A process has been completed. Dispatched after the last activity ACTIVITY_COMPLETED event. Process is completed when it reaches state in which process instance does not have any transition to take.


org.activiti…?ActivitiEntityEvent


PROCESS_CANCELLED


A process has been cancelled. Dispatched before the process instance is deleted from runtime. Process instance is cancelled by API call RuntimeService.deleteProcessInstance


org.activiti…?ActivitiCancelledEvent


MEMBERSHIP_CREATED


A user has been added to a group. The event contains the ids of the user and group involved.


org.activiti…?ActivitiMembershipEvent


MEMBERSHIP_DELETED


A user has been removed from a group. The event contains the ids of the user and group involved.


org.activiti…?ActivitiMembershipEvent


MEMBERSHIPS_DELETED


All members will be removed from a group. The event is thrown before the members are removed, so they are still accessible. No individual MEMBERSHIP_DELETED events will be thrown if all members are deleted at once, for performance reasons.


org.activiti…?ActivitiMembershipEvent

简单的说明:

所有的ENTITY_\*事件关联流程引擎的entity-event事件,具体的entity-event监听的范围有这些:

  • ENTITY_CREATED, ENTITY_INITIALIZED, ENTITY_DELETED: Attachment, Comment, Deployment, Execution, Group, IdentityLink, Job, Model, ProcessDefinition, ProcessInstance, Task, User.
  • ENTITY_UPDATED: Attachment, Deployment, Execution, Group, IdentityLink, Job, Model, ProcessDefinition, ProcessInstance, Task, User.
  • ENTITY_SUSPENDED, ENTITY_ACTIVATED: ProcessDefinition, ProcessInstance/Execution, Task.

补充说明:

事件监听器仅仅会监听流程引擎里面的事件发生,所以如果你有其他的引擎在运行同一个数据库里面,仅仅是在同一个流程引擎中注册的事件监听器会被调用,在其他流程引擎中注册的,即使在同一个JVM运行也不会生效。即使是这样,你需要谨慎和更新/操作实体参与派遣的事件。

三、工作流核心--流程引擎

Activiti的流程引擎穿插在整个工作流中,从ProcessEngine中你可以获取不同的service, ProcessEngine和Service对象都是线程安全的,所以在整个服务器中你只需要存在这些单例对象就可以了。

ProcessEngine和Service对象的创建方式如下:

ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
RepositoryService repositoryService = processEngine.getRepositoryService();
TaskService taskService = processEngine.getTaskService();
ManagementService managementService = processEngine.getManagementService();
IdentityService identityService = processEngine.getIdentityService();
HistoryService historyService = processEngine.getHistoryService();
FormService formService = processEngine.getFormService();

ProcessEngines.getDefaultProcessEngine(),合理的关闭和初始化所有的ProcessEngine的方法是:ProcessEngines.destroy()和ProcessEngines.init() 。ProcessEngines对象将会扫描所有不同目录的activiti.cfg.xmlactiviti-context.xml文件,对于所有的activiti.cfg.xml文件,内部都会调用Activiti底层的方法,ProcessEngineConfiguration.createProcessEngineConfigurationFromInputStream(inputStream).buildProcessEngine(),对于所有的activiti-context.xml,ProcessEngine将会被以Spring的方式创建,在Spring的上下文创建后,ProcessEngine将会被加进Spring的上下文中。

所有的Service是无状态的,这意味这你可以在具有多个节点的集群上面,每个相同的数据库上面进行调用这些Service,而不用关心是那一台机器调用的,任何服务的调用都是幂等的,无论它何处运行。

注:幂等:是一个数学与计算机学概念,常见于抽象代数中。 在编程中.一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同。

一般的,在Activiti的开发中都会先去获取RepositoryService,RepositoryService提供了管理和操作deployments和process definitions,这里就不用细说更多细节的东西了,一个流程定义的java对象其实就是BPMN 2.0文件的副本,流程定义对象也表现了每一步流程步骤的结构和行为。deployment是在工作流引擎里面打包的单位,也就是说它囊括了单个的bpmn文件到流程的文件夹和相关资源,在一个deployment中应该包含多少配置文件取决于开发者。RepositoryService允许发布文件夹,发布一个deployment意味着将会上传到引擎中,在存进数据库之前需要检查和解析,从那时起,deployment中所有的流程就可以启动了.

RepositoryService也提供了一些功能, 比如:

  • 提供在流程引擎中流程定义和deployments的查询。
  • 阻塞或者激活deployment中全部或者部分流程定义。阻塞的意思是指以后这些流程定义将不会执行,直到被激活为止。
  • 获取各种资源文件,例如在deployment中的file,流程图等。
  • 获取流程定义对象的版本信息。

RepositoryService保存了静态信息,而RuntimeService则相反, RuntimeService处理流程定义实例。综上所述,process definition对象是描述流程的结构和具体步骤的表现,而流程实例对象是这个流程定义对象的执行对象。同一时间每一个流程定义对象对应了多个流程实例在运行,RuntimeService同样可以获取存储process variables(流程变量),在流程实例中获取流程变量,在流程中作用于各种execution(比如排他网关通常使用流程变量来判断流程路线的走向),Runtimeservice也允许查询流程实例和执行实例,Executions是取用BPMN 2.0的概念,Execution(也就是执行实例)可以说一个指针指出当前的流程实例的位置。RuntimeService让流程实例的深入执行,一个流程实例有几种wait states,并且RunTimeService提供的不同的方式发信号(外部的触发器触发)给流程实例,流程实例向下执行。

Tasks(任务)在Activiti中需要被实际的人员去完成,关于所有的Task的事情都交给了TaskService处理,例如:

  • 查询任务被分配的组或人员
  • 额外创建任务,这些任务是和流程实例没有关系的。
  • 任务的分配和相关人员的参与
  • 领取任务和完成任务,领取任务就是说当前任务被人获得,并且该任务将由他来完成。完成任务就是执行任务工作,常见的就是填写表单。

IdentityService是很简单的,它就是对users和group的增删改查,重要提示:activiti在运行的时候并不会检测用户,比如现在一个任务可以分配给任何人做,但activiti并不会去检查这个人是否存在于系统里,要知道Activiti是被用来提供LDAP,Active Directory等服务的。

FormService是一个可选用的Service,意味着activiti没有它不会受到影响,不会因此少了功能。这个Service用来提供start form和task form的概念。start form指的是流程实例开始之前展示用户的表单,而task form在用户需要完成任务是展示的表单。Activiti允许用户将这些表单插入流程定义中,但是没必要内嵌进入activiti。

HistoryService提供所有关于工作流引擎的历史数据,在流程执行的时候,一大笔数据会被引擎保存起来,例如流程实例开始的时候,记录了谁在做任务,做了多久,流程实例的走向等等,HistoryService提供查询方法支持这些数据的获取。

ManagementService也是一个在工作流中可选的service,它允许从数据库表和元数据中获取数据,他也提供了对作业(job)的查询和管理,Job被用来处理这些事情:定时器,异步延迟等。

3.1 异常的处理策略

activiti里面最常用的是org.activiti.engine.ActivitiException,他是一个运行时异常,在整个工作流API中都可以抛出,但最好还是根据api在相应的地方使用对应提供的异常类。比如:在RuntimeService中的一个方法

/**
 * Called when the task is successfully executed.
 * @param taskId the id of the task to complete, cannot be null.
 * @throws ActivitiObjectNotFoundException when no task exists with the given id.
 */
 void complete(String taskId);

明确的说明的id不能为空,否则抛出ActivitiIllegalArgumentException.

尽管我们想避免抛出很宽泛的异常,但是在activiti的运行执行当中,仍然有超出我们预料的异常出现,不符合下面异常的都会抛出ActivitiException:

ActivitiWrongDbException:在工作流引擎发现数据库中工作流版本和引擎本身的版本不一致的时候抛出。

ActivitiOptimisticLockingException:在并发读写相同数据,乐观锁发生版本不一致抛出异常。

ActivitiClassLoadingException:类加载的时候没有找到或者加载它的时候发生错误会抛出异常。

ActivitiObjectNotFoundException:请求对象或者动作不存在的时候抛出。

ActivitiIllegalArgumentException:在调用含参API的时候,参数值非法。

ActivitiTaskAlreadyClaimedException:在任务已经被认领的时候。

时间: 2024-10-12 12:37:21

Activiti工作流学习-----基于5.19.0版本(3)的相关文章

Activiti工作流学习-----基于5.19.0版本(5)

五.与Spring集成 实际项目中一般都有Spring的身影,与Spring集成使得Activiti的实用性得到提高.activiti和Spring整合需要activiti-spring的jar在类路径下面,整合类是:org.activiti.spring.ProcessEngineFactoryBean,它将加载配置对象和产生流程引擎对象.例如下面代码: <bean id="processEngineConfiguration" class="org.activiti

Activiti工作流学习-----基于5.19.0版本(8)

8.1.5 Start Event 继续上一篇的事件的分享笔记,Start Event指明该处是流程开始,至于开始事件的类型(消息到达开始,指定的事件循环开始等),定义如何开始是在开始事件圆圈图标里面的小图标表示,具体反映到xml中就是子元素的不同. Start Event总是进行捕获:在任何时候等待相应的触发器触发. 下面展示start event的xml,其中initiator指的是流程发起人,流程开始后他将会被保存起来: <startEvent id="request" a

Activiti工作流学习-----基于5.19.0版本(6)

七. BPMN的简介 读者了解到这里,应付一般的工作流开发已经足够了.此处应该有华丽的分割线,在工作流项目中核心开发人员主要是对工作流业务设计以及实现,而初级开发人员是对业务功能的代码实现.以后将主要对流程图设计的概念和具体设计进行描述,如果你感兴趣可以继续了解. 7.1 什么是BPMN? BPMN的全称是Business Process Model and Notation,它是一套BPM的规范,从2004年起,BPMN规范的推动有OMG组织维护,开始推出的BPMN1.0主要是对设计图中图形的

Activiti工作流学习-----基于5.19.0版本(2)

二.activiti.cfg.xml的其他bean节点配置 2.1 新特性:Job Executor和Async Executor 从5.17.0版本的activiti开始提供作业执行者(Job Executor)和异步作业执行者(Async Executor),Async Executor执行表现更好,并且执行异步作业对数据库更加友善.activiti官方推荐使用Async Executor,并且一些老的Job Executor依旧有效.在Java EE 7运行环境中,JSR-236规范支持容

Activiti工作流学习-----基于5.19.0版本(1)

该版本的Activiti运行须知: 1.JDK 6+,Eclipse最好是Kepler以上版本. 2.试验功能都有EXPERIMENTAL标注,被标注的部分不应该视为稳定的. 有兴趣的同学可以去了解下Activiti Explorer项目,他涵盖了大部分Activiti的功能,还没有Activiti概念的同学可以看看了解一下. 一.工作流开发之配置 Activiti沿用具有Spring配置文件风格的配置,工作流默认是加载名叫activiti.cfg.xml的文件,配置文件大体: <beans x

Activiti工作流学习(三)Activiti工作流与spring集成

一.前言 前面Activiti工作流的学习,说明了Activiti的基本应用,在我们开发中可以根据实际的业务参考Activiti的API去更好的理解以及巩固.我们实际的开发中我们基本上都使用spring框架进行开发,现在来说明一下Activiti工作流与spring集成,Activiti工作流与spring集成还算比较简单,可以参考Activiti的API来进行整合. 二.Activiti常用的表 ---------------------------------------------部署对象

Activiti工作流学习(二)流程实例、执行对象、任务

一.前言 前面说明了基本的流程部署.定义,启动流程实例等基本操作,下面我们继续来学习流程实例.执行对象.任务. 二.流程实例.执行对象说明 整个Activiti的生命周期经过了如下的几个步骤: 1.流程部署 --->  2.启动流程实例 --- > 3.执行流程对象(一个流程实例包含多执行对象) ---> 4.完成整个流程 说明: 三.例子演示   1.完成我的任务     2.查询流程状态(判断流程正在执行,还是结束) 3.查询历史任务  4.流程实例.执行对象.任务  四.对应数据库

Activiti工作流学习一安装和下载

1.下载和安装 1) 下载activiti相关jar包和文件 官方下载地址:http://www.activiti.org/download.html 2) Activiti-eclipse插件下载 1,手动下载==>官方下载地址:http://activiti.org/designer/update 2,自动下载==>运行eclipse,安装Activiti BPMN 2.0 designer插件,具体参见:http://www.ecmkit.com/zh-hans/2012/03/21/a

activiti工作流学习链接

首页: http://www.activiti.org/书籍: activiti in action  入门demo: kft-activiti-demo   http://www.oschina.net/p/kft-activiti-demo/论坛:http://www.activiti-cn.org/