Activiti6

编程入门 行业动态 更新时间:2024-10-11 23:26:55

Activiti6

Activiti6

文章目录

  • 1. 开始事件
    • 1.1 简介
  • 2. 空开始事件
    • 2.1 简介
  • 3.定时器开始事件
    • 3.1 简介
    • 3.2 流程设计
  • 4. 消息开始事件
    • 4.1 简介
    • 4.2 流程设计
  • 5. 错误开始事件
    • 5.1 简介
    • 5.2 流程设计

1. 开始事件

版权声明:本文部分为CSDN博主「司马缸砸缸了」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接

1.1 简介

开始事件用来指明流程在哪里开始,分为空开始事件,消息开始事件,信号开始事件,定时器开始事件,错误开始事件。

2. 空开始事件

2.1 简介

空开始事件意味着没有指定启动流程实例的触发条件。
启动方式如下:

ProcessInstance processInstance = runtimeService.startProcessInstanceByXXX();

之前所有事例都是空开始事件,就不在实践。

3.定时器开始事件

3.1 简介

定时器开始事件当符合时间条件时触发。常用场景,例如:定时查看系统运行情况。

3.2 流程设计

场景:系统每五秒执行一次任务。


流程文件bpmn

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="" xmlns:activiti="" xmlns:bpmndi="" xmlns:omgdc="" xmlns:omgdi="" xmlns:tns="" xmlns:xsd="" xmlns:xsi="" expressionLanguage="" id="m1596072377817" name="" targetNamespace="" typeLanguage=""><process id="start" isClosed="false" isExecutable="true" name="Start" processType="None"><startEvent id="_2" name="开始"><timerEventDefinition id="_2_ED_1"><timeCycle><![CDATA[*/5 * * * * ?  ]]></timeCycle></timerEventDefinition></startEvent><serviceTask activiti:class="com.yb.activiti6.delegate.NewStartDelegate" activiti:exclusive="true" id="_3" name="任务执行"/><endEvent id="_4" name="结束"/><sequenceFlow id="_5" sourceRef="_2" targetRef="_3"/><sequenceFlow id="_6" sourceRef="_3" targetRef="_4"/></process><bpmndi:BPMNDiagram documentation="background=#000000;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram"><bpmndi:BPMNPlane bpmnElement="start"><bpmndi:BPMNShape bpmnElement="_2" id="Shape-_2"><omgdc:Bounds height="32.0" width="32.0" x="70.0" y="340.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="_3" id="Shape-_3"><omgdc:Bounds height="55.0" width="85.0" x="320.0" y="330.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="_4" id="Shape-_4"><omgdc:Bounds height="32.0" width="32.0" x="650.0" y="340.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="_5" id="BPMNEdge__5" sourceElement="_2" targetElement="_3"><omgdi:waypoint x="102.0" y="356.0"/><omgdi:waypoint x="320.0" y="357.5"/><bpmndi:BPMNLabel><omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6" sourceElement="_3" targetElement="_4"><omgdi:waypoint x="405.0" y="357.5"/><omgdi:waypoint x="650.0" y="356.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>

测试类

package com.yb.activiti6;import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.io.IOException;/*** @version 1.0* @date 2020/7/23*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivitiDemoTest15 {@Autowiredprivate RuntimeService runtimeService;@Autowiredprivate TaskService taskService;@Autowiredprivate IdentityService identityService;@Autowiredprivate RepositoryService repositoryService;@Autowiredprivate ProcessEngine processEngine;@Autowiredprivate HistoryService historyService;/*** 发布流程,测试定时器开始事件* @throws IOException*/@Testpublic void deploymentProcesses_zip(){Deployment deploy = repositoryService.createDeployment().name("测试-定时器开始事件-1")//创建流程名称.addClasspathResource("processes/activiti_start.bpmn")//指定zip完成部署.addClasspathResource("processes/activiti_start.png").deploy();System.out.println("部署id:"+deploy.getId());System.out.println("部署名称:"+deploy.getName());//睡一会try {Thread.sleep(1000*30);} catch (InterruptedException e) {e.printStackTrace();}}
}

运行效果:

流程每五秒启动一次

4. 消息开始事件

4.1 简介

runtimeService.startProcessByMessage("abc");

4.2 流程设计

场景:系统以发送消息的方式执行一次任务

