jBPM5中的Multiple Instance sub-process是较特殊的子流程,它允许多次执行包含的子流程,当一个集合中有元素存在,则子流程循环执行,直到集合中的元素为空,子流程执行结束。如下为Multiple Instance sub-process流程示意:
如图,在主流程启动时我们传入一个队列,队列中包括一系列整数,子流程运行每次判断队列中的整数能否被2整除(奇数),如果不能被则将相应的整数添加到错误列表,注意,子流程运行的次数等于队列的大小。流程运行结束打印输出错误列表。
- Multiple Instance sub-process流程运行代码:
- Multiple Instance sub-process流程:
package org.jbpm.quickstarts.subprocess;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.drools.runtime.StatefulKnowledgeSession;import org.drools.runtime.process.ProcessInstance;import org.drools.runtime.process.WorkflowProcessInstance;import org.jbpm.quickstarts.QuickStartBase;public class MultipleNodeInstanceProcessStart extends QuickStartBase { public static void main(String[] args) { new MultipleNodeInstanceProcessStart().test(); } public void test() { StatefulKnowledgeSession ksession = createKnowledgeSession("quickstarts/multipleNodeInstanceProcess.bpmn"); ListMultiple Instance sub-process流程运行结果:numbers = new ArrayList (); numbers.add(2); numbers.add(4); numbers.add(56); numbers.add(7); numbers.add(10); numbers.add(13); Map parameters = new HashMap (); parameters.put("numberList", numbers); parameters.put("errorList", new ArrayList()); //Start the process using its id ProcessInstance process = ksession.startProcess("org.jbpm.quickstarts.multiplenodeinstanceprocess",parameters); List errorList = (List) ((WorkflowProcessInstance)process).getVariable("errorList"); System.out.println("Error List: " + errorList); }}
Is 2 Even? trueIs 4 Even? trueIs 56 Even? trueIs 7 Even? falseOdd number found: 7Is 10 Even? trueIs 13 Even? falseOdd number found: 13Error List: [7, 13]如上结果显示,队列中奇数为7和13.