Activiti5 开发介绍1

原文来自:http://blog.csdn.net/m13321169565/article/details/7528280#comments

Activiti5是一款基于java的轻量级工作流引擎。支持BPMN2.0,其依赖的jar非常少,只依赖mybatis。上手迅速,功能丰富,对于一般的工作流应用都可以满足,废话少说,进入正题。

 

一、搭建开发环境

1、安装jdk5+

2、安装eclipse

3、安装eclipse BPMN designer plugin,地址:http://activiti.org/designer/update/

4、下载Activiti5,地址:http://activiti.org/download.html

5、建立项目,添加jar包:

activation-1.1.jar

activiti-engine-5.9.jar

activiti-spring-5.9-sources.jar

activiti-spring-5.9.jar

aopalliance-1.0.jar

commons-dbcp-1.4.jar

commons-email-1.2.jar

commons-lang-2.4.jar

commons-logging-1.1.1.jar

commons-pool-1.3.jar

livetribe-jsr223-2.0.6.jar

mail-1.4.1.jar

mybatis-3.0.6.jar

spring-aop-3.0.3.RELEASE.jar

spring-asm-3.0.3.RELEASE.jar

spring-beans-3.0.3.RELEASE.jar

spring-context-3.0.3.RELEASE.jar

spring-core-3.0.3.RELEASE.jar

spring-expression-3.0.3.RELEASE.jar

spring-jdbc-3.0.3.RELEASE.jar

spring-orm-3.0.3.RELEASE.jar

spring-test-3.0.3.RELEASE.jar

spring-tx-3.0.3.RELEASE.jar

 6、安装数据库,可以选择以下一种,

Activiti database type Versions tested Example JDBC URL Notes
h2 1.2.132 jdbc:h2:tcp://localhost/activiti Default configured database
mysql 5.1.11 jdbc:mysql://localhost:3306/activiti?autoReconnect=true Tested using mysql-connetor-java database driver
oracle 10.2.0 jdbc:oracle:thin:@localhost:1521:xe  
postgres 8.4 jdbc:postgresql://localhost:5432/activiti  
db2 DB2 9.7 using db2jcc4 jdbc:db2://localhost:50000/activiti [EXPERIMENTAL]
mssql 2008 using JDBC jtds-1.2.4 jdbc:jtds:sqlserver://localhost:1433/activiti [EXPERIMENTAL]

7、加入相应的数据库驱动jar包,以及数据源支持。


OK,到这里,开发环境已经搭建完成,下面进入开发阶段。


二、开发概述使用Activiti5和Spring集成。