流程文件bpmn

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="" xmlns:activiti="" xmlns:bpmndi="" xmlns:omgdc="" xmlns:omgdi="" xmlns:tns="" xmlns:xsd="" xmlns:xsi="" expressionLanguage="" id="m1596074402033" name="" targetNamespace="" typeLanguage=""><message id="msg" name="msg"/><process id="start2" isClosed="false" isExecutable="true" processType="None"><startEvent id="_2" name="开始"><messageEventDefinition  messageRef="msg"/></startEvent><serviceTask activiti:class="com.yb.activiti6.delegate.NewStartDelegate" activiti:exclusive="true" id="_3" name="任务执行"/><endEvent id="_4" name="结束"/><sequenceFlow id="_5" sourceRef="_2" targetRef="_3"/><sequenceFlow id="_6" sourceRef="_3" targetRef="_4"/></process><bpmndi:BPMNDiagram documentation="background=#000000;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram"><bpmndi:BPMNPlane bpmnElement="start2"><bpmndi:BPMNShape bpmnElement="_2" id="Shape-_2"><omgdc:Bounds height="32.0" width="32.0" x="10.0" y="90.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="_3" id="Shape-_3"><omgdc:Bounds height="55.0" width="85.0" x="325.0" y="80.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="_4" id="Shape-_4"><omgdc:Bounds height="32.0" width="32.0" x="675.0" y="90.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="_5" id="BPMNEdge__5" sourceElement="_2" targetElement="_3"><omgdi:waypoint x="42.0" y="106.0"/><omgdi:waypoint x="325.0" y="107.5"/><bpmndi:BPMNLabel><omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6" sourceElement="_3" targetElement="_4"><omgdi:waypoint x="410.0" y="107.5"/><omgdi:waypoint x="675.0" y="106.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>

测试类

package com.yb.activiti6;import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.io.IOException;/*** @version 1.0* @date 2020/7/23*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivitiDemoTest16 {@Autowiredprivate RuntimeService runtimeService;@Autowiredprivate TaskService taskService;@Autowiredprivate IdentityService identityService;@Autowiredprivate RepositoryService repositoryService;@Autowiredprivate ProcessEngine processEngine;@Autowiredprivate HistoryService historyService;/*** 发布流程,测试消息开始事件* @throws IOException*/@Testpublic void deploymentProcesses_zip(){Deployment deploy = repositoryService.createDeployment().name("测试-消息开始事件-1")//创建流程名称.addClasspathResource("processes/activiti_start2.bpmn")//指定zip完成部署.addClasspathResource("processes/activiti_start2.png").deploy();System.out.println("部署id:"+deploy.getId());System.out.println("部署名称:"+deploy.getName());}/*** 启动流程,测试消息开始事件*/@Testpublic void startProcess(){//可根据id,key,message启动流程ProcessInstance msg = runtimeService.startProcessInstanceByMessage("msg");System.out.println("流程实例id:"+msg.getId());System.out.println("流程定义id:"+msg.getProcessDefinitionId());}
}

启动任务效果:

5. 错误开始事件

5.1 简介

错误开始事件可以用来触发一个事件子流程

5.2 流程设计

场景:检查端口占用情况


任务执行类

package com.yb.activiti6.delegate;
import org.activiti.engine.delegate.BpmnError;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import java.Socket;
/*** @version 1.0* @date 2020/7/30*/
public class ErrorStartDelegate implements JavaDelegate {@Overridepublic void execute(DelegateExecution delegateExecution) {System.out.println("开始检查2222端口");try {Socket socket = new Socket("127.0.0.1", 2222);System.out.println("端口检查完成");} catch (Exception e) {System.out.println("检查异常");throw new BpmnError("error");}}
}

错误处理类

package com.yb.activiti6.delegate;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
/*** @version 1.0* @date 2020/7/30*/
public class ErrorDelegate implements JavaDelegate  {@Overridepublic void execute(DelegateExecution delegateExecution) {System.out.println("错误处理执行.......");}
}

