DelegateExecution和EXecution有啥区别

DelegateExecution 和 Execution 都是 Activiti 引擎中用于表示流程实例的执行对象,但它们在具体使用上有一些区别:

DelegateExecution

接口:DelegateExecution 是一个接口,继承自 VariableScope 接口。它主要在扩展点(如执行监听器、任务监听器、服务任务)中使用,提供了一些额外的方法来操作流程实例和变量。

使用场景:通常在编写自定义的业务逻辑时使用,例如实现 ExecutionListener 或 JavaDelegate 接口时。

功能:提供了丰富的方法来访问流程实例的状态、获取和设置流程变量、获取当前活动信息等。

Execution

实现类:Execution 是一个具体的类,代表一个正在执行的流程实例或者子流程。它是一个较低级别的 API,主要在 Activiti 引擎内部使用。

使用场景:一般不直接在业务逻辑中使用,而是在引擎内部进行流程实例的执行和控制。

功能:提供了基本的执行上下文信息,如流程实例 ID、父执行 ID、活动 ID 等。

主要区别

1. 抽象程度

• DelegateExecution 是接口,更高层次,提供给用户在扩展点上进行操作。

• Execution 是具体的实现类,主要用于引擎内部处理。

2. 使用方式

• DelegateExecution 通常在实现 ExecutionListener、TaskListener 或 JavaDelegate 接口时使用。

• Execution 一般不直接用于业务逻辑中,而是由引擎内部使用。

示例对比

使用 DelegateExecution:

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;


public class MyExecutionListener implements ExecutionListener {


    @Override
    public void notify(DelegateExecution execution) {
        // 使用 DelegateExecution 获取和设置流程变量
        String myVar = (String) execution.getVariable("myVar");
        System.out.println("流程变量 myVar 的值: " + myVar);
        execution.setVariable("myVar", "newValue");
    }
}

引擎内部使用 Execution:

import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionManager;


// 引擎内部代码示例
public void someInternalMethod(CommandContext commandContext) {
    ExecutionManager executionManager = commandContext.getExecutionManager();
    ExecutionEntity execution = executionManager.findExecutionById("executionId");
    
    // 进行一些引擎内部处理
    execution.setVariable("internalVar", "value");
}

总结

• DelegateExecution 是一个接口,主要用于扩展点上进行业务逻辑操作,提供了丰富的方法来访问和操作流程实例。

• Execution 是一个具体类,主要在引擎内部使用,负责流程实例的执行和控制。

在大多数情况下,您将在编写自定义业务逻辑时使用 DelegateExecution,而不是直接使用 Execution。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值