使用 Activiti5.22.0 版本
资料文件已整理:https://pan.baidu.com/s/1UyCCAfMQd9yO_yNmH81Qbw 提取码:6724
官网下载地址:
Activiti 5.22.0 源码包: https://codeload.github.com/Activiti/Activiti/zip/activiti-5.22.0
将压缩包解压到任一目录下。我们可以看到Activiti的目录结构
将wars中的 activiti-explorer.war 复制到一个tomcat的webapp目录下,启动tomcat。在浏览器中输入 http://localhost:8080/activiti-explorer/,我们可以看到Activiti的登录界面
使用账户 kermit/kermit 登录系统
登录成功,我们来画一个流程图试试
ssm 项目搭建 maven配置 主要看 Activiti jar 就好了
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zy</groupId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<activiti.version>5.22.0</activiti.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.0</version>
</dependency>
<!-- activiti需要log4j日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- json格式包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<!-- ibatis分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
<!-- Activiti jar -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-model</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-model</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-converter</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-layout</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-common-rest</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-crystalball</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-diagram-rest</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-explorer</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-image-generator</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-json-converter</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-modeler</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-simple-workflow</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-process-validation</artifactId>
<version>${activiti.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<artifactId>ActivitiDemo</artifactId>
</project>
配置activiti-context.xml
在 resouce 目录下创建 activiti-context.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">
<!-- spring负责创建流程引擎的配置文件 -->
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置事务管理器,统一事务 -->
<property name="transactionManager" ref="txManager" />
<!-- 设置建表策略,如果没有表,自动创建表 -->
<property name="databaseSchemaUpdate" value="true" />
<!-- 解决发布部署模型获取图片中文乱码 -->
<property name="activityFontName" value="宋体" />
<property name="labelFontName" value="微软雅黑" />
<property name="annotationFontName" value="黑体"/>
</bean>
<!-- 创建流程引擎对象 -->
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
<bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper"/>
<!--
相当于下面的代码
RepositoryServicie repositoryService = processEngine.getRepositoryService();
RuntimeServicie repositoryService = processEngine.getRuntimeServicie();
TaskServicie taskServicie = processEngine.getTaskServicie();
HistoryServicie historyServicie = processEngine.getHistoryServicie();
-->
<!-- 由流程引擎对象,提供的方法,创建项目中使用的Activiti工作流的Service -->
<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="formService" factory-bean="processEngine" factory-method="getFormService" />
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
</beans>
在 spring 配置文件中导入
<import resource="classpath:activiti-context.xml"/>
集成Activiti的web流程设计器
1,复制文件
1.1 将tomcat下源码文件 activit-explorer 中的diagram-viewer,editor-app和modeler.html文件复制到demo工程的src/main/webapp目录下
1.2将activiti-explorer工程中WEB-INF/classes文件加下的stencilset.json,ui.properties,ui.properties.alfresco复制到demo工程的src/main/resources目录下。
1.3 打开activiti5.22.0的源码
将modules/目录中找到
Activiti-activiti-5.22.0\modules\activiti-diagram-rest\src\main\java\org\activiti\rest\diagram
Activiti-activiti-5.22.0\modules\activiti-modeler\src\main\java\org\activiti\rest\editor
这两个包,将他们整个复制到demo工程的src/main/java
总结:
2,配置springmvc 扫描
<context:component-scan base-package="org.activiti.rest" />
3,修改web.xml配置
让spring也管理起来带/service/
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
4,修改editor-app/app-cfg.js 文件
将其中的 “activiti-explorer” 修改为 当前项目名称 (activiti_demo)
5,最后 测试
package com.zyxx.activiti.editor;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.editor.language.json.converter.BpmnJsonConverter;
import org.activiti.engine.HistoryService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.Model;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
@Controller
@RequestMapping("/model")
public class ModuleController {
private Logger logger = LoggerFactory.getLogger(ModuleController.class);
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Autowired
private ManagementService managementService;
@Autowired
private HistoryService historyService;
/**
* 跳转到模型编辑页面
* author klc
* date 2018年11月21日
* @param request
* @param response
*/
@RequestMapping(value = "create")
public void create(@RequestParam("name") String name, @RequestParam("key") String key,
@RequestParam("description") String description, HttpServletRequest request, HttpServletResponse response) {
try {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode editorNode = objectMapper.createObjectNode();
editorNode.put("id", "canvas");
editorNode.put("resourceId", "canvas");
ObjectNode stencilSetNode = objectMapper.createObjectNode();
stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");
editorNode.put("stencilset", stencilSetNode);
Model modelData = repositoryService.newModel();
ObjectNode modelObjectNode = objectMapper.createObjectNode();
modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, name);
modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
description = StringUtils.defaultString(description);
modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
modelData.setMetaInfo(modelObjectNode.toString());
modelData.setName(name);
modelData.setKey(StringUtils.defaultString(key));
repositoryService.saveModel(modelData);
repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));
response.sendRedirect(request.getContextPath() + "/modeler.html?modelId=" + modelData.getId());
} catch (Exception e) {
logger.error("创建模型失败:", e);
}
}
/**
* 部署
* author klc
* date 2018年11月21日
* @param modelId
* @throws Exception
*/
@RequestMapping("/deploy")
public void deploy(String modelId) throws Exception{
Model modelData = this.repositoryService.getModel(modelId);
ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelData.getId()));
byte[] bpmnBytes = null;
BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
bpmnBytes = new BpmnXMLConverter().convertToXML(model);
String processName = modelData.getName() + ".bpmn20.xml";
Deployment deployment = repositoryService.createDeployment()
.name(modelData.getName())
.addString(processName, new String(bpmnBytes)).deploy();
if(deployment != null){
System.out.println("deploy successful......"+deployment.getId());
}
}
@RequestMapping("/deleteByteArrayModelId")
public void deleteByteArrayModelId(String modelId) {
repositoryService.deleteModel(modelId);
System.out.println("over.... ");
}
@RequestMapping(value = "showmodelpicture")
public void showModelPicture(HttpServletResponse response,String modelId)throws Exception{
//ModelAndView mv=new ModelAndView();
Model modelData = this.repositoryService.getModel(modelId);
ObjectNode modelNode = null;
try {
modelNode = (ObjectNode) new ObjectMapper()
.readTree(repositoryService.getModelEditorSource(modelData.getId()));
} catch (IOException e) {
e.printStackTrace();
}
BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
ProcessDiagramGenerator processDiagramGenerator = new DefaultProcessDiagramGenerator();
InputStream inputStream = processDiagramGenerator.generateDiagram(model, "jpg", new ArrayList<String>(),
new ArrayList<String>(), "微软雅黑", "微软雅黑", "微软雅黑",null, 1.1);
/*InputStream inputStream = processDiagramGenerator.generateDiagram(model,
"jpg",
Collections.<String>emptyList(), Collections.<String>emptyList(),
"WenQuanYi Micro Hei", "WenQuanYi Micro Hei",
null, null, 1.0);*/
OutputStream out = response.getOutputStream();
for (int b = -1; (b = inputStream.read()) != -1; ) {
out.write(b);
}
out.close();
inputStream.close();
}
/**
* 启动
* author klc
* date 2018年11月21日
* @param modelId
* @throws Exception
*/
@RequestMapping("/start")
public void deploy(String key,String user,String businessId) throws Exception{
Map<String, Object> params = new HashMap<String, Object>();
params.put("user", user);
params.put("busKey", "LeaveBill."+businessId);
ProcessInstance processInstance = this.runtimeService.startProcessInstanceByKey(key, "LeaveBill."+businessId, params);
if(processInstance != null){
System.out.println("start successful......"+processInstance.getId());
}
}
/**
* 查看我的任务
* author klc
* date 2018年11月21日
* @param modelId
* @throws Exception
*/
@RequestMapping("/myTask")
public void myTask(String user) throws Exception{
List<Task> list = this.taskService.createTaskQuery()
.taskAssignee(user)
.orderByTaskCreateTime()
.desc()
.list();
for (Task task : list) {
System.out.println("任务ID:"+ task.getId());
System.out.println("任务办理人:"+ task.getAssignee());
System.out.println("任务名:"+ task.getName());
}
}
/**
* 办理任务
* author klc
* date 2018年11月21日
* @param modelId
* @throws Exception
*/
@RequestMapping("/doTask")
@ResponseBody
public void doTask(String user,String taskId,String comment,String option) throws Exception{
Map<String, Object> variables = new HashMap<String, Object>();
//使用任务ID,查询任务对象,获取流程流程实例ID
Task task = this.taskService.createTaskQuery()//
.taskId(taskId)//使用任务ID查询
.singleResult();
//获取流程实例ID
String processInstanceId = task.getProcessInstanceId();
Authentication.setAuthenticatedUserId(user);
this.taskService.addComment(taskId, processInstanceId, comment);
variables.put("user", user);
variables.put("option", option);
this.taskService.complete(taskId, variables);
ProcessInstance pi = this.runtimeService.createProcessInstanceQuery()//
.processInstanceId(processInstanceId)//使用流程实例ID查询
.singleResult();
//流程结束了
if(pi==null){
System.out.println("workflow end!");
}
}
/**
* 获取批注
* author klc
* date 2018年11月21日
* @param modelId
* @throws Exception
*/
@RequestMapping("/getComment")
@ResponseBody
public void getComment() throws Exception{
String businessKey = "LeaveBill.1";
HistoricProcessInstance processInstance = this.historyService.createHistoricProcessInstanceQuery().processInstanceBusinessKey(businessKey).singleResult();
List<Comment> list = this.taskService.getProcessInstanceComments(processInstance.getId());
for (Comment comment : list) {
System.out.println(comment.getUserId() + ":" + comment.getFullMessage());
}
}
@RequestMapping("/editor")
public void editor(String modelId,HttpServletResponse response) {
BufferedOutputStream bos = null;
try {
try {
Model modelData = this.repositoryService.getModel(modelId);
ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelData.getId()));
byte[] bpmnBytes = null;
BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
bpmnBytes = new BpmnXMLConverter().convertToXML(model);
String processName = modelData.getName() + ".bpmn20.xml";
// 封装输出流
bos = new BufferedOutputStream(response.getOutputStream());
bos.write(bpmnBytes);// 写入流
response.setContentType("application/x-msdownload;");
response.setHeader("Content-Disposition", "attachment; filename=" + processName);
response.flushBuffer();
} finally {
bos.flush();
bos.close();
}
} catch (Exception e) {
System.out.println("流程部署失败");
e.printStackTrace();
}
}
}
http://localhost:8088/ActivitiDemo/model/create?name=test&key=test&description=testModel
访问成功!
请大家不要着急 一步一步弄。 下一个博客 Activiti5.22.0 汉化
源码地址:https://pan.baidu.com/s/1UyCCAfMQd9yO_yNmH81Qbw 提取码:6724
分享是快乐的,也见证了个人成长历程,文章大多都是工作经验总结以及平时学习积累,基于自身认知不足之处在所难免,也请大家指正,共同进步。