activiti 并发任务实例流程图的显示

1.写在前面: 
activiti中并发后的活动任务节点通过runtimeService.getActiveActivityIds(String proInstId)得到。 
这个方法在Activiti的doc文档中介绍的不对,它错把参数写成了executionId,而事实上,一个流程实例(对应流程实例编号ProInstId)在运行中通过并发节点产生多个执行任务(对应执行编号executionId)。 
2.关键代码: 
ProcessInstanceAction.java 

Java代码   收藏代码
  1. public class ProcessInstanceAction extends BaseAction {  
  2.   private List<ActivityImpl> actImpls = new ArrayList<ActivityImpl>();  
  3.   
  4.     public List<ActivityImpl> getActImpls() {  
  5.         return actImpls;  
  6.     }  
  7.   
  8.     public void setActImpls(List<ActivityImpl> actImpls) {  
  9.         this.actImpls = actImpls;  
  10.     }  
  11.     /** 
  12.      * 显示流程图 
  13.      *  
  14.      * @return 
  15.      * @throws Exception 
  16.      */  
  17.     public String getProcessPic() throws Exception {  
  18.         // String taskId =  
  19.         // "2901";//getRequest().getParameter("taskId");3016,552,3020  
  20.         String procDefId = getRequest().getParameter("procDefId");  
  21.         ProcessDefinition procDef = repositoryService  
  22.                 .createProcessDefinitionQuery().processDefinitionId(procDefId)  
  23.                 .singleResult();  
  24.         String diagramResourceName = procDef.getDiagramResourceName();  
  25.         InputStream imageStream = repositoryService.getResourceAsStream(procDef  
  26.                 .getDeploymentId(), diagramResourceName);  
  27.         getRequest().setAttribute("inputStream", imageStream);  
  28.         return SUCCESS;  
  29.     }  
  30.   
  31.     /** 
  32.      * 获取跟踪信息 
  33.      *  
  34.      * @return 
  35.      * @throws Exception 
  36.      */  
  37.     public String getProcessMap() throws Exception {  
  38.         String procDefId = getRequest().getParameter("procDefId");  
  39.         String proInstId = getRequest().getParameter("procInstId");  
  40.         ProcessDefinition processDefinition = repositoryService  
  41.                 .createProcessDefinitionQuery().processDefinitionId(procDefId)  
  42.                 .singleResult();  
  43.   
  44.         ProcessDefinitionImpl pdImpl = (ProcessDefinitionImpl) processDefinition;  
  45.         String processDefinitionId = pdImpl.getId();// 流程标识  
  46.   
  47.         ProcessDefinitionEntity def = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)  
  48.                 .getDeployedProcessDefinition(processDefinitionId);  
  49.   
  50.         List<ActivityImpl> activitiList = def.getActivities();// 获得当前任务的所有节点  
  51.   
  52.         /**获取活动任务start**/  
  53.   
  54.         List<String> ActiveActivityIds = runtimeService  
  55.                 .getActiveActivityIds(proInstId);//获取将被执行的活动节点  
  56.         for (String activeId : ActiveActivityIds) {  
  57.   
  58.             for (ActivityImpl activityImpl : activitiList) {  
  59.                 String id = activityImpl.getId();  
  60.                 if (activityImpl.isScope()) {//判断节点是否为subProcess  
  61.                     if (activityImpl.getActivities().size() > 1) {  
  62.                         List<ActivityImpl> subAcList = activityImpl  
  63.                                 .getActivities();  
  64.                         for (ActivityImpl subActImpl : subAcList) {  
  65.                             String subid = subActImpl.getId();  
  66.                             if (activeId.equals(subid)) {// 获得执行到的活动节点  
  67.                                 actImpls.add(subActImpl);  
  68.                                 break;  
  69.                             }  
  70.                         }  
  71.                     }  
  72.   
  73.                 }  
  74.                 if (activeId.equals(id)) {// 获得执行到的活动节点  
  75.   
  76.                     actImpls.add(activityImpl);  
  77.                 }  
  78.             }  
  79.         }  
  80.         /**获取活动任务end**/  
  81.          
  82.         getRequest().setAttribute("procDefId", procDefId);  
  83.         /**这个参数通过前台的showImg.jsp传给getProcessPic.action获取流程图**/  
  84.   
  85.         return SUCCESS;  
  86.     }  
  87. }  



pic.jsp 

Html代码   收藏代码
  1. <%@page import="java.io.InputStream"%>  
  2. <%@page import="org.activiti.engine.impl.*"%>  
  3. <%@page import="org.activiti.engine.impl.pvm.*"%>  
  4. <%@page import="org.activiti.engine.impl.pvm.process.*"%>  
  5. <%@page import="org.activiti.engine.repository.*"%>  
  6. <%@page import="org.activiti.engine.*"%>  
  7. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  8.     pageEncoding="UTF-8"%>  
  9.   
  10. <%  
  11.       
  12.     InputStream is = (InputStream)request.getAttribute("inputStream");  
  13.       
  14.     byte[] b = new byte[1024];  
  15.     int len = -1;  
  16.     while((len = is.read(b, 0, 1024)) != -1) {  
  17.         response.getOutputStream().write(b, 0, len);  
  18.         // 防止异常:getOutputStream() has already been called for this response  
  19.         out.clear();  
  20.         out = pageContext.pushBody();  
  21.     }     
  22.   
  23. %>  


