Activiti6--入门学习--开始事件

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="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1596072377817" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
  <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 {
    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private HistoryService historyService;


    /**
     * 发布流程,测试定时器开始事件
     * @throws IOException
     */
    @Test
    public 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="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1596074402033" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
  <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 {
    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private HistoryService historyService;


    /**
     * 发布流程,测试消息开始事件
     * @throws IOException
     */
    @Test
    public 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());

    }

    /**
     * 启动流程,测试消息开始事件
     */
    @Test
    public 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.net.Socket;
/**
 * @version 1.0
 * @date 2020/7/30
 */
public class ErrorStartDelegate implements JavaDelegate {
    @Override
    public 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  {
    @Override
    public void execute(DelegateExecution delegateExecution) {
        System.out.println("错误处理执行.......");
    }
}

流程文件bpmn

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1596078568885" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
  <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 {
    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private HistoryService historyService;


    /**
     * 发布流程,测试错误开始事件
     * @throws IOException
     */
    @Test
    public 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());

    }

    /**
     * 启动流程,测试错误开始事件
     */
    @Test
    public void startProcess(){
        //可根据id,key,message启动流程
        ProcessInstance msg = runtimeService.startProcessInstanceByKey("errorStart");
        System.out.println("流程实例id:"+msg.getId());
        System.out.println("流程定义id:"+msg.getProcessDefinitionId());
    }
}

启动效果:
在这里插入图片描述
到此,开始事件测试完毕。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值