Activiti工作流学习 Activiti工作流与spring集成

一.前言

      前面Activiti工作流的学习,说明了Activiti的基本应用,在我们开发中可以根据实际的业务参考Activiti的API去更好的理解以及巩固。我们实际的开发中我们基本上都使用spring框架进行开发,现在来说明一下Activiti工作流与spring集成,Activiti工作流与spring集成还算比较简单,可以参考Activiti的API来进行整合。

二.Activiti常用的表

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
---------------------------------------------部署对象和流程定义相关的表---------------------------------------------
 
--部署对象信息表
SELECT T.*, T.ROWID FROM ACT_RE_DEPLOYMENT T;
 
--流程定义表
--ID_ 由KEY_ + VERSION_ + 随机生成是数组成
SELECT T.*, T.ROWID FROM ACT_RE_PROCDEF T where t.category_='1' order by t.version_ asc;
 
--资源文件表
SELECT T.*, T.ROWID FROM ACT_GE_BYTEARRAY T;
 
--主键生成策略表
SELECT T.*, T.ROWID FROM ACT_GE_PROPERTY T;
 
------------------------------------------流程实例、执行对象、任务------------------------------------------------
 
--正在执行的执行对象表
-- 执行ID_ 56  流程实例ID_ 56  流程定义ID_ _3701622B-4133-7B3D-F50F-E14B4F21E847:1:55  正在运行的任务定义ID_ 【可变】 USERTASKE736BEF8-4133-7B3D-F510-7B2DE7BEA8C6
SELECT T.*, T.ROWID FROM ACT_RU_EXECUTION T;
 
--流程实例历史表 开始信息
--历史流程定义ID_ 56 流程S实例ID_ 56 业务KEY_10000001 流程定义ID_ _3701622B-4133-7B3D-F50F-E14B4F21E847:1:55 开始任务节点ID_ STARTEVENT52B3145F-C133-7B3D-F50F-E6D48BA60EAE
SELECT T.*, T.ROWID FROM ACT_HI_PROCINST T;
 
--正在执行的任务对象表
--任务ID_ 68  执行ID_ 56  流程实例ID_ 56  流程定义ID_ _3701622B-4133-7B3D-F50F-E14B4F21E847:1:55  任务节点ID_ USERTASKE736BEF8-4133-7B3D-F510-7B2DE7BEA8C6
SELECT T.*, T.ROWID FROM ACT_RU_TASK T;
 
--历史任务流程实例信息
--历史任务ID_ 68   流程实例ID_ 56  执行实例ID_ 56   流程定义ID_ _3701622B-4133-7B3D-F50F-E14B4F21E847:1:55  任务节点ID_ USERTASKE736BEF8-4133-7B3D-F510-7B2DE7BEA8C6   表单KEY_ /PAGES/HOLIDAY/HOLIDAYMANAGE/HOLIDAYFORMHANDLE.JSP
--历史任务ID_ 74   流程实例ID_ 56  执行实例ID_ 56  流程定义ID_ _3701622B-4133-7B3D-F50F-E14B4F21E847:1:55  任务节点ID_ USERTASK04A84BE1-1133-7B3D-F511-1D0B7BB0A668   表单KEY_ /PAGES/HOLIDAY/HOLIDAYMANAGE/HOLIDAYFORMVIEW.JSP
SELECT T.*, T.ROWID FROM ACT_HI_TASKINST T;
 
