Flowable源码注释(六十六)边界注册、取消、条件、变量事件活动行为类

Flowable-6.7.2 源码注释地址:https://github.com/solojin/flowable-6.7.2-annotated

BoundaryEventRegistryEventActivityBehavior 边界事件注册事件活动行为类

/**
 * 边界事件注册事件活动行为类
 * 
 * @author Tijs Rademakers
 */
public class BoundaryEventRegistryEventActivityBehavior extends BoundaryEventActivityBehavior {
   

    private static final long serialVersionUID = 1L;

    protected String eventDefinitionKey;

    public BoundaryEventRegistryEventActivityBehavior(String eventDefinitionKey, boolean interrupting) {
   
        super(interrupting);
        this.eventDefinitionKey = eventDefinitionKey;
    }

    @Override
    public void execute(DelegateExecution execution) {
   
        CommandContext commandContext = Context.getCommandContext();
        ExecutionEntity executionEntity = (ExecutionEntity) execution;

        ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);
        String eventDefinitionKey = getEventDefinitionKey(executionEntity, processEngineConfiguration);
        EventSubscriptionEntity eventSubscription = (EventSubscriptionEntity) processEngineConfiguration.getEventSubscriptionServiceConfiguration()
                .getEventSubscriptionService().createEventSubscriptionBuilder()
                        .eventType(eventDefinitionKey)
                        .executionId(executionEntity.getId())
                        .processInstanceId(executionEntity.getProcessInstanceId())
                        .activityId(executionEntity.getCurrentActivityId())
                        .processDefinitionId(executionEntity.getProcessDefinitionId())
                        .scopeType(ScopeTypes.BPMN)
                        .tenantId(executionEntity.getTenantId())
                        .configuration(CorrelationUtil.getCorrelationKey(BpmnXMLConstants.ELEMENT_EVENT_CORRELATION_PARAMETER, commandContext, executionEntity))
                        .create();
        
        CountingEntityUtil.handleInsertEventSubscriptionEntityCount(eventSubscription);
        executionEntity.getEventSubscriptions().add(eventSubscription);
    }

    @Override
    public void trigger(DelegateExecution execution, String triggerName, Object triggerData) {
   
        ExecutionEntity executionEntity = (ExecutionEntity) execution;
        BoundaryEvent boundaryEvent = (BoundaryEvent) execution.getCurrentFlowElement();
        
        Object eventInstance = execution.getTransientVariables().get(EventConstants.EVENT_INSTANCE);
        if (eventInstance instanceof EventInstance) {
   
            EventInstanceBpmnUtil
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flowable 是一个流程引擎框架,条件判断是其中非常重要的一部分。Flowable 中的条件判断主要分为两类:表达式条件判断和脚本条件判断。下面分别介绍这两种条件判断的码实现。 1. 表达式条件判断 表达式条件判断是通过表达式来判断条件是否成立。Flowable 中支持的表达式语言有 EL 表达式、Juel 表达式、Mvel 表达式等。这里以 EL 表达式为例。 首先我们看到 Flowable 中的表达式条件判断是通过 org.flowable.bpmn.model.SequenceFlow 类中的 conditionExpression 属性来实现的。该属性的类型为 String,表示一个表达式字符串。 在执行条件判断时,Flowable 会将这个表达式字符串解析成一个 EL 表达式对象,并将当前执行上下文中的变量传递给该表达式对象进行计算,最终得出判断结果。下面是相关码实现: ```java public class SequenceFlow extends FlowElement { protected String conditionExpression; // ... public boolean hasCondition() { return StringUtils.isNotEmpty(conditionExpression); } public Expression getConditionExpression() { if (hasCondition()) { ExpressionManager expressionManager = Context.getProcessEngineConfiguration().getExpressionManager(); return expressionManager.createExpression(conditionExpression); } return null; } // ... } ``` 可以看到,当 conditionExpression 属性不为空时,调用 getConditionExpression 方法会将该属性解析成一个 EL 表达式对象返回。 接下来看一下条件判断的执行过程。在 Flowable 中,条件判断是通过 org.flowable.engine.impl.bpmn.behavior.ConditionalEventBehavior 类的 execute 方法实现的。该方法中会先获取 SequenceFlow 对象的 conditionExpression 属性,然后调用 getConditionExpression 方法解析成一个 EL 表达式对象。最后将当前执行上下文中的变量传递给该表达式对象进行计算,得出判断结果。下面是相关码实现: ```java public class ConditionalEventBehavior extends FlowNodeActivityBehavior { // ... @Override public void execute(ActivityExecution execution) throws Exception { // ... SequenceFlow outgoingSequenceFlow = (SequenceFlow) conditionalEvent.getOutgoingFlows().get(0); if (outgoingSequenceFlow.hasCondition()) { Expression conditionExpression = outgoingSequenceFlow.getConditionExpression(); Object value = conditionExpression.getValue(execution); if (value instanceof Boolean && (Boolean) value) { leave(execution); } } else { leave(execution); } // ... } // ... } ``` 可以看到,当 SequenceFlow 对象的 conditionExpression 属性不为空时,会调用 getConditionExpression 方法获取一个 EL 表达式对象,并将当前执行上下文中的变量传递给该表达式对象进行计算。最终得出的判断结果为 true 时,会继续执行下一步任务,否则不会执行。 2. 脚本条件判断 脚本条件判断是通过脚本来判断条件是否成立。Flowable 中支持的脚本语言有 Groovy、JavaScript、Python 等。这里以 Groovy 为例。 在 Flowable 中,脚本条件判断是通过 org.flowable.bpmn.model.ScriptTask 类中的 script 属性来实现的。该属性的类型为 String,表示一个 Groovy 脚本字符串。 执行脚本条件判断的过程与表达式条件判断类似,在这里不再赘述。需要注意的是,在使用脚本条件判断时,需要在流程引擎配置中添加对应的脚本引擎。下面是相关码实现: ```java public class ScriptTask extends TaskWithFieldExtensions { protected String scriptFormat; protected String script; // ... public boolean hasScript() { return StringUtils.isNotEmpty(script); } public Object executeScript(VariableScope variableScope) { ScriptingEngines scriptingEngines = Context.getProcessEngineConfiguration().getScriptingEngines(); ScriptEngine scriptEngine = scriptingEngines.getScriptEngine(scriptFormat); return scriptingEngines.evaluate(script, scriptEngine, variableScope); } // ... } ``` 可以看到,当 script 属性不为空时,调用 executeScript 方法会将该属性解析成一个 Groovy 脚本对象,并将当前执行上下文中的变量传递给该脚本对象进行计算,最终得出判断结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值