1、编写配置文件,activiti-conf.xml
<span class="tag" style="color: rgb(0, 0, 136);"><beans</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">xmlns</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"http://www.springframework.org/schema/beans"</span><span class="pln" style="color: rgb(0, 0, 0);"> 
       </span><span class="atn" style="color: rgb(102, 0, 102);">xmlns:context</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"http://www.springframework.org/schema/context"</span><span class="pln" style="color: rgb(0, 0, 0);">
       </span><span class="atn" style="color: rgb(102, 0, 102);">xmlns:tx</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"http://www.springframework.org/schema/tx"</span><span class="pln" style="color: rgb(0, 0, 0);">
       </span><span class="atn" style="color: rgb(102, 0, 102);">xmlns:xsi</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"http://www.w3.org/2001/XMLSchema-instance"</span><span class="pln" style="color: rgb(0, 0, 0);">
       </span><span class="atn" style="color: rgb(102, 0, 102);">xsi:schemaLocation</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"</span><span class="tag" style="color: rgb(0, 0, 136);">></span><span class="pln" style="color: rgb(0, 0, 0);">

  </span><span class="tag" style="color: rgb(0, 0, 136);"><bean</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">id</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"dataSource"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">class</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"org.springframework.jdbc.datasource.SimpleDriverDataSource"</span><span class="tag" style="color: rgb(0, 0, 136);">></span><span class="pln" style="color: rgb(0, 0, 0);">
    </span><span class="tag" style="color: rgb(0, 0, 136);"><property</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"driverClass"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">value</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"org.h2.Driver"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span><span class="pln" style="color: rgb(0, 0, 0);">
    </span><span class="tag" style="color: rgb(0, 0, 136);"><property</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"url"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">value</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span><span class="pln" style="color: rgb(0, 0, 0);">
    </span><span class="tag" style="color: rgb(0, 0, 136);"><property</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"username"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">value</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"sa"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span><span class="pln" style="color: rgb(0, 0, 0);">
    </span><span class="tag" style="color: rgb(0, 0, 136);"><property</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"password"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">value</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"123456"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span><span class="pln" style="color: rgb(0, 0, 0);">
  </span><span class="tag" style="color: rgb(0, 0, 136);"></bean></span><span class="pln" style="color: rgb(0, 0, 0);">

  </span><span class="tag" style="color: rgb(0, 0, 136);"><bean</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">id</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"transactionManager"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">class</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"org.springframework.jdbc.datasource.DataSourceTransactionManager"</span><span class="tag" style="color: rgb(0, 0, 136);">></span><span class="pln" style="color: rgb(0, 0, 0);">
    </span><span class="tag" style="color: rgb(0, 0, 136);"><property</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"dataSource"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">ref</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"dataSource"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span><span class="pln" style="color: rgb(0, 0, 0);">
  </span><span class="tag" style="color: rgb(0, 0, 136);"></bean></span><span class="pln" style="color: rgb(0, 0, 0);">
  
  </span><span class="tag" style="color: rgb(0, 0, 136);"><bean</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">id</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"processEngineConfiguration"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">class</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"org.activiti.spring.SpringProcessEngineConfiguration"</span><span class="tag" style="color: rgb(0, 0, 136);">></span><span class="pln" style="color: rgb(0, 0, 0);">
    </span><span class="tag" style="color: rgb(0, 0, 136);"><property</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"dataSource"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">ref</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"dataSource"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span><span class="pln" style="color: rgb(0, 0, 0);">
    </span><span class="tag" style="color: rgb(0, 0, 136);"><property</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"transactionManager"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">ref</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"transactionManager"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span><span class="pln" style="color: rgb(0, 0, 0);">
    </span><span class="tag" style="color: rgb(0, 0, 136);"><property</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"databaseSchemaUpdate"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">value</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"true"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);">/>  <!-- 是否更新数据库结构,如果表不存在,启动时会自动创建 --></span><span class="pln" style="color: rgb(0, 0, 0);">
    </span><span class="tag" style="color: rgb(0, 0, 136);"><property</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"jobExecutorActivate"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">value</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"false"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);">/>  
</span><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="tag" style="color: rgb(0, 0, 136);"></bean></span><span class="pln" style="color: rgb(0, 0, 0);">
  
  </span><span class="tag" style="color: rgb(0, 0, 136);"><bean</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">id</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"processEngine"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">class</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"org.activiti.spring.ProcessEngineFactoryBean"</span><span class="tag" style="color: rgb(0, 0, 136);">></span><span class="pln" style="color: rgb(0, 0, 0);">
    </span><span class="tag" style="color: rgb(0, 0, 136);"><property</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"processEngineConfiguration"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">ref</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"processEngineConfiguration"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span><span class="pln" style="color: rgb(0, 0, 0);">
  </span><span class="tag" style="color: rgb(0, 0, 136);"></bean></span><span class="pln" style="color: rgb(0, 0, 0);">

</span>

2、使用BPMN设计器,编写"demo.bpmn20.xml"流程定义文件

3、运行代码:


