SpringBoot使用activiti自定义流程demo解析
环境搭建【这里直接讲解自定义流程】
集成 Activiti Modeler
下载源码
我这里选用的是 Activiti 5.23.0 版本的页面,下载 zip,解压
Activiti 5.23.0 源码
pom依赖
1 | <?xml version="1.0" encoding="UTF-8"?> |
代码集成
前端代码集成
1 | 在项目中的 resource 文件夹下新建一个 static 文件夹 |
后端代码集成
将 Activit 5.23.0 项目中 resource 文件夹下的 stencilset.json 放到自己项目的 resource 目录下
路径:Activiti-5.23.0\modules\activiti-webapp-explorer2\src\main\resources
在我提供的demo中有汉化版本,这里只有国际版本
找到 activiti-modeler 包
将里面的 StencilsetRestResource.java,ModelEditorJsonRestResource.java,ModelSaveRestResource.java 文件放到自己的项目里
- StencilsetRestResource.java:用于读取 stencilset.json 文件
- ModelEditorJsonRestResource.java:用户获取流程数据
- ModelSaveRestResource.java:用于保存流程数据
路径:Activiti-5.23.0\modules\activiti-modeler\src\main\java\org\activiti\rest\editor项目结构
创建流程与部署流程1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221package com.activiti6.controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
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.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.Model;
import org.activiti.engine.runtime.ProcessInstance;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* 流程控制器
* zhudunfrng
*/
public class ModelerController{
private static final Logger logger = LoggerFactory.getLogger(ModelerController.class);
private RepositoryService repositoryService;
private ObjectMapper objectMapper;
private HistoryService historyService;
private RuntimeService runtimeService;
public ModelAndView index(ModelAndView modelAndView) {
modelAndView.setViewName("index");
modelAndView.addObject("modelList", repositoryService.createModelQuery().list());
return modelAndView;
}
/**
* 跳转编辑器页面
* @return
*/
public String editor(){
return "modeler";
}
/**
* 创建模型
* @param response
* @param name 模型名称
* @param key 模型key
*/
public void create(HttpServletResponse response,String name,String key) throws IOException {
logger.info("创建模型入参name:{},key:{}",name,key);
Model model = repositoryService.newModel();
ObjectNode modelNode = objectMapper.createObjectNode();
modelNode.put(ModelDataJsonConstants.MODEL_NAME, name);
modelNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, "");
modelNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
model.setName(name);
model.setKey(key);
model.setMetaInfo(modelNode.toString());
//存入表act_re_model
repositoryService.saveModel(model);
createObjectNode(model.getId());
response.sendRedirect("/editor?modelId="+ model.getId());
logger.info("创建模型结束,返回模型ID:{}",model.getId());
}
/**
* 创建模型时完善ModelEditorSource,这里是对画布的相关设置
* @param modelId
*/
private void createObjectNode(String modelId){
logger.info("创建模型完善ModelEditorSource入参模型ID:{}",modelId);
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);
try {
repositoryService.addModelEditorSource(modelId,editorNode.toString().getBytes("utf-8"));
} catch (Exception e) {
logger.info("创建模型时完善ModelEditorSource服务异常:{}",e);
}
logger.info("创建模型完善ModelEditorSource结束");
}
/**
* 发布流程///流程部署
* @param modelId 模型ID
* @return
*/
public Object publish(String modelId){
logger.info("流程部署入参modelId:{}",modelId);
Map<String, String> map = new HashMap<String, String>();
try {
Model modelData = repositoryService.getModel(modelId);//获取相应的模型信息,act_re_model
byte[] bytes = repositoryService.getModelEditorSource(modelData.getId());//获取相应的流程文件信息,act_ge_bytearray
if (bytes == null) {//没有相应的流程文件
logger.info("部署ID:{}的模型数据为空,请先设计流程并成功保存,再进行发布",modelId);
map.put("code", "FAILURE");
return map;
}
JsonNode modelNode = new ObjectMapper().readTree(bytes);//解析相应的流程文件