--所有活动节点历史任务表
--历史任务ID_58 流程定义ID_ _3701622B-4133-7B3D-F50F-E14B4F21E847:1:55 流程实例ID_ 56  流程执行ID_ 56  任务节点ID_ STARTEVENT52B3145F-C133-7B3D-F50F-E6D48BA60EAE
--历史任务ID_67 流程定义ID_ _3701622B-4133-7B3D-F50F-E14B4F21E847:1:55 流程实例ID_ 56  流程执行ID_ 56  任务节点ID_ USERTASKE736BEF8-4133-7B3D-F510-7B2DE7BEA8C6   任务ID_ 68
--历史任务ID_73 流程定义ID_ _3701622B-4133-7B3D-F50F-E14B4F21E847:1:55 流程实例ID_ 56  流程执行ID_ 56  任务节点ID_ USERTASK04A84BE1-1133-7B3D-F511-1D0B7BB0A668   任务ID_ 74
SELECT T.*, T.ROWID FROM ACT_HI_ACTINST T;
 
----------------------------------------流程变量信息--------------------------------------------------
--正在执行的流程变量信息
SELECT T.*, T.ROWID FROM ACT_RU_VARIABLE T;
 
--历史流程变量信息 存放历史表单重要信息
--流程实例ID_ 56  执行实例ID_ 56 任务ID_
SELECT T.*, T.ROWID FROM ACT_HI_VARINST T;
 
 
-------------------------------------------历史意见信息-----------------------------------------------
 
--历史审批意见表
--任务ID_ 68  流程定义ID_ 56 
SELECT T.*, T.ROWID FROM ACT_HI_COMMENT T;
 
-----------------------------------------节点参与者信息-------------------------------------------------
--任务办理人表(个人任务、组任务)
SELECT T.*, T.ROWID FROM ACT_RU_IDENTITYLINK T;
 
--历史任务办理人表(个人任务、组任务)
SELECT T.*, T.ROWID FROM ACT_HI_IDENTITYLINK T;
 
--临时对象
SELECT T.*, T.ROWID FROM EA_IST.IST_APPR_BUSI_DATA T

三.Activiti与spring整合xml配置文件

向spring配置文件中,添加一个spring-workflow2-activiti.xml配置文件,里面如下:

复制代码
<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!--工作流引擎配置 -->
    <bean id="processEngineConfiguration" class="com.shine.workflow2.spring.ShineSpringProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseSchemaUpdate" value="false" />
        <property name="jobExecutorActivate" value="false" />
         <!-- 组织机构适配  -->
        <property name="organizationConnector"  ref="organizationAdapter" />

     </bean>
     
      <!-- 组织机构适配实现  -->
     <bean id="organizationAdapter" class="com.shine.workflow2.organization.impl.OrganizationAdapter" />

    <!--工作流引擎 -->
    <bean id="processEngine" class="com.shine.workflow2.spring.ShineProcessEngineFactoryBean"> 
        <property name="processEngineConfiguration" ref="processEngineConfiguration" />
    </bean>
    
    
    <!--扩展服务 -->
    <bean id="processDefinitionService" factory-bean="processEngine"
        factory-method="getProcessDefinitionService" />
    <bean id="processLogService" factory-bean="processEngine"
        factory-method="getProcessLogService" />
    <bean id="processManagementService" factory-bean="processEngine"
        factory-method="getProcessManagementService" />
    
    <!--原生服务 -->
    <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="formService" factory-bean="processEngine"
        factory-method="getFormService" />
    <bean id="identityService" factory-bean="processEngine"
        factory-method="getIdentityService" />
    
</beans>
复制代码

四.Activiti工作流常用service 

  1.BaseProcessService

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
/**
 *
 */
package com.shine.eosp.workflow2.common.process;
 
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.task.TaskDefinition;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
 
import com.shine.workflow2.exception.WorkflowException;
 
/**
 *
 * 类说明: 常用工作流核心操作封装 .
 *
 * <pre>
 * 修改日期          修改人              修改原因
 * 2016-6-2     hongwz         新建
 * </pre>
 */
public interface BaseProcessService {
     
     
    /**
     * 方法说明 : 根据流程定义Key查询最新流程定义.
     *
     * @param processDefinitionKey
     * @throws WorkflowException
     */
    public ProcessDefinition findLatestProcessDefinitionByPrcDefKey(String processDefinitionKey) throws WorkflowException;
     
