Activiti入门列子一个简单的Activiti请假流程

整个项目的目录结构:


pom.xml:

[html] view plain copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0modelVersion>  
  4.   
  5.     <groupId>cn.ztogroupId>  
  6.     <artifactId>myActivitiartifactId>  
  7.     <version>0.0.1-SNAPSHOTversion>  
  8.     <packaging>jarpackaging>  
  9.   
  10.     <name>myActivitiname>  
  11.     <url>http://maven.apache.orgurl>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  15.     properties>  
  16.   
  17.     <dependencies>  
  18.           
  19.         <dependency>  
  20.             <groupId>org.activitigroupId>  
  21.             <artifactId>activiti-engineartifactId>  
  22.             <version>5.15.1version>  
  23.         dependency>  
  24.         <dependency>  
  25.             <groupId>org.activitigroupId>  
  26.             <artifactId>activiti-springartifactId>  
  27.             <version>5.15.1version>  
  28.         dependency>  
  29.         <dependency>  
  30.             <groupId>org.activitigroupId>  
  31.             <artifactId>activiti-explorerartifactId>  
  32.             <version>5.15.1version>  
  33.         dependency>  
  34.           
  35.         <dependency>  
  36.             <groupId>commons-dbcpgroupId>  
  37.             <artifactId>commons-dbcpartifactId>  
  38.             <version>1.4version>  
  39.         dependency>  
  40.         <dependency>  
  41.             <groupId>mysqlgroupId>  
  42.             <artifactId>mysql-connector-javaartifactId>  
  43.             <version>5.1.29version>  
  44.         dependency>  
  45.           
  46.         <dependency>  
  47.             <groupId>org.springframeworkgroupId>  
  48.             <artifactId>spring-coreartifactId>  
  49.             <version>4.0.6.RELEASEversion>  
  50.         dependency>  
  51.         <dependency>  
  52.             <groupId>org.springframeworkgroupId>  
  53.             <artifactId>spring-beansartifactId>  
  54.             <version>4.0.6.RELEASEversion>  
  55.         dependency>  
  56.         <dependency>  
  57.             <groupId>org.springframeworkgroupId>  
  58.             <artifactId>spring-testartifactId>  
  59.             <version>4.0.6.RELEASEversion>  
  60.         dependency>  
  61.         <dependency>  
  62.             <groupId>org.springframeworkgroupId>  
  63.             <artifactId>spring-contextartifactId>  
  64.             <version>4.0.6.RELEASEversion>  
  65.         dependency>  
  66.         <dependency>  
  67.             <groupId>org.springframeworkgroupId>  
  68.             <artifactId>spring-context-supportartifactId>  
  69.             <version>4.0.6.RELEASEversion>  
  70.         dependency>  
  71.         <dependency>  
  72.             <groupId>org.springframeworkgroupId>  
  73.             <artifactId>spring-txartifactId>  
  74.             <version>4.0.6.RELEASEversion>  
  75.         dependency>  
  76.         <dependency>  
  77.             <groupId>org.springframeworkgroupId>  
  78.             <artifactId>spring-ormartifactId>  
  79.             <version>4.0.6.RELEASEversion>  
  80.         dependency>  
  81.           
  82.         <dependency>  
  83.             <groupId>org.mybatisgroupId>  
  84.             <artifactId>mybatisartifactId>  
  85.             <version>3.2.5version>  
  86.         dependency>  
  87.         <dependency>  
  88.             <groupId>org.mybatisgroupId>  
  89.             <artifactId>mybatis-springartifactId>  
  90.             <version>1.2.2version>  
  91.         dependency>  
  92.           
  93.         <dependency>  
  94.             <groupId>junitgroupId>  
  95.             <artifactId>junitartifactId>  
  96.             <version>4.11version>  
  97.         dependency>  
  98.           
  99.         <dependency>  
  100.             <groupId>log4jgroupId>  
  101.             <artifactId>log4jartifactId>  
  102.             <version>1.2.4version>  
  103.         dependency>  
  104.         <dependency>  
  105.             <groupId>org.slf4jgroupId>  
  106.             <artifactId>slf4j-apiartifactId>  
  107.             <version>1.7.6version>  
  108.         dependency>  
  109.         <dependency>  
  110.             <groupId>org.slf4jgroupId>  
  111.             <artifactId>jcl-over-slf4jartifactId>  
  112.             <version>1.7.6version>  
  113.         dependency>  
  114.         <dependency>  
  115.             <groupId>org.slf4jgroupId>  
  116.             <artifactId>slf4j-log4j12artifactId>  
  117.             <version>1.7.6version>  
  118.         dependency>  
  119.           
  120.         <dependency>  
  121.             <groupId>org.apache.commonsgroupId>  
  122.             <artifactId>commons-lang3artifactId>  
  123.             <version>3.1version>  
  124.         dependency>  
  125.         <dependency>  
  126.             <groupId>commons-iogroupId>  
  127.             <artifactId>commons-ioartifactId>  
  128.             <version>2.4version>  
  129.         dependency>  
  130.         <dependency>  
  131.             <groupId>commons-fileuploadgroupId>  
  132.             <artifactId>commons-fileuploadartifactId>  
  133.             <version>1.2.2version>  
  134.         dependency>  
  135.           
  136.         <dependency>  
  137.             <groupId>javax.annotationgroupId>  
  138.             <artifactId>javax.annotation-apiartifactId>  
  139.             <version>1.2version>  
  140.         dependency>  
  141.         <dependency>  
  142.             <groupId>joda-timegroupId>  
  143.             <artifactId>joda-timeartifactId>  
  144.             <version>2.1version>  
  145.         dependency>  
  146.     dependencies>  
  147. project>  


