1.写在前面:
activiti中并发后的活动任务节点通过runtimeService.getActiveActivityIds(String proInstId)得到。
这个方法在Activiti的doc文档中介绍的不对,它错把参数写成了executionId,而事实上,一个流程实例(对应流程实例编号ProInstId)在运行中通过并发节点产生多个执行任务(对应执行编号executionId)。
2.关键代码:
ProcessInstanceAction.java
- public class ProcessInstanceAction extends BaseAction {
- private List<ActivityImpl> actImpls = new ArrayList<ActivityImpl>();
- public List<ActivityImpl> getActImpls() {
- return actImpls;
- }
- public void setActImpls(List<ActivityImpl> actImpls) {
- this.actImpls = actImpls;
- }
- /**
- * 显示流程图
- *
- * @return
- * @throws Exception
- */
- public String getProcessPic() throws Exception {
- // String taskId =
- // "2901";//getRequest().getParameter("taskId");3016,552,3020
- String procDefId = getRequest().getParameter("procDefId");
- ProcessDefinition procDef = repositoryService
- .createProcessDefinitionQuery().processDefinitionId(procDefId)
- .singleResult();
- String diagramResourceName = procDef.getDiagramResourceName();
- InputStream imageStream = repositoryService.getResourceAsStream(procDef
- .getDeploymentId(), diagramResourceName);
- getRequest().setAttribute("inputStream", imageStream);
- return SUCCESS;
- }
- /**
- * 获取跟踪信息
- *
- * @return
- * @throws Exception
- */
- public String getProcessMap() throws Exception {
- String procDefId = getRequest().getParameter("procDefId");
- String proInstId = getRequest().getParameter("procInstId");
- ProcessDefinition processDefinition = repositoryService
- .createProcessDefinitionQuery().processDefinitionId(procDefId)
- .singleResult();
- ProcessDefinitionImpl pdImpl = (ProcessDefinitionImpl) processDefinition;
- String processDefinitionId = pdImpl.getId();// 流程标识
- ProcessDefinitionEntity def = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
- .getDeployedProcessDefinition(processDefinitionId);
- List<ActivityImpl> activitiList = def.getActivities();// 获得当前任务的所有节点
- /**获取活动任务start**/
- List<String> ActiveActivityIds = runtimeService
- .getActiveActivityIds(proInstId);//获取将被执行的活动节点
- for (String activeId : ActiveActivityIds) {
- for (ActivityImpl activityImpl : activitiList) {
- String id = activityImpl.getId();
- if (activityImpl.isScope()) {//判断节点是否为subProcess
- if (activityImpl.getActivities().size() > 1) {
- List<ActivityImpl> subAcList = activityImpl
- .getActivities();
- for (ActivityImpl subActImpl : subAcList) {
- String subid = subActImpl.getId();
- if (activeId.equals(subid)) {// 获得执行到的活动节点
- actImpls.add(subActImpl);
- break;
- }
- }
- }
- }
- if (activeId.equals(id)) {// 获得执行到的活动节点
- actImpls.add(activityImpl);
- }
- }
- }
- /**获取活动任务end**/
- getRequest().setAttribute("procDefId", procDefId);
- /**这个参数通过前台的showImg.jsp传给getProcessPic.action获取流程图**/
- return SUCCESS;
- }
- }
pic.jsp
- <%@page import="java.io.InputStream"%>
- <%@page import="org.activiti.engine.impl.*"%>
- <%@page import="org.activiti.engine.impl.pvm.*"%>
- <%@page import="org.activiti.engine.impl.pvm.process.*"%>
- <%@page import="org.activiti.engine.repository.*"%>
- <%@page import="org.activiti.engine.*"%>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%
- InputStream is = (InputStream)request.getAttribute("inputStream");
- byte[] b = new byte[1024];
- int len = -1;
- while((len = is.read(b, 0, 1024)) != -1) {
- response.getOutputStream().write(b, 0, len);
- // 防止异常:getOutputStream() has already been called for this response
- out.clear();
- out = pageContext.pushBody();
- }
- %>
showImg.jsp
- <%@page import="java.io.InputStream"%>
- <%@page import="org.activiti.engine.impl.*"%>
- <%@page import="org.activiti.engine.impl.pvm.*"%>
- <%@page import="org.activiti.engine.impl.pvm.process.*"%>
- <%@page import="org.activiti.engine.repository.*"%>
- <%@page import="org.activiti.engine.*"%>
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title></title>
- </head>
- <body>
- <div >
- <img src="getProcessPic.action?procDefId=${procDefId}" style="position:absolute; left:0px; top:0px;">
- <s:iterator value="actImpls">
- <!-- 给执行的节点加框 -->
- <div style="position:absolute; border:2px solid red;left:${x-1 }px;top:${y-1 }px;width:${width }px;height:${height }px;"></div>
- </s:iterator>
- </div>
- </body>
- </html>
另附:环境配置struts+activiti+spring:
struts.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constant name="struts.objectFactory.spring.autoWire" value="name" />
- <constant name="struts.objectFactory" value="spring" />
- <package name="default" extends="struts-default">
- <action name="getProcessPic" class="processInstanceAction" method="getProcessPic">
- <result name = "success" >/incident/pic.jsp</result>
- </action>
- <action name="getProcessMap" class="processInstanceAction" method="getProcessMap">
- <result name = "success" >/incident/showImg.jsp</result>
- </action>
- </package>
- </struts>
applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="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">
- <context:component-scan base-package="org.activiti.spring.test.jpa"/>
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/acitivitiexample?autoReconnect=true" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- <property name="defaultAutoCommit" value="false" />
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
- <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref="entityManagerFactory"/>
- </bean>
- <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="persistenceXmlLocation">
- <value>classpath:/jpa/persistence.xml</value>
- </property>
- <property name="jpaVendorAdapter">
- <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
- <property name="databasePlatform" value="org.apache.openjpa.jdbc.sql.MySQLDictionary" />
- </bean>
- </property>
- </bean>
- <bean id="incidentBean" class="jpa.IncidentBean">
- </bean>
- <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
- <property name="dataSource" ref="dataSource" />
- <property name="transactionManager" ref="transactionManager" />
- <property name="databaseSchemaUpdate" value="true" />
- <property name="jpaEntityManagerFactory" ref="entityManagerFactory" />
- <property name="jpaHandleTransaction" value="false" />
- <property name="jpaCloseEntityManager" value="false" />
- <property name="jobExecutorActivate" value="true" />
- <property name="mailServerPort" value="25" />
- </bean>
- <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
- <property name="processEngineConfiguration" ref="processEngineConfiguration" />
- </bean>
- <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
- <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
- <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
- <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
- <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
- <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />
- <bean id="formService" factory-bean="processEngine"
- factory-method="getFormService" />
- <bean id="processInstanceAction" class="action.ProcessInstanceAction"
- scope="prototype">
- <property name="runtimeService" ref="runtimeService"></property>
- <property name="taskService" ref="taskService"></property>
- <property name="formService" ref="formService"></property>
- <property name="identityService" ref="identityService"></property>
- <property name="incidentBean" ref="incidentBean"></property>
- <property name="repositoryService" ref="repositoryService"></property>
- </bean>
- </beans>
***********************分割线*********************
秋天该多好,若你尚在场……