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
/**
* setValiable 与 setLocalValiable作用域不同
*/
@Test
public void deploeyFlow() {
String folderPath = "study/activiti/diagrams/sequenceFlow";
HashMap<String, String> map = new HashMap<>();
map.put("name", "连线流程");// 名称
map.put("id", "sequenceFlow");// id
map.put("category", "办公流程");// 类别
activitiService.devlopActiviti(folderPath, map);

}

@Test
public void startActiviti() {
String processDefinitionKey = "sequenceFlow";
String orgCode = "070067801";
// 设置变量
HashMap<String, Object> map = new HashMap<>();
map.put("createLoginName", "0003");
map.put("orgCode", orgCode);
activitiService.startActiviti(processDefinitionKey, map);

}

此时没有完成第一个任务。


设置变量并获取变量


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
@Test
public void setVariable() {
// String excutionId = "25001";
String taskId = "10008";
HashMap<String, Object> map = new HashMap<>();
map.put("message", "重要");
boolean isLocal = true;
// activitiService.setVariableByExcutionId(excutionId, isLocal, map);
activitiService.setVariableByTaskId(taskId, isLocal, map);

}
@Test
public void getVariable() {
// String excutionId = "10001";
String taskId = "10008";
Map<String, Object> map = new HashMap<>();

boolean isLocal = false;
//map = activitiService.getVariableByExcutionId(excutionId, isLocal );
map = activitiService.getVariableByTaskId(taskId, isLocal);
LOGGER.info("message【{}】",(String)map.get("message"));
//SequenceFlow sequenceFlow = activitiService.getVariableByTaskId(taskId, "sequenceFlow", SequenceFlow.class);

// LOGGER.info("创建时间【{}】", sequenceFlow.getDate());
}
@Override
public boolean setVariableByTaskId(String taskId, boolean isLocal, HashMap<String, Object> map) {
/*
* taskService.setVariable(taskId, variableName, value);
*
* 设置本执行对象的变量,该作用域只在当前的executionId中 taskService.setVariableLocal(taskId,
* variableName, value); 可以设置对个变量,放在map中
*/
if (isLocal) {
taskService.setVariablesLocal(taskId, map);
} else {
taskService.setVariables(taskId, map);
}
return true;
}
@Override
public Map<String, Object> getVariableByTaskId(String taskId, boolean isLocal) {
Map<String, Object> variablesMap = new HashMap<String, Object>();
if (isLocal) {
variablesMap = taskService.getVariablesLocal(taskId);
} else {
variablesMap = taskService.getVariables(taskId);
}
return variablesMap;
}

日志


1
2
[20:39:24:795] [INFO] - wsylp.log.MyLog.logAfter(MyLog.java:29) - @AfterReturning:wsylp.service.ActivitiService.getVariableByTaskId end.
[20:39:24:795] [INFO] - serviceTest.ActivitiVariableTest.getVariable(ActivitiVariableTest.java:172) - message【重要】

设置变量为对象


如果变量为对象需要实现序列化接口

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
public class SequenceFlow implements Serializable {

private static final long serialVersionUID = -1164661902871465047L;
private String message;//信息(重要,不重要)
private String createLoginName;// 申请人
private Date date;// 申请日期

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getCreateLoginName() {
return createLoginName;
}

public void setCreateLoginName(String createLoginName) {
this.createLoginName = createLoginName;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

}

设置变量

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
/**
* 设置流程变量值
*/
@Test
public void setVariablesByBean() {
String taskId = "10008";

/**
* 支持的变量类型
*
* <br>
* 简单类型:String boolean Integer double date <br>
* 自定义对象bean
*/
// 传递的是一个自定义的bean对象
SequenceFlow sequenceFlow = new SequenceFlow();
sequenceFlow.setCreateLoginName("0003");
sequenceFlow.setDate(new Date());
sequenceFlow.setMessage("重要");
activitiService.setVariableByTaskId(taskId, "sequenceFlow", sequenceFlow);

}

@Test
public void getVariable() {
// String excutionId = "10001";
String taskId = "10008";
Map<String, Object> map = new HashMap<>();

boolean isLocal = false;
//map = activitiService.getVariableByExcutionId(excutionId, isLocal );
// map = activitiService.getVariableByTaskId(taskId, isLocal);
//LOGGER.info("message【{}】",(String)map.get("message"));
SequenceFlow sequenceFlow = activitiService.getVariableByTaskId(taskId, "sequenceFlow", SequenceFlow.class);

LOGGER.info("创建时间【{}】", sequenceFlow.getDate());
}

日志


1
2
[20:45:47:841] [INFO] - wsylp.log.MyLog.logAfter(MyLog.java:29) - @AfterReturning:wsylp.service.ActivitiService.getVariableByTaskId end.
[20:45:47:841] [INFO] - serviceTest.ActivitiVariableTest.getVariable(ActivitiVariableTest.java:175) - 创建时间【2018-06-17T20:45:24.240+0800

由于此时串的message为重要,那么下一个审批人为总经理


完成任务并查询


由于变量设置在sequenceFlow上,尽量设置变量为全局的。或者设置在执行对象上。

1
[20:53:50:698] [INFO] - wsylp.service.impl.ActivitiServiceImpl.finshTask(ActivitiServiceImpl.java:148) - 完成任务【10008

github地址:https://github.com/wsylp/gms.git

本文标题:activiti初体验(五) 变量设置与获取

文章作者:wsylp

发布时间:2018年06月01日 - 13:06

最后更新:2020年01月02日 - 10:01

原始链接:http://wsylp.top/2018/06/01/activiti初体验-五-变量设置与获取/

许可协议: 本文为 wsylp 版权所有 转载请保留原文链接及作者。

-------------本文结束感谢阅读-------------