    /**
     * 方法说明 : 根据流程定义Id查询流程定义.
     *
     * @param processDefinitionId  流程定义Id
     * @throws WorkflowException
     */
    public ProcessDefinitionEntity findProcessDefinitionEntityByProcDefId(String processDefinitionId) throws WorkflowException;
         
    /**
     *
     * 方法说明  : 根据流程实例Id查询流程实例.
     *
     * @param processInstanceId 流程实例Id
     * @return
     * @throws WorkflowException
     */
    public ProcessInstance findProcessInstanceByProcInst(String processInstanceId) throws WorkflowException;
     
    /**
     * 根据流程实例Id查询流程实例.
     *
     * @param processInstanceId
     * @throws WorkflowException
     */
    public Execution findExecutionByProcInst(String processInstanceId) throws WorkflowException;
     
    /**
     * 方法说明 : 根据流程实例Id查询任务.
     *
     * @param processInstanceId 流程实例Id
     * @throws WorkflowException
     */
    public Task findTaskByProcInstId(String processInstanceId) throws WorkflowException;
     
    /**
     * 方法说明 : 根据实例Id查询任务.
     *
     * @param executionId 实例Id
     * @throws WorkflowException
     */
    public Task findTaskByExecutionId(String executionId) throws WorkflowException;
     
    /**
     * 方法说明 : 根据活动节点查询任务定义.
     *
     * @param activityImpl  活动节点
     * @throws WorkflowException
     */
    public TaskDefinition findTaskDefinitionByActivityImpl(ActivityImpl activityImpl) throws WorkflowException;
     
    /**
     * 方法说明 : 查询上一个节点.
     *
     * @param activityImpl 活动节点
     * @param activityId 当前活动节点ID 
     * @param elString
     * @throws ShineException
     */
    public TaskDefinition beforeTaskDefinition(ActivityImpl activityImpl,String activityId, String elString) throws WorkflowException;
     
    /**
     * 方法说明 : 查询下一个节点.
     *
     * @param activityImpl 活动节点
     * @param activityId 当前活动节点ID 
     * @param elString
     * @throws ShineException
     */
    public TaskDefinition nextTaskDefinition(ActivityImpl activityImpl,String activityId, String elString) throws WorkflowException;
 
    /**
     * 方法说明: 根据活动节点、活动线路查询线路的连接线.
     *
     * @throws WorkflowException
     */
    public PvmActivity findPvmActivity(ActivityImpl activityImpl, String transitions) throws WorkflowException;
     
    /**
     * 方法说明 :根据流程定义Id查询任务定义
     *
     * @param processDefinitionId 流程定义Id
     * @return
     * @throws WorkflowException
     */
    public TaskDefinition findTaskDefinition(String processDefinitionId) throws WorkflowException;
     
    /**
     * 方法说明 : 添加任务意见.
     *
     * @param taskId      任务Id
     * @param processInstanceId   流程实例Id
     * @param comment     意见
     * @throws WorkflowException
     */
    public void addTaskComment(String taskId, String processInstanceId, String comment) throws WorkflowException;
     
    /**
     * 方法说明 : 拾取任务.
     *
     * @param taskId  任务Id
     * @param operator 办理人
     * @throws WorkflowException
     */
    public void claimTask(String taskId, String operator) throws WorkflowException;
     
    /**
     * 方法说明 : 根据流程定义Id查询最新流程定义.
     *
     * @param processDefinitionId 流程定义Id
     * @return
     * @throws WorkflowException
     */
    public ProcessDefinition findProcessDefinitionByPrcDefId(String processDefinitionId) throws WorkflowException;
}

2.BaseProcessServiceImpl

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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
 *
 */
package com.shine.eosp.workflow2.common.process;
 
import java.util.Iterator;
import java.util.List;
 
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.task.TaskDefinition;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
 