activiti.cfg.xml:

[html] view plain copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">  
  5.       
  6.     <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">  
  7.         <property name="jdbcUrl" value="jdbc:mysql:///myactiviti" />  
  8.         <property name="jdbcDriver" value="com.mysql.jdbc.Driver" />  
  9.         <property name="jdbcUsername" value="root" />  
  10.         <property name="jdbcPassword" value="123456" />  
  11.         <property name="databaseSchemaUpdate" value="true" />  
  12.     bean>  
  13.   
  14. beans>  

在 resource 下新建Activiti Diagram

可以通过这个地址安装插件  《Activiti BPMN 2.0 designer - http://activiti.org/designer/update/ 》




将UserTask分别修改成入下图所示



修改好ID 和 NAME然后在修改Main config中的Assignee,员工请假修改为张三,老板审批修改为老板



点击Window--->Preferences--->Activiti--->Save Actions:将Create process definition image when saving the diagram勾选

然后保存bpmn文件的时候会自动帮你截图

通过ProcessEngines.getDefaultProcessEngine获取流程引擎

[java] view plain copy
  1. //通过activiti.cfg.xml获取流程引擎  
  2. ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();"code" class="java">  

通过源码可以看到  getDefaultProcessEngine方法会通过默认的activiti.cfg.xml文件名或者是activiti-context.xml文件名读取xml文件

当获取到的processEngine对象不为空时   就会在数据库创建关于activiti的23张表

具体作用如下:

跟详细的情况可以去下面这个地址了解:

http://www.cnblogs.com/llzgzljl/archive/2013/10/07/3356108.html

此时你可以在数据库中看到act_ge_property表中插入了3条数据

将MyLeave.bpmn和MyLeave.png打包成myleave.zip文件(一定要是zip别压缩成rar)

执行以下方法,发布请假流程:

[java] view plain copy
  1. /** 
  2.  * 发布流程 
  3.  * 发布流程后,流程文件会保存到数据库中 
  4.  */  
  5. @Test  
  6. public void deployFlow(){  
  7.     RepositoryService repositoryService = processEngine.getRepositoryService();  
  8.       
  9.     //获取在classpath下的流程文件  
  10.     InputStream in = this.getClass().getClassLoader().getResourceAsStream("myleave.zip");  
  11.     ZipInputStream zipInputStream = new ZipInputStream(in);  
  12.     //使用deploy方法发布流程  
  13.     repositoryService.createDeployment()  
  14.                      .addZipInputStream(zipInputStream)  
  15.                      .name("Myleave")  
  16.                      .deploy();  
  17. }  

查看数据库  发布的流程文件信息会保存在下面的三张表中:

 

我们可以通过RepositoryService获取详细的流程定义信息