流程文件bpmn

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="" xmlns:activiti="" xmlns:bpmndi="" xmlns:omgdc="" xmlns:omgdi="" xmlns:tns="" xmlns:xsd="" xmlns:xsi="" expressionLanguage="" id="m1596078568885" name="" targetNamespace="" typeLanguage=""><error errorCode="error" id="ERR_1" name="error"/><process id="errorStart" isClosed="false" isExecutable="true" processType="None"><startEvent id="_2" name="开始"/><serviceTask activiti:class="com.yb.activiti6.delegate.ErrorStartDelegate" activiti:exclusive="true" id="_3" name="任务执行"/><endEvent id="_4" name="结束"/><subProcess activiti:exclusive="true" id="_5" name="SubProcess" triggeredByEvent="true"><startEvent id="_8" name="开始2"><errorEventDefinition id="_8_ED_1"/></startEvent><serviceTask activiti:class="com.yb.activiti6.delegate.ErrorDelegate" activiti:exclusive="true" id="_9" name="错误处理"/><endEvent id="_10" name="结束2"/><sequenceFlow id="_11" sourceRef="_8" targetRef="_9"/><sequenceFlow id="_12" sourceRef="_9" targetRef="_10"/></subProcess><sequenceFlow id="_6" sourceRef="_2" targetRef="_3"/><sequenceFlow id="_7" sourceRef="_3" targetRef="_4"/></process><bpmndi:BPMNDiagram documentation="background=#000000;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram"><bpmndi:BPMNPlane bpmnElement="errorStart"><bpmndi:BPMNShape bpmnElement="_2" id="Shape-_2"><omgdc:Bounds height="32.0" width="32.0" x="70.0" y="45.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="_3" id="Shape-_3"><omgdc:Bounds height="55.0" width="85.0" x="290.0" y="45.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="_4" id="Shape-_4"><omgdc:Bounds height="32.0" width="32.0" x="625.0" y="45.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="_5" id="Shape-_5" isExpanded="true"><omgdc:Bounds height="95.0" width="650.0" x="25.0" y="205.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="95.0" width="650.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="_8" id="Shape-_8"><omgdc:Bounds height="32.0" width="32.0" x="70.0" y="235.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="_9" id="Shape-_9"><omgdc:Bounds height="55.0" width="85.0" x="305.0" y="225.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="_10" id="Shape-_10"><omgdc:Bounds height="32.0" width="32.0" x="615.0" y="235.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="_12" id="BPMNEdge__12" sourceElement="_9" targetElement="_10"><omgdi:waypoint x="390.0" y="252.5"/><omgdi:waypoint x="615.0" y="251.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6" sourceElement="_2" targetElement="_3"><omgdi:waypoint x="102.0" y="61.0"/><omgdi:waypoint x="290.0" y="72.5"/><bpmndi:BPMNLabel><omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="_7" id="BPMNEdge__7" sourceElement="_3" targetElement="_4"><omgdi:waypoint x="375.0" y="72.5"/><omgdi:waypoint x="625.0" y="61.0"/><bpmndi:BPMNLabel><omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="_11" id="BPMNEdge__11" sourceElement="_8" targetElement="_9"><omgdi:waypoint x="102.0" y="251.0"/><omgdi:waypoint x="305.0" y="252.5"/><bpmndi:BPMNLabel><omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/></bpmndi:BPMNLabel></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>

测试类

package com.yb.activiti6;import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.io.IOException;/*** @version 1.0* @date 2020/7/23*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivitiDemoTest17 {@Autowiredprivate RuntimeService runtimeService;@Autowiredprivate TaskService taskService;@Autowiredprivate IdentityService identityService;@Autowiredprivate RepositoryService repositoryService;@Autowiredprivate ProcessEngine processEngine;@Autowiredprivate HistoryService historyService;/*** 发布流程,测试错误开始事件* @throws IOException*/@Testpublic void deploymentProcesses_zip(){Deployment deploy = repositoryService.createDeployment().name("测试-错误开始事件-1")//创建流程名称.addClasspathResource("processes/activiti_errorStart.bpmn")//指定zip完成部署.addClasspathResource("processes/activiti_errorStart.png").deploy();System.out.println("部署id:"+deploy.getId());System.out.println("部署名称:"+deploy.getName());}/*** 启动流程,测试错误开始事件*/@Testpublic void startProcess(){//可根据id,key,message启动流程ProcessInstance msg = runtimeService.startProcessInstanceByKey("errorStart");System.out.println("流程实例id:"+msg.getId());System.out.println("流程定义id:"+msg.getProcessDefinitionId());}
}

启动效果:

到此,开始事件测试完毕。

更多推荐

Activiti6

本文发布于:2024-03-04 21:52:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1710519.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!