showImg.jsp 

Html代码   收藏代码
  1. <%@page import="java.io.InputStream"%>  
  2. <%@page import="org.activiti.engine.impl.*"%>  
  3. <%@page import="org.activiti.engine.impl.pvm.*"%>  
  4. <%@page import="org.activiti.engine.impl.pvm.process.*"%>  
  5. <%@page import="org.activiti.engine.repository.*"%>  
  6. <%@page import="org.activiti.engine.*"%>  
  7. <%@ page language="java" pageEncoding="UTF-8"%>  
  8. <%@ taglib prefix="s" uri="/struts-tags"%>  
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  10.   
  11. <html>  
  12. <head>  
  13.     <title></title>  
  14. </head>  
  15. <body>  
  16.     <div >  
  17.         <img src="getProcessPic.action?procDefId=${procDefId}" style="position:absolute; left:0px; top:0px;">  
  18.     <s:iterator value="actImpls">       
  19.         <!-- 给执行的节点加框 -->  
  20.         <div style="position:absolute; border:2px solid red;left:${x-1 }px;top:${y-1 }px;width:${width }px;height:${height }px;"></div>  
  21.         </s:iterator>  
  22.     </div>  
  23.   
  24. </body>  
  25. </html>  


另附:环境配置struts+activiti+spring: 
struts.xml 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <constant name="struts.objectFactory.spring.autoWire" value="name" />  
  7.     <constant name="struts.objectFactory" value="spring" />  
  8.     <package name="default" extends="struts-default">  
  9.     <action name="getProcessPic" class="processInstanceAction" method="getProcessPic">  
  10.             <result name = "success" >/incident/pic.jsp</result>  
  11.         </action>  
  12.         <action name="getProcessMap" class="processInstanceAction" method="getProcessMap">  
  13.             <result name = "success" >/incident/showImg.jsp</result>  
  14.         </action>  
  15. </package>  
  16. </struts>  


applicationContext.xml 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"   
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  9.                            http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  10.   
  11.   <context:component-scan base-package="org.activiti.spring.test.jpa"/>  
  12.   
  13.   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  14.     <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  15.     <property name="url" value="jdbc:mysql://localhost:3306/acitivitiexample?autoReconnect=true" />  
  16.     <property name="username" value="root" />  
  17.     <property name="password" value="root" />  
  18.     <property name="defaultAutoCommit" value="false" />  
  19.   </bean>  
  20.     
  21.   <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>  
  22.   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
  23.     <property name="entityManagerFactory" ref="entityManagerFactory"/>  
  24.   </bean>  
  25.     
  26.   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
  27.     <property name="dataSource" ref="dataSource"/>  
  28.      <property name="persistenceXmlLocation">  
  29.       <value>classpath:/jpa/persistence.xml</value>  
  30.     </property>  
  31.     <property name="jpaVendorAdapter">  
  32.       <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">  
  33.         <property name="databasePlatform" value="org.apache.openjpa.jdbc.sql.MySQLDictionary" />  
  34.       </bean>  
  35.     </property>  
  36.   </bean>  
  37.     
  38.   <bean id="incidentBean" class="jpa.IncidentBean">  
  39.   </bean>  
  40.     
  41.   <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">  
  42.     <property name="dataSource" ref="dataSource" />  
  43.     <property name="transactionManager" ref="transactionManager" />  
  44.     <property name="databaseSchemaUpdate" value="true" />  
  45.     <property name="jpaEntityManagerFactory" ref="entityManagerFactory" />  
  46.     <property name="jpaHandleTransaction" value="false" />  
  47.     <property name="jpaCloseEntityManager" value="false" />  
  48.     <property name="jobExecutorActivate" value="true" />  
  49.     <property name="mailServerPort" value="25" />   
  50.    </bean>  
  51.     
  52.   <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">  
  53.     <property name="processEngineConfiguration" ref="processEngineConfiguration" />  
  54.   </bean>  
  55.     
  56.   <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />  
  57.   <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />  
  58.   <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />  
  59.   <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />  
  60.   <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />  
  61.   <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />  
  62.   <bean id="formService" factory-bean="processEngine"  
  63.         factory-method="getFormService" />  
  64.    <bean id="processInstanceAction" class="action.ProcessInstanceAction"  
  65.         scope="prototype">  
  66.         <property name="runtimeService" ref="runtimeService"></property>  
  67.         <property name="taskService" ref="taskService"></property>  
  68.         <property name="formService" ref="formService"></property>  
  69.         <property name="identityService" ref="identityService"></property>  
  70.         <property name="incidentBean" ref="incidentBean"></property>  
  71.         <property name="repositoryService" ref="repositoryService"></property>  
  72.   </bean>  
  73.  </beans>  


***********************分割线********************* 
秋天该多好,若你尚在场……

你可能感兴趣的:(Activiti)