import com.shine.util.CollectionUtil;
import com.shine.workflow2.exception.WorkflowException;
 
/**
 *
 * 类说明: 常用工作流核心操作封装 .
 *
 * <pre>
 * 修改日期          修改人              修改原因
 * 2016-6-2     hongwz         新建
 * </pre>
 */
public class BaseProcessServiceImpl implements BaseProcessService {
     
    /**
     * log.
     */
    private static Logger logger = LoggerFactory.getLogger(BaseProcessServiceImpl.class);
        
    @Autowired
    private RepositoryService repositoryService;
     
    @Autowired
    private RuntimeService runtimeService;
     
    @Autowired
    private TaskService taskService;
     
    @Autowired
    private HistoryService historyService;
     
    /**
     * 方法说明 : 根据流程定义Key查询最新流程定义.
     *
     * @param processDefinitionKey  流程定义Key
     * @return
     * @throws WorkflowException
     */
    @Override
    public ProcessDefinition findLatestProcessDefinitionByPrcDefKey(String processDefinitionKey) throws WorkflowException{
         
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                                                               .processDefinitionKey(processDefinitionKey)
                                                               .latestVersion()
                                                               .singleResult();
        return processDefinition;
                                 
    }
     
    /**
     * 方法说明 : 根据流程定义Id查询最新流程定义.
     *
     * @param processDefinitionId 流程定义Id
     * @return
     * @throws WorkflowException
     */
    @Override
    public ProcessDefinition findProcessDefinitionByPrcDefId(String processDefinitionId) throws WorkflowException{
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(processDefinitionId)
                .orderByProcessDefinitionVersion()
                .desc()
                .singleResult();
         
        return processDefinition;
    }
     
    /**
     * 方法说明 : 根据流程定义Id查询流程定义.
     *
     * @param processDefinitionId  流程定义Id
     * @return 
     * @throws WorkflowException
     */
    @Override
    public ProcessDefinitionEntity findProcessDefinitionEntityByProcDefId(String processDefinitionId) throws WorkflowException{
         
        ProcessDefinitionEntity processDefinitionEntity  = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
                                                                  .getDeployedProcessDefinition(processDefinitionId);
        return processDefinitionEntity;
    }
     
    /**
     *
     * 方法说明  : 根据流程实例Id查询流程实例.
     *
     * @param processInstanceId 流程实例Id
     * @return
     * @throws WorkflowException
     */
    @Override
    public ProcessInstance findProcessInstanceByProcInst(String processInstanceId) throws WorkflowException{
        return runtimeService.createProcessInstanceQuery()
                             .processInstanceId(processInstanceId)
                             .singleResult();
    }
     
     
    /**
     * 根据流程实例Id查询流程实例.
     *
     * @param processInstanceId
     * @return
     * @throws WorkflowException
     */
    @Override
    public Execution findExecutionByProcInst(String processInstanceId) throws WorkflowException{
        return runtimeService.createExecutionQuery().processInstanceId(processInstanceId).singleResult();
    }
     
    /**
     * 方法说明 : 根据流程实例Id查询任务.
     *
     * @param processInstanceId 流程实例Id
     * @return
     * @throws WorkflowException
     */
    @Override
    public Task findTaskByProcInstId(String processInstanceId) throws WorkflowException{
        return taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
    }
     
    /**
     * 方法说明 : 根据实例Id查询任务.
     *
     * @param executionId 实例Id
     * @return
     * @throws WorkflowException
     */
    @Override
    public Task findTaskByExecutionId(String executionId) throws WorkflowException{
        return taskService.createTaskQuery().executionId(executionId).singleResult();
    }
     
    /**
     * 方法说明 : 根据活动节点查询任务定义.
     *
     * @param activityImpl  活动节点
     * @return
     * @throws WorkflowException
     */
    @Override
    public TaskDefinition findTaskDefinitionByActivityImpl(ActivityImpl activityImpl) throws