2023年11月29日发(作者:)

activiti学习之事件⼦流程

写在前⾯

事件⼦流程⼀般和错误开始节点⼀起使⽤,当主流程中发⽣了特定的错误后触发,开始执⾏⼦流

⼀种特殊的开始节点,发⽣了指定的错误才开始

程,从xml的结构上来看其实就是多了⼀个属性,如下是普通开始节点和错误开始节点的⽐较:

errorEventDefinition

1

2

3

4

5

<startEvent id="startevent2" name="Start">startEvent>

<startEvent id="errorstartevent1" name="Error start">

<errorEventDefinition errorRef="error1">errorEventDefinition>

startEvent>

下⾯⼀起来看下。

1:测试

1.1:效果图

如下是错误开始节点的定义:

如下是主流程服务节点的类定义:

任务执⾏

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

public class ErrorStartRunDelegate implements JavaDelegate {

@Override

public void execute(DelegateExecution execution) {

System.out.println("开始检查2222端⼝");

try {

Socket socket = new Socket("127.0.0.1", 8223);

//socket = new Socket("127.0.0.1", 2222);

System.out.println("端⼝检查完成");

} catch (Exception e) {

System.out.println("检查异常");

throw new BpmnError("error");

}

}

}

如下是事件⼦流程中服务节点的类定义:

错误处理

1

2

3

4

5

6

7

8

public class ErrorDelegate implements JavaDelegate {

@Override

public void execute(DelegateExecution execution) {

System.out.println("错误处理执⾏...");

}

}

xml

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

<definitions xmlns="/spec/BPMN/20100524/MODEL" xsi="/2001/XMLSchema-instance" xsd="www.w

<startEvent id="startevent2" name="Start">startEvent>

<serviceTask id="servicetask2" name="任务执⾏" class="tartRunDelegate">serviceTask>

<endEvent id="endevent2" name="End">endEvent>

<sequenceFlow id="flow4" sourceRef="servicetask2" targetRef="endevent2">sequenceFlow>

<subProcess id="eventsubprocess1111" name="Event sub Process111" triggeredByEvent="true">

<startEvent id="errorstartevent1" name="Error start">

<errorEventDefinition errorRef="error">errorEventDefinition>

startEvent>

<serviceTask id="servicetask3" name="错误处理" class="elegate">serviceTask>

<endEvent id="endevent3" name="End">endEvent>

<sequenceFlow id="flow5" sourceRef="servicetask3" targetRef="endevent3">sequenceFlow>

<sequenceFlow id="flow7" sourceRef="errorstartevent1" targetRef="servicetask3">sequenceFlow>

subProcess>

process>

<BPMNDiagram id="BPMNDiagram_myErrorStartEventProcess">

<BPMNPlane bpmnElement="myErrorStartEventProcess" id="BPMNPlane_myErrorStartEventProcess">

<BPMNShape bpmnElement="startevent2" id="BPMNShape_startevent2">

<Bounds height="35.0" width="35.0" x="250.0" y="340.0">Bounds>

BPMNShape>

<BPMNShape bpmnElement="servicetask2" id="BPMNShape_servicetask2">

<Bounds height="55.0" width="105.0" x="450.0" y="330.0">Bounds>

BPMNShape>

<BPMNShape bpmnElement="endevent2" id="BPMNShape_endevent2">

<Bounds height="35.0" width="35.0" x="710.0" y="340.0">Bounds>

BPMNShape>

<BPMNShape bpmnElement="eventsubprocess1111" id="BPMNShape_eventsubprocess1111">

<Bounds height="221.0" width="591.0" x="200.0" y="60.0">Bounds>

BPMNShape>

<BPMNShape bpmnElement="errorstartevent1" id="BPMNShape_errorstartevent1">

<Bounds height="35.0" width="35.0" x="240.0" y="140.0">Bounds>

BPMNShape>

<BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3">

<Bounds height="55.0" width="105.0" x="400.0" y="130.0">Bounds>

BPMNShape>

<BPMNShape bpmnElement="endevent3" id="BPMNShape_endevent3">

<Bounds height="35.0" width="35.0" x="600.0" y="140.0">Bounds>

BPMNShape>

<BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">

<waypoint x="285.0" y="357.0">waypoint>

<waypoint x="450.0" y="357.0">waypoint>

BPMNEdge>

<BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">

<waypoint x="555.0" y="357.0">waypoint>

<waypoint x="710.0" y="357.0">waypoint>

BPMNEdge>

<BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">

<waypoint x="505.0" y="157.0">waypoint>

<waypoint x="600.0" y="157.0">waypoint>

BPMNEdge>

<BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">

<waypoint x="275.0" y="157.0">waypoint>

<waypoint x="400.0" y="157.0">waypoint>

BPMNEdge>

BPMNPlane>

BPMNDiagram>

<sequenceFlow id="flow3" sourceRef="startevent2" targetRef="servicetask2">sequenceFlow>

<process id="myErrorStartEventProcess" name="myErrorStartEventProcess" isExecutable="true">

definitions>

1.2:部署流程

1

2

3

4

5

6

7

8

9

@Test

public void deploy() {

Deployment deployment = repositoryService.createDeployment() //

创建部署

.addClasspathResource("com/jh/activiti/error_start_") //

加载流程资源⽂件

.name("⼦流程中错误开始节点09211118") //

流程名称

.deploy(); //

部署

System.out.println("流程部署ID:" + deployment.getId());

System.out.println("流程部署Name:" + deployment.getName());

}

1.3:启动流程实例

1

2

3

4

5

6

7

8

9

10

11

12

13

/**

*

启动流程实例

*/

@Test

public void start() throws InterruptedException {

// 2222

创建实例,⾃动开始执⾏主流程,在主流程的服务节点检查端⼝占⽤,

//

发现占⽤,进⼊⼦流程中的错误开始节点,开始执⾏⼦流程,然后流程结束

ProcessInstance pi = runtimeService.startProcessInstanceByKey("myErrorStartEventProcess"); // KEY

流程定义表的字段值

/*

n("ID:" + ());

流程实例

n("ID:" + cessDefinitionId());

流程定义

*/

}

输出如下:

1

2

3

开始检查2222端⼝

检查异常

错误处理执⾏...