[java]  view plain  copy
  1. public static void main(String args[]) {  
  2.     ApplicationContext ctx = new ClassPathXmlApplicationContext("activiti-conf.xml");    //初始化spring上下文  
  3.     ProcessEngine processEngine = (ProcessEngine) ctx.getBean("processEngine");         //获得Activiti主接口  
  4.     System.out.println("processEngine name :" + processEngine.getName());  
  5.     RepositoryService repositoryService = processEngine.getRepositoryService();         //获得资源操作接口  
  6.     RuntimeService runtimeService = (RuntimeService) processEngine.getRuntimeService(); //获得运行时操作接口(大多操作流程实例)  
  7.     TaskService taskService = processEngine.getTaskService();                           //获得任务操作接口  
  8.     IdentityService identityService = processEngine.getIdentityService();               //获得身份操作接口(用户和用户组)  
  9.     HistoryService historyService = processEngine.getHistoryService();          //获得历史查询接口  
  10.     FormService formService = processEngine.getFormService();                   //获得表单数据绑定接口  
  11.     ManagementService managementService = processEngine.getManagementService(); //获得管理接口  
  12.       
  13.     Deployment deployment = repositoryService.createDeployment().addClasspathResource("demo.bpmn20.xml").deploy();//部署流程定义  
  14.     System.out.print("发布流程定义成功!" + "  Id=" + deployment.getId() + ", Name=" + deployment.getName());  
  15. }  

如果执行成功,说明Activiti已经于spring成功集成。

同时,数据库中会产生相应的表。


4、表说明

RE:RepositoryService接口所操作的表
ACT_RE_DEPLOYMENT
部署流程定义时需要被持久化保存下来的信息
ACT_RE_PROCDEF  流程定义数据表

ID:IdentityService接口所操作的表
ACT_ID_INFO 用户扩展信息表
ACT_ID_MEMBERSHIP 用户与分组对应信息表
ACT_ID_GROUP 用户组信息 
ACT_ID_USER 用户信息

RU:运行时表 - RuntimeService
ACT_RU_VARIABLE 运行时流程变量表 
ACT_RU_TASK 任务节点表 
ACT_RU_JOB 定时任务表 
ACT_RU_IDENTITYLINK 任务参与者表。主要存储任务节点与参与者的相关信息
ACT_RU_EXECUTION 流程执行路径信息

HI:历史数据表,HistoryService
ACT_HI_PROCINST 历史流程实例表
ACT_HI_ATTACHMENT 历史附件表 
ACT_HI_COMMENT 历史注释表 
ACT_HI_DETAIL 历史详情表 
ACT_HI_TASKINST 历史任务实例表
ACT_HI_ACTINST 历史执行路径信息 

GE:全局数据
ACT_GE_BYTEARRAY 二进制数据表:流程定义图片和xml、Serializable的变量
ACT_GE_PROPERTY 属性数据表。存储整个流程引擎级别的数据


注:由于Activiti会在任务或者流程结束时,删除其所在的运行时数据,存入历史数据表。所以保证了运行时表小且快。不会有性能问题。


5、涉及到的名词,对应类,解释

流程定义:ProcessDefinition类,也就是我们用设计器生成的xml文件,Activiti将根据流程定义产生流程实例。

流程实例:ProcessInstance类,是一个完整的流程表示。

流程执行路径:Execution类, 表示流程的执行实例。

任务节点:Task类,有很多这里不一一介绍

参与者:User,参与人


注:如果同一个流程定义被发布多次,那么Activiti将默认采用最新版本。


注: Activiti中关于 流程实例 和 流程执行实例 的区别

ProcessInstance是流程实例,Execution是流程的一个执行实例。一个流程在并发分支的情况下,会产生多个执行实例,一个Execution也可以分出多个Execution。

又有:ProcessInstance extends Execution,说明ProcessInstance也是一个Execution,

Execution的 Parent Execution可能是一个ProcessInstance,所以,在流程没有并发分支的情况下ProcessInstance与Execution是相同的。

三、接口介绍

RepositoryService :流程定义发布、查询、管理相关操作的接口

//查询所有已发布信息
List<Deployment> ds = repositoryService.createDeploymentQuery().list(); 

// 查询所有最新版本的流程定义
List<ProcessDefinition> pds = repositoryService.createProcessDefinitionQuery().latestVersion().list();  

//发布流程定义
DeploymentBuilder builder = repositoryService.createDeployment();
builder = builder.addClasspathResource(definitionResource);
Deployment deployment = builder.deploy();

未完,待续-----

你可能感兴趣的:(Activiti5 开发介绍1)