package com.xinwei; import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.image.ProcessDiagramGenerator;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; public class TraceProcessInstaceUtil {
public static void main(String[] args) {
generateImage("");
}
public static byte[] generateImage(String processInstanceId){
//1.创建核心引擎流程对象processEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
//流程定义
BpmnModel bpmnModel = processEngine.getRepositoryService().getBpmnModel(task.getProcessDefinitionId()); //正在活动节点
List<String> activeActivityIds = processEngine.getRuntimeService().getActiveActivityIds(task.getExecutionId()); ProcessDiagramGenerator pdg = processEngine.getProcessEngineConfiguration().getProcessDiagramGenerator(); //得到图片输出流(这样加可防止生成的流程图片乱码)
InputStream inputStream = processEngine.getProcessEngineConfiguration().getProcessDiagramGenerator()
.generateDiagram(bpmnModel, "png", activeActivityIds, new ArrayList(),
processEngine.getProcessEngineConfiguration().getActivityFontName(),
processEngine.getProcessEngineConfiguration().getLabelFontName(), null, 1.0);
try {
//生成本地图片
File file = new File("D:/test1.png");
FileUtils.copyInputStreamToFile(inputStream, file);
System.out.println("生成流程图成功");
return IOUtils.toByteArray(inputStream);
} catch (Exception e) {
throw new RuntimeException("生成流程图异常!", e);
} finally {
IOUtils.closeQuietly(inputStream);
}
}
}
附:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<!-- 连接数据的配置 -->
<property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/activitidemo?useUnicode=true&characterEncoding=utf8"></property>
<property name="jdbcUsername" value="root"></property>
<property name="jdbcPassword" value="xinwei"></property>
<property name="activityFontName" value="宋体" />
<property name="labelFontName" value="宋体" />
<!-- 没有表创建表 -->
<property name="databaseSchemaUpdate" value="true"></property>
</bean> </beans>