在BPM流程中经常需要错误升级信号,当流程中错误事件发生,错误升级信号触发,相应的错误处理操作被执行。jBPM5中使用Error Event来完成这一需求,如下为ErrorEvent流程:
如图,Embedded Sub-Process中的Validate Input Data中判断是否触发错误升级信号,如果当流程中的变量inputData为空,则触发错误升级信号,Signsl Event被触发,Inform Error节点被执行,错误被处理(添加到错误List)。否则,主流程Do Your Job!节点执行。
- ErrorEvent流程运行代码:https://github.com/kylinsoong/.../EscalationEventStart.java
- ErrorEvent流程:https://github.com/kylinsoong/.../escalationEventProcess.bpmn
运行org.jbpm.quickstarts.event.EscalationEventStart会执行ErrorEvent流程,EscalationEventStart代码如下:
package org.jbpm.quickstarts.event;
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 EscalationEventStart extends QuickStartBase{
public static void main(String[] args) {
new EscalationEventStart().test();
}
public void test() {
StatefulKnowledgeSession ksession = createKnowledgeSession("quickstarts/escalationEventProcess.bpmn");
List<String> errorList = new ArrayList<String>();
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("inputData", null);
parameters.put("errorList", errorList) ;
ProcessInstance process = ksession.startProcess("org.jbpm.quickstarts.escalationeventprocess", parameters);
errorList = (List<String> )((WorkflowProcessInstance)process).getVariable("errorList");
System.out.println("Error List: " + errorList);
}
}
ErrorEvent流程执行的结果:
Validation errors? true
Escalation handled
Error List: [Invalid input data!]
分析结果由于inputData值为空(parameters.put("inputData", null)),所以错误升级信号触发,Inform Error节点被执行。