Activiti6.0对于trigger、signal、message的区别于测试

本文详细介绍了Activiti流程引擎中的三种触发机制:trigger、signal和message的配置与测试方法。通过具体的流程定义文件和测试代码示例,展示了如何在流程中使用这些触发机制来控制流程的执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

trigger的配置与测试文件内容如下

  • 流程定义文件:
<process id="my-process">
    <startEvent id="start"/>
    <sequenceFlow sourceRef="start" targetRef="someTask" id="flow1"/>
    <receiveTask id="someTask" />
    <endEvent id="end"/>
    <sequenceFlow id="flow3" sourceRef="someTask" targetRef="end"/>
</process>
  • 测试代码如下:
@Test
	@Deployment(resources = {"receiveTask.bpmn20.xml"})
	public void testTrigger() {

		RuntimeService runtimeService = activitiRule.getRuntimeService();
		//启动流程
		runtimeService.startProcessInstanceByKey("my-process");

		Execution task = runtimeService.createExecutionQuery().activityId("someTask").singleResult();
		LOGGER.info("task = {}", task);

		//执行触发器之后就会完成
		runtimeService.trigger(task.getId());
		task = runtimeService.createExecutionQuery().activityId("someTask").singleResult();
		LOGGER.info("task = {}", task);
	}

signal的配置与测试

  • 流程定义文件:
<signal id="signalStart" name="my-signal"/>
    <process id="my-process">
    <startEvent id="start"/>
    <sequenceFlow sourceRef="start" targetRef="signal-received" id="flow1"/>
    <intermediateCatchEvent id="signal-received">
        <signalEventDefinition signalRef="signalStart"/>
    </intermediateCatchEvent>
    <endEvent id="end"/>
    <sequenceFlow id="flow3" sourceRef="signal-received" targetRef="end"/>
</process>
  • 测试类:
@Test
	@Deployment(resources = {"my-process-signal.bpmn20.xml"})
	public void testSignalEventRecrived() {

		RuntimeService runtimeService = activitiRule.getRuntimeService();
		//启动流程
		runtimeService.startProcessInstanceByKey("my-process");

		//在数据库中是否存在信号执行流
		Execution execution = runtimeService
				.createExecutionQuery().signalEventSubscriptionName("my-signal").singleResult();
		LOGGER.info("execution = {}", execution);

		//触发信息向后执行
		runtimeService.signalEventReceived("my-signal");
		execution = runtimeService
				.createExecutionQuery().signalEventSubscriptionName("my-signal").singleResult();
		LOGGER.info("execution = {}", execution);
	}

Message的配置与测试文件内容如下(message有两种配置,一种是类似于signal那样配置,一种是放在start里面,可以通过messageId进行启动流程)

  • 通过message启动的流程配置
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<message id="messageStart" name="my-message"/>
    <process id="my-process">
    <startEvent id="start"/>
    <sequenceFlow sourceRef="start" targetRef="message-received" id="flow1"/>
    <intermediateCatchEvent id="message-received">
        <messageEventDefinition messageRef="messageStart"/>
    </intermediateCatchEvent>
    <endEvent id="end"/>
    <sequenceFlow id="flow3" sourceRef="message-received" targetRef="end"/>
</process>
</definitions>
  • 测试类
@Test
	@Deployment(resources = {"my-process-message.bpmn20.xml"})
	public void testMessage() {

		RuntimeService runtimeService = activitiRule.getRuntimeService();
		runtimeService.startProcessInstanceByKey("my-process");

		Execution execution = runtimeService.createExecutionQuery().messageEventSubscriptionName("my-message").singleResult();
		LOGGER.info("execution = {}", execution);

		runtimeService.messageEventReceived("my-message", execution.getId());
		execution = runtimeService.createExecutionQuery().messageEventSubscriptionName("my-message").singleResult();
		LOGGER.info("execution = {}", execution);
	}
  • 基于message启动的xml配置
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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"
             typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"
             targetNamespace="http://www.activiti.org/test">
    <message id="messageStart" name="my-message"/>
    <process id="my-process">
        <startEvent id="start">
            <messageEventDefinition messageRef="messageStart"/>
        </startEvent>
        <sequenceFlow sourceRef="start" targetRef="someTask" id="flow1"/>
        <userTask id="someTask"/>
        <endEvent id="end"/>
        <sequenceFlow id="flow3" sourceRef="someTask" targetRef="end"/>
    </process>
</definitions>
  • 测试类:
@Test
	@Deployment(resources = {"my-process-message-send.bpmn20.xml"})
	public void testMessageStart() {

		RuntimeService runtimeService = activitiRule.getRuntimeService();
		ProcessInstance processInstance = runtimeService.startProcessInstanceByMessage("my-message");
		LOGGER.info("processInstance = {}", processInstance);
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值