[java] view plain copy
  1. @Test  
  2. public void queryProcdef(){  
  3.     RepositoryService repositoryService = processEngine.getRepositoryService();  
  4.     //创建查询对象  
  5.     ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();  
  6.     //添加查询条件  
  7.     query.processDefinitionKey("myProcess");//通过key获取  
  8.         // .processDefinitionName("My process")//通过name获取  
  9.         // .orderByProcessDefinitionId()//根据ID排序  
  10.     //执行查询获取流程定义明细  
  11.     List pds = query.list();  
  12.     for (ProcessDefinition pd : pds) {  
  13.         System.out.println("ID:"+pd.getId()+",NAME:"+pd.getName()+",KEY:"+pd.getKey()+",VERSION:"+pd.getVersion()+",RESOURCE_NAME:"+pd.getResourceName()+",DGRM_RESOURCE_NAME:"+pd.getDiagramResourceName());  
  14.     }  
  15. }  

key 和 name 就是我们原先画bpmn时候声明的:

 

使用流程定义的Key发布一个请假流程:

[java] view plain copy
  1. /** 
  2.      * 发布流程 
  3.      */  
  4.     @Test  
  5.     public void startFlow(){  
  6.           
  7.         RuntimeService runtimeService = processEngine.getRuntimeService();  
  8.         /** 
  9.          * 启动请假单流程  并获取流程实例 
  10.          * 因为该请假单流程可以会启动多个所以每启动一个请假单流程都会在数据库中插入一条新版本的流程数据 
  11.          * 通过key启动的流程就是当前key下最新版本的流程 
  12.          *  
  13.          */  
  14.         ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");  
  15.         System.out.println("id:"+processInstance.getId()+",activitiId:"+processInstance.getActivityId());  
  16.     }  

当流程发布后在  act_ru_task ,act_ru_execution, act_ru_identitylink 表中插入流程数据

接下来就可以通过用户ID去查看该用户的任务了

[java] view plain copy
  1. /** 
  2.  * 查看任务 
  3.  */  
  4. @Test  
  5. public void queryTask(){  
  6.     //获取任务服务对象  
  7.     TaskService taskService = processEngine.getTaskService();  
  8.     //根据接受人获取该用户的任务  
  9.     List tasks = taskService.createTaskQuery()  
  10.                                 .taskAssignee("张三")  
  11.                                 .list();  
  12.     for (Task task : tasks) {  
  13.         System.out.println("ID:"+task.getId()+",姓名:"+task.getName()+",接收人:"+task.getAssignee()+",开始时间:"+task.getCreateTime());  
  14.     }  
  15. }  

提出请假申请,启动流程

[java] view plain copy
  1. @Test  
  2. public void startTask(){  
  3.     TaskService taskService = processEngine.getTaskService();  
  4.     //taskId 就是查询任务中的 ID  
  5.     String taskId = "204";  
  6.     //完成请假申请任务  
  7.     taskService.complete(taskId );  
  8. }  

查看数据库变化 可以看到 表中的数据已经变成了老板审批相关数据

老板查看任务 ,并审批请假

[java] view plain copy
  1. /** 
  2.  * 查看任务 
  3.  */  
  4. @Test  
  5. public void queryTask(){  
  6.     //获取任务服务对象  
  7.     TaskService taskService = processEngine.getTaskService();  
  8.     //根据接受人获取该用户的任务  
  9.     List tasks = taskService.createTaskQuery()  
  10.                                 .taskAssignee("老板")  
  11.                                 .list();  
  12.     for (Task task : tasks) {  
  13.         System.out.println("ID:"+task.getId()+",姓名:"+task.getName()+",接收人:"+task.getAssignee()+",开始时间:"+task.getCreateTime());  
  14.     }  
  15. }  
[java] view plain copy
  1. @Test  
  2. public void startTask(){  
  3.     TaskService taskService = processEngine.getTaskService();  
  4.     //taskId 就是查询任务中的 ID  
  5.     String taskId = "302";  
  6.     //完成请假申请任务  
  7.     taskService.complete(taskId );  
  8. }  

流程完毕,可以再act_hi_actinst表中看到整个请假流程

 

整个流程的过程是  1.发布流程 --->2.启动流程--->3.相关人查看任务完成并完成

数据都是存放在数据库中


具体详情可以查看Activiti 用户手册

http://www.mossle.com/docs/activiti/#


activiti的视频资料

java学习路线-Java技术人员之路从初级到高级   里面有

你可能感兴趣的:(Activiti,JavaWeb)