在业务场景中,如果需要调用其他流程,我们可以使用callactivity,通过activiti:in,我们可以将主流程的参数传递给子流程,通过activiti:out可以将子流程执行结束后的参数返回给主流程,在单实例情况下,是没有问题的。
但是,当我们调用的子流程数量是不定的,且每个子流程都不一样时,我们可以通过设置调用活动为multi-instance,将子流程运行时动态传递给工作流引擎,如下:
<callActivity id="call" name="子流程调用" calledElement="${subprocess}">
<extensionElements>
<activiti:in source="businessId" target="businessId"/>
<activiti:in source="businessTable" target="businessTable"/>
<activiti:out source="cancelled" target="cancelled"/>
</extensionElements>
<multiInstanceLoopCharacteristics isSequential="false" activiti:collection="${subprocesslist}" activiti:elementVariable="subprocess">
<completionCondition>${cancelled=='1'}</completionCondition>
</multiInstanceLoopCharacteristics>
</callActivity>
这种情况下,activiti:in依然可以传递参数给子流程,但是activiti:out无法返回子流程的值给主流程,
org.activiti.engine.impl.bpmn.behavior.CallActivityBehavior中,单实例情形中,流程完成前会将子流程的变量传递给父流程
public void completing(DelegateExecution execution, DelegateExecution subProcessInstance) throws Exception {
// only data. no control flow available on this execution.
// copy process variables
for (AbstractDataAssociation dataOutputAssociation : dataOutputAssociations) {
Object value = null;
if (dataOutputAssociation.getSourceExpression()!=null) {
value = dataOutputAssociation.getSourceExpression().getValue(subProcessInstance);
}
else {
value = subProcessInstance.getVariable(dataOutputAssociation.getSource());
}
execution.setVariable(dataOutputAssociation.getTarget(), value);
}
}
org.activiti.engine.impl.bpmn.behavior.MultiInstanceActivityBehavior中,多实例情形中,流程完成前直接忽略了子流程变量的处理,所以activiti:out中得不到值
public void completing(DelegateExecution execution, DelegateExecution subProcessInstance) throws Exception {
// only data. no control flow available on this execution.
// copy process variables
for (AbstractDataAssociation dataOutputAssociation : dataOutputAssociations) {
Object value = null;
if (dataOutputAssociation.getSourceExpression()!=null) {
value = dataOutputAssociation.getSourceExpression().getValue(subProcessInstance);
}
else {
value = subProcessInstance.getVariable(dataOutputAssociation.getSource());
}
execution.setVariable(dataOutputAssociation.getTarget(), value);
}
}
// required for supporting external subprocesses
public void completing(DelegateExecution execution, DelegateExecution subProcessInstance) throws Exception {
}
解决方案:
将
MultiInstanceActivityBehavior类复制一份到项目中,补充comleting方法:
调用CallActivityBehavior
的completing方法,完成变量的传递。
下载
MultiInstanceActivityBehavior类