前段时间一直在弄工作流的事情,也是我第一次搞工作流,本来很早就想写的,一直拖到现在,变懒了。。。。想了又想还是必须记录下,不然以后就忘了
项目地址:https://github.com/James-Pan0525/activiti.git
先说下版本
springboot :2.1.6.RELEASE
activiti: 5.22.0
首先是整个activiti官方的编辑器到项目中来
1.下载activiti5.22的源码包
链接:https://pan.baidu.com/s/1Aashjs67-L1ytT9jRcPzoA
提取码:k23l
2 导入静态文件
将activiti-5.22.0/war/activiti-explorer.war解压
将文件夹内 diagram-viewer,editor-app,modeler.html拷贝到项目中resource/public目录下如图
上图editor-app就是编辑器,modeler.html是编辑器的入口页面。
在editor-app/app-cfg.js中配置一下项目url。访问项目前缀 我下面设置的null,
var ACTIVITI = ACTIVITI || {};
ACTIVITI.CONFIG = {
'contextRoot' : '',
};
前端页面基本上就这样,后面配置后端
pom:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>activiti</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>activiti</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Activiti 启动器 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>5.22.0</version>
</dependency>
<!-- Activiti 流程图 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-diagram-rest</artifactId>
<version>5.22.0</version>
</dependency>
<!-- Activiti 在线设计 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-modeler</artifactId>
<version>5.22.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件:application.yml ,主要是配置数据库,首先要创建数据库activiti,然后启动项目的时候activiti才会自动创建表
spring:
datasource:
url: jdbc:mysql://localhost:3306/activiti?characterEncoding=UTF-8&nullCatalogMeansCurrent=true&serverTimezone=GMT&useSSL=false
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
connection-test-query: SELECT 1 FROM DUAL
connection-timeout: 30000
maximum-pool-size: 20
max-lifetime: 1800000
minimum-idle: 5
拷贝jar包中controller
在activiti-5.22.0\libs\activiti-modeler-5.22.0-sources.jar,将StencilsetRestResource.java,
ModelEditorJsonRestResource.java,ModelSaveRestResource.java三个文件拷贝到controller目录
类上加上注解 @RestController
添加activitiConfig配置类,设置流程图字体和自动创建数据库表等
ActivitiConfig
package com.example.activiti.config;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration
public class ActivitiConfig {
@Autowired
private DataSource dataSource;
@Autowired
private PlatformTransactionManager transactionManager;
@Bean
public SpringProcessEngineConfiguration getProcessEngineConfiguration() {
SpringProcessEngineConfiguration config =
new SpringProcessEngineConfiguration();
// 流程图字体设置
config.setActivityFontName("宋体");
config.setAnnotationFontName("宋体");
config.setLabelFontName("黑体");
config.setDataSource(dataSource);
config.setTransactionManager(transactionManager);
config.setDatabaseType("mysql");
//设置自动创建表
config.setDatabaseSchemaUpdate("true");
return config;
}
@Bean
@Primary
public TaskExecutor primaryTaskExecutor() {
return new ThreadPoolTaskExecutor();
}
}
还有禁用activiti中自动集成的security的权限验证,当我们访问接口的时候会弹出登录界面,所以我们需要禁用掉登录验证
在启动类中添加注解
package com.example.activiti;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(exclude = {org.activiti.spring.boot.SecurityAutoConfiguration.class, org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class})
public class ActivitiApplication {
public static void main(String[] args) {
SpringApplication.run(ActivitiApplication.class, args);
}
}
创建空mode即跳转到modeler编辑页面 ActivitiController
package com.example.activiti.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/activiti")
public class ActivitiController {
/**
* 创建模型
*/
@RequestMapping("/create")
public void create(HttpServletRequest request, HttpServletResponse response) {
try {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
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, "hello1111");
modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
String description = "hello1111";
modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
modelData.setMetaInfo(modelObjectNode.toString());
modelData.setName("hello1111");
modelData.setKey("12313123");
//保存模型
repositoryService.saveModel(modelData);
repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));
response.sendRedirect(request.getContextPath() + "/modeler.html?modelId=" + modelData.getId());
} catch (Exception e) {
System.out.println("创建模型失败:");
}
}
@GetMapping
public void get(HttpServletRequest request, HttpServletResponse response) throws Exception{
response.sendRedirect(request.getContextPath() + "/modeler.html?modelId=" + request.getParameter("id"));
}
}
汉化:在resources下创建 stencilset.json,网上到处都是,下载一个放在这就行
启动项目
activiti会自动创建表
访问连接:http://127.0.0.1:8